Basically, I want to measure the text size in a TextBox, and I found out that TextRenderer gives me the correct values, while Graphics would give me wrong values. So that se
The .NET Framework uses GDI+ in most places. The Graphics
class is a wrapper around GDI+.
However, WinForms also wraps native Win32 controls, which do not use GDI+. They use GDI. TextBox and RichTextBox are examples of this. They draw their text using GDI.
To measure GDI+ text, you will use Graphics.MeasureString
(note the Graphics
class there). To measure GDI text, you will use TextRenderer.MeasureText
(note how it is not provided by the Graphics
class).
Things do get slightly more complicated: some controls do use GDI+ for drawing purposes. The Button control is an example of this. Unless you have its FlatStyle set to System, it is owner-drawn by the .NET Framework, and that owner drawing is done using GDI+ (the graphics subsystem used by .NET). However, if you set the FlatStyle to System, the rendering is delegated to the native Win32 Button control, which uses GDI (like all native Win32 controls).
My suggestion is to use GDI (TextRenderer
) whenever possible to draw text. If you use a control that implements the FlatStyle property, set it to System. Text drawn with GDI+ just looks lousy by comparison. As a bonus, you'll get controls that actually look native and blend in with the system. FlatStyle.System is the only way to get a button control that actually pulses and glows. The WinForms renderer doesn't implement this.