private static string TruncateMessageToFitScreen(string message) { IntPtr stockObject = Microsoft.Win32.SafeNativeMethods.GetStockObject(0x11); IntPtr dC = Microsoft.Win32.UnsafeNativeMethods.GetDC(IntPtr.Zero); NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); stockObject = Microsoft.Win32.UnsafeNativeMethods.SelectObject(dC, stockObject); Microsoft.Win32.SafeNativeMethods.GetTextMetrics(dC, tm); Microsoft.Win32.UnsafeNativeMethods.SelectObject(dC, stockObject); Microsoft.Win32.UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, dC); dC = IntPtr.Zero; int num2 = (Microsoft.Win32.UnsafeNativeMethods.GetSystemMetrics(1) / tm.tmHeight) - 15; int num3 = 0; int num4 = 0; int length = 0; while ((num3 < num2) && (length < (message.Length - 1))) { char ch = message[length]; num4++; if (((ch == '\n') || (ch == '\r')) || (num4 > 80)) { num3++; num4 = 0; } if ((ch == '\r') && (message[length + 1] == '\n')) { length += 2; } else { if ((ch == '\n') && (message[length + 1] == '\r')) { length += 2; continue; } length++; } } if (length < (message.Length - 1)) { message = SR.GetString("DebugMessageTruncated", new object[] { message.Substring(0, length) }); } return message; }
public static extern bool GetTextMetrics(IntPtr hDC, [In, Out] NativeMethods.TEXTMETRIC tm);
public static extern bool GetTextMetrics(HandleRef hDC, [In, Out] NativeMethods.TEXTMETRIC tm);