private static string TruncateMessageToFitScreen(string message) { const int MaxCharsPerLine = 80; IntPtr hFont = SafeNativeMethods.GetStockObject(NativeMethods.DEFAULT_GUI_FONT); IntPtr hdc = UnsafeNativeMethods.GetDC(IntPtr.Zero); NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); hFont = UnsafeNativeMethods.SelectObject(hdc, hFont); SafeNativeMethods.GetTextMetrics(hdc, tm); UnsafeNativeMethods.SelectObject(hdc, hFont); UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, hdc); hdc = IntPtr.Zero; int cy = UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CYSCREEN); int maxLines = cy / tm.tmHeight - 15; int lineCount = 0; int lineCharCount = 0; int i = 0; while (lineCount < maxLines && i < message.Length - 1) { char ch = message[i]; lineCharCount++; if (ch == '\n' || ch == '\r' || lineCharCount > MaxCharsPerLine) { lineCount++; lineCharCount = 0; } // treat \r\n or \n\r as a single line break if (ch == '\r' && message[i + 1] == '\n') { i += 2; } else if (ch == '\n' && message[i + 1] == '\r') { i += 2; } else { i++; } } if (i < message.Length - 1) { message = SR.GetString(SR.DebugMessageTruncated, message.Substring(0, i)); } return(message); }
private static string TruncateMessageToFitScreen(string message) { const int MaxCharsPerLine = 80; IntPtr hFont = SafeNativeMethods.GetStockObject(NativeMethods.DEFAULT_GUI_FONT); IntPtr hdc = UnsafeNativeMethods.GetDC(IntPtr.Zero); NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); hFont = UnsafeNativeMethods.SelectObject(hdc, hFont); SafeNativeMethods.GetTextMetrics(hdc, tm); UnsafeNativeMethods.SelectObject(hdc, hFont); UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, hdc); hdc = IntPtr.Zero; int cy = UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CYSCREEN); int maxLines = cy / tm.tmHeight - 15; int lineCount = 0; int lineCharCount = 0; int i = 0; while (lineCount < maxLines && i < message.Length - 1) { char ch = message[i]; lineCharCount++; if (ch == '\n' || ch == '\r' || lineCharCount > MaxCharsPerLine) { lineCount++; lineCharCount = 0; } // treat \r\n or \n\r as a single line break if (ch == '\r' && message[i + 1] == '\n') i+=2; else if (ch == '\n' && message[i + 1] == '\r') i+=2; else i++; } if (i < message.Length - 1) message = SR.GetString(SR.DebugMessageTruncated, message.Substring(0, i)); return message; }