Share via


UseCompatibleTextRendering - Compatible with whaaaaaat?

There seems to be quite a bit of confusion around this little old flag that has cropped up in VS 2005. 

In Windows Forms 2.0, we added support for drawing GDI text. At first we had grandiose plans of poking and prodding at the DrawText API such that we could make it match up exactly how GDI+'s DrawString API works.  I actually think we got pretty close, but there are fundamental differences in word wrapping and character spacing that as mere consumers of the both APIs, Windows Forms could not solve.

So now we're presented with a problem: we want to switch everyone over to the new TextRenderer APIs so text will look better, localize better, draw more consistently with other dialogs in the operating system... ...but we dont want to break folks counting on GDI+ measure string for calculations of where their text should line up.

Long story short UseCompatibleTextRendering means "compatible" with version 1.0 and 1.1 of the framework. If you built your app using Visual Studio 2003, running your app on version 2.0 of the framework should *just* work.

In order to easily switch ALL of your controls over to the new GDI TextRenderer, use Application.SetCompatibleTextRenderingDefault(). 

Note an individual control when it is painting/measuring checks it's value of UseCompatibleTextRendering to do its decision making between GDI/GDI+ measuring/drawing so setting the value of this property directly on the control trumps whatever you've set in Application's handy helper method.

In summary

Control.UseCompatibleTextRendering
Sets/Gets whether a control should use GDI+ or GDI for text drawing.

True Use GDI+/Graphics.DrawString Behaves same as 1.1
False Use GDI/TextRenderer Behaves similar to 1.1, looks nicer, localizes better

Application.SetCompatibleTextRenderingDefault
Handy helper method for setting UseCompatibleTextRendering on ALL of your controls.

Oh, and one more thing...

When you switch over to UseCompatibleTextRendering = false (which I know you'll want to), if you were using graphics.MeasureString, you will want to move over to TextRenderer.MeasureText. As noted above there are differences in how the two technologies word wrap, character space etc. 

A common case for using MeasureString is figuring out how to size your label control. Instead of blindly moving over to TextRenderer.MeasureText, I would recommend just calling label.GetPreferredSize( ) with the same constraints you were passing into MeasureString. This will correctly switch between the two measuring technologies, you dont have to create a graphics object, and it may even help your perf a little. (no guarantees on the last one, but sometimes this value is cached.)

Comments

  • Anonymous
    October 13, 2005
    It would be interesting to hear why the GDI+ text rendering functionality is inferior to the GDI one. One would think that the + would mean something....
  • Anonymous
    October 13, 2005
    A lot of this was discussed on Michael Kaplan's blog:
    http://blogs.msdn.com/michkap/archive/2005/06/27/432986.aspx

    From an API perspective, I enjoy programming to the GDI+ model more than the GDI - I've been bitten by the "stateful" GDI model, but I've also been bitten by the "stateless" GDI+ model.

    In general, because of power/ease of use I would still recommend using GDI+ for pretty much everything except text.

    That said, some folks may still want to p/invoke other aspects of the GDI library to compare performance between the two models.
  • Anonymous
    October 14, 2005
    GDI gives you a some stuff that GDI+ doesn't, specifically hardware acceleration and support for complex scripts and the Uniscribe APIs.

    That said, GDI+ is much easier to use for most things, and gives you things GDI doesn't like textures, some alpha functionality, jpg/gif support, etc.
  • Anonymous
    September 15, 2006
    UseCompatibleTextRendering にもてあそばれる
  • Anonymous
    September 15, 2006
    UseCompatibleTextRendering にもてあそばれる
  • Anonymous
    January 20, 2009
    PingBack from http://www.hilpers.com/266054-problem-mit-measurestring