4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Copyright (c) 2006 Novell, Inc.
26 // Jonathan Pobst (monkey@jpobst.com)
31 using System.Runtime.InteropServices;
33 using System.Drawing.Text;
35 namespace System.Windows.Forms
37 public sealed class TextRenderer
39 private TextRenderer ()
43 #region Public Methods
44 [MonoTODO("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
45 public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags)
47 if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
48 Rectangle new_bounds = bounds;
49 new_bounds.Offset ((int)(dc as Graphics).Transform.OffsetX + 0, (int)(dc as Graphics).Transform.OffsetY + 0);
50 IntPtr hdc = dc.GetHdc ();
52 SetTextColor (hdc, ColorTranslator.ToWin32 (foreColor));
53 SetBkMode (hdc, 1); //1-Transparent, 2-Opaque
55 VisualStyles.UXTheme.RECT r = VisualStyles.UXTheme.RECT.FromRectangle (new_bounds);
58 SelectObject (hdc, font.ToHfont ());
60 DrawText (hdc, text, text.Length, ref r, (int)flags);
69 g = Graphics.FromHdc (dc.GetHdc ());
71 StringFormat sf = FlagsToStringFormat (flags);
73 using (Brush b = new SolidBrush (foreColor))
74 g.DrawString (text, font, b, bounds, sf);
76 if (!(dc is Graphics)) {
83 [MonoTODO ("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
84 public static void DrawText (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags)
86 if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
87 IntPtr hdc = dc.GetHdc ();
89 SetTextColor (hdc, ColorTranslator.ToWin32 (foreColor));
90 SetBkMode (hdc, 1); //1-Transparent, 2-Opaque
92 Size sz = MeasureText(text, font);
94 VisualStyles.UXTheme.RECT r = new System.Windows.Forms.VisualStyles.UXTheme.RECT(pt.X, pt.Y, pt.X + sz.Width, pt.Y + sz.Height);
97 SelectObject (hdc, font.ToHfont ());
99 DrawText (hdc, text, text.Length, ref r, (int)flags);
108 g = Graphics.FromHdc (dc.GetHdc ());
110 StringFormat sf = FlagsToStringFormat (flags);
112 using (Brush b = new SolidBrush (foreColor))
113 g.DrawString (text, font, b, pt, sf);
115 if (!(dc is Graphics)) {
122 [MonoTODO ("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
123 public static Size MeasureText (string text, Font font)
125 if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
126 Bitmap b = new Bitmap (5, 5);
127 Graphics g = Graphics.FromImage (b);
129 IntPtr hdc = g.GetHdc ();
132 SelectObject (hdc, font.ToHfont ());
134 VisualStyles.UXTheme.SIZE text_size = new System.Windows.Forms.VisualStyles.UXTheme.SIZE();
136 GetTextExtentPoint32 (hdc, text, text.Length, out text_size);
140 Size retval = text_size.ToSize();
141 //retval.Height += 4;
142 if (retval.Width > 0) retval.Width += 6;
146 Bitmap b = new Bitmap (5, 5);
147 Graphics g = Graphics.FromImage (b);
149 Size retval = g.MeasureString(text,font).ToSize();
150 if (retval.Width > 0) retval.Width += 6;
156 #region Private Methods
157 private static StringFormat FlagsToStringFormat (TextFormatFlags flags)
159 StringFormat sf = new StringFormat ();
161 // Translation table: http://msdn.microsoft.com/msdnmag/issues/06/03/TextRendering/default.aspx?fig=true#fig4
163 // Horizontal Alignment
164 if ((flags & TextFormatFlags.HorizontalCenter) == TextFormatFlags.HorizontalCenter)
165 sf.Alignment = StringAlignment.Center;
166 else if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right)
167 sf.Alignment = StringAlignment.Far;
169 sf.Alignment = StringAlignment.Near;
171 // Vertical Alignment
172 if ((flags & TextFormatFlags.Bottom) == TextFormatFlags.Bottom)
173 sf.LineAlignment = StringAlignment.Far;
174 else if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter)
175 sf.LineAlignment = StringAlignment.Center;
177 sf.LineAlignment = StringAlignment.Near;
180 if ((flags & TextFormatFlags.EndEllipsis) == TextFormatFlags.EndEllipsis)
181 sf.Trimming = StringTrimming.EllipsisCharacter;
182 else if ((flags & TextFormatFlags.PathEllipsis) == TextFormatFlags.PathEllipsis)
183 sf.Trimming = StringTrimming.EllipsisPath;
184 else if ((flags & TextFormatFlags.WordEllipsis) == TextFormatFlags.WordEllipsis)
185 sf.Trimming = StringTrimming.EllipsisWord;
187 sf.Trimming = StringTrimming.Character;
190 if ((flags & TextFormatFlags.NoPrefix) == TextFormatFlags.NoPrefix)
191 sf.HotkeyPrefix = HotkeyPrefix.None;
192 else if ((flags & TextFormatFlags.HidePrefix) == TextFormatFlags.HidePrefix)
193 sf.HotkeyPrefix = HotkeyPrefix.Hide;
195 sf.HotkeyPrefix = HotkeyPrefix.Show;
198 if ((flags & TextFormatFlags.NoPadding) == TextFormatFlags.NoPadding)
199 sf.FormatFlags |= StringFormatFlags.FitBlackBox;
202 if ((flags & TextFormatFlags.SingleLine) == TextFormatFlags.SingleLine)
203 sf.FormatFlags |= StringFormatFlags.NoWrap;
204 else if ((flags & TextFormatFlags.TextBoxControl) == TextFormatFlags.TextBoxControl)
205 sf.FormatFlags |= StringFormatFlags.LineLimit;
208 if ((flags & TextFormatFlags.RightToLeft) == TextFormatFlags.RightToLeft)
209 sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
210 if ((flags & TextFormatFlags.NoClipping) == TextFormatFlags.NoClipping)
211 sf.FormatFlags |= StringFormatFlags.NoClip;
212 sf.FormatFlags |= StringFormatFlags.NoClip;
218 #region DllImports (Windows)
219 [DllImport ("user32", CharSet = CharSet.Unicode)]
220 static extern int DrawText (IntPtr hdc, string lpStr, int nCount, ref VisualStyles.UXTheme.RECT lpRect, int wFormat);
222 [DllImport ("gdi32")]
223 static extern int SetTextColor (IntPtr hdc, int crColor);
225 [DllImport ("gdi32")]
226 private extern static IntPtr SelectObject (IntPtr hDC, IntPtr hObject);
228 [DllImport ("gdi32")]
229 static extern int SetBkColor (IntPtr hdc, int crColor);
231 [DllImport ("gdi32")]
232 static extern int SetBkMode (IntPtr hdc, int iBkMode);
234 [DllImport ("gdi32")]
235 static extern bool GetTextExtentExPoint (IntPtr hdc, string lpszStr, int cchString, int nMaxExtent, IntPtr lpnFit, IntPtr alpDx, out VisualStyles.UXTheme.SIZE lpSize);
237 [DllImport ("gdi32")]
238 static extern bool GetTextExtentPoint32 (IntPtr hdc, string lpString, int cbString, out VisualStyles.UXTheme.SIZE lpSize);