Discussion:
Text Rendering accuracy problem in TextBox/RichTextBox
(too old to reply)
Sichel Mond
2009-04-10 12:49:48 UTC
Permalink
Hi all

I need to display text in a RichTextBox or a TextBox EXACTLY as
windows renders it onto controls like buttons and labels.
Unfortunately, this is not the case by default. Some font settings
like Arial 8.25 point Regular look very similar in a TextBox and on a
Button,
while other settings like Arial 13 point Regular do not. The most
promising method to draw text exactly as the .NET Framework does it
internally is
by using TextRenderer.DrawText.
What I need is a TextBox-like control that uses TextRenderer.DrawText.
Does anyone know/have such a control? Or at least can help me
developing one? It would be very much appreciated.

thanks in advance!
Morten Wennevik [C# MVP]
2009-04-15 06:14:01 UTC
Permalink
Hi Sichel,

The difference you see is the GDI vs GDI+ rendering. .Net uses GDI+ which
is resolution independent, but some controls like RichTextBox are just
wrapped win32 controls which use the resolution dependent GDI. TextRenderer
actually uses GDI, which will let you draw text like the RichTextBox does,
but it is not how .Net draws it. If you want to draw text as .Net does it,
you need to use Graphics.DrawString.

There is an article on the subject
http://support.microsoft.com/kb/307208

TextBox and Button both use GDI+ whereas RichTextBox uses GDI, but as you
point out there is a difference at Arial 13. This, however is caused by the
fact the Arial does not have a font resolution for 13 and for some reason
TextBox and Button defaults to different resolutions. TextBox drops back to
12,75 which is the closest resolution whereas Button opts for the higher
resolution of 13,5. This will only happen if you change the font resolution
to 13 manually instead of using the font dialog. Try using 12,75 or 13,5
--
Happy Coding!
Morten Wennevik [C# MVP]
Post by Sichel Mond
Hi all
I need to display text in a RichTextBox or a TextBox EXACTLY as
windows renders it onto controls like buttons and labels.
Unfortunately, this is not the case by default. Some font settings
like Arial 8.25 point Regular look very similar in a TextBox and on a
Button,
while other settings like Arial 13 point Regular do not. The most
promising method to draw text exactly as the .NET Framework does it
internally is
by using TextRenderer.DrawText.
What I need is a TextBox-like control that uses TextRenderer.DrawText.
Does anyone know/have such a control? Or at least can help me
developing one? It would be very much appreciated.
thanks in advance!
Sichel Mond
2009-05-25 14:20:32 UTC
Permalink
On Apr 15, 8:14 am, Morten Wennevik [C# MVP]
Post by Morten Wennevik [C# MVP]
Hi Sichel,
The difference you see is the GDI vs GDI+ rendering.  .Net uses GDI+ which
is resolution independent, but some controls like RichTextBox are just
wrapped win32 controls which use the resolution dependent GDI.  TextRenderer
actually uses GDI, which will let you draw text like the RichTextBox does,
but it is not how .Net draws it.  If you want to draw text as .Net does it,
you need to use Graphics.DrawString.  
There is an article on the subjecthttp://support.microsoft.com/kb/307208
TextBox and Button both use GDI+ whereas RichTextBox uses GDI, but as you
point out there is a difference at Arial 13.  This, however is caused by the
fact the Arial does not have a font resolution for 13 and for some reason
TextBox and Button defaults to different resolutions.  TextBox drops back to
12,75 which is the closest resolution whereas Button opts for the higher
resolution of 13,5.  This will only happen if you change the font resolution
to 13 manually instead of using the font dialog.  Try using 12,75 or 13,5
--
Happy Coding!
Morten Wennevik [C# MVP]
Post by Sichel Mond
Hi all
I need to display text in a RichTextBox or a TextBox EXACTLY as
windows renders it onto controls like buttons and labels.
Unfortunately, this is not the case by default. Some font settings
like Arial 8.25 point Regular look very similar in a TextBox and on a
Button,
while other settings like Arial 13 point Regular do not. The most
promising method to draw text exactly as the .NET Framework does it
internally is
by using TextRenderer.DrawText.
What I need is a TextBox-like control that uses TextRenderer.DrawText.
Does anyone know/have such a control? Or at least can help me
developing one? It would be very much appreciated.
thanks in advance!
Hej Morten (and all others listening on the channel)

Thanks a lot for the information you've posted (quite some time ago).
I have been busy working on other projects but now the GDI(+) forces
are against me again:

Is there any more elegant way to determine a TextBox or RichTextBox
fallback-fontsize than by
Creating a RTB -> set the font -> set some text -> get the rtf-string -
Post by Morten Wennevik [C# MVP]
create new RTB -> get font size ?
Or is it possible to retrieve the availlable sizes for a given
FontFamily?

Your help would be, once again, very much appreciated!

Loading...