{
public sealed class TextRenderer
{
+ private TextRenderer ()
+ {
+ }
+
#region Public Methods
[MonoTODO("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags)
{
+ if (text == null || text.Length == 0)
+ return;
+
if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
Rectangle new_bounds = bounds;
new_bounds.Offset ((int)(dc as Graphics).Transform.OffsetX + 0, (int)(dc as Graphics).Transform.OffsetY + 0);
return retval;
}
}
+
+ [MonoTODO ("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
+ public static Size MeasureText (string text, Font font, Size proposedSize, TextFormatFlags flags)
+ {
+ if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
+ if ((flags & TextFormatFlags.HidePrefix) == TextFormatFlags.HidePrefix)
+ {
+ text = text.Replace ("&", "");
+ }
+
+ Bitmap b = new Bitmap (5, 5);
+ Graphics g = Graphics.FromImage (b);
+
+ IntPtr hdc = g.GetHdc ();
+
+ if (font != null)
+ SelectObject (hdc, font.ToHfont ());
+
+ VisualStyles.UXTheme.SIZE text_size = new System.Windows.Forms.VisualStyles.UXTheme.SIZE ();
+
+ GetTextExtentPoint32 (hdc, text, text.Length, out text_size);
+
+ g.ReleaseHdc ();
+
+ Size retval = text_size.ToSize ();
+ //retval.Height += 4;
+ if (retval.Width > 0) retval.Width += 6;
+ return retval;
+ }
+ else {
+ Bitmap b = new Bitmap (5, 5);
+ Graphics g = Graphics.FromImage (b);
+
+ Size retval = g.MeasureString (text, font).ToSize ();
+ if (retval.Width > 0) retval.Width += 6;
+ return retval;
+ }
+ }
#endregion
#region Private Methods
[DllImport ("gdi32")]
private extern static IntPtr SelectObject (IntPtr hDC, IntPtr hObject);
- [DllImport ("gdi32")]
- static extern int SetBkColor (IntPtr hdc, int crColor);
+ //[DllImport ("gdi32")]
+ //static extern int SetBkColor (IntPtr hdc, int crColor);
[DllImport ("gdi32")]
static extern int SetBkMode (IntPtr hdc, int iBkMode);
- [DllImport ("gdi32")]
- static extern bool GetTextExtentExPoint (IntPtr hdc, string lpszStr, int cchString, int nMaxExtent, IntPtr lpnFit, IntPtr alpDx, out VisualStyles.UXTheme.SIZE lpSize);
+ //[DllImport ("gdi32")]
+ //static extern bool GetTextExtentExPoint (IntPtr hdc, string lpszStr, int cchString, int nMaxExtent, IntPtr lpnFit, IntPtr alpDx, out VisualStyles.UXTheme.SIZE lpSize);
[DllImport ("gdi32")]
static extern bool GetTextExtentPoint32 (IntPtr hdc, string lpString, int cbString, out VisualStyles.UXTheme.SIZE lpSize);
}
}
-#endif
\ No newline at end of file
+#endif