views:

162

answers:

2

I have a Win32 GUI application that uses GDI havily. It needs to draw text over a bitmap at specified coordinates and later erase it and substitute with the original bitmap.

I proceed as follows:

  • select font (GetStockObject( DEFAULT_GUI_FONT)), brush, other stuff into the device context
  • call GetTextExtentPoint32() to compute the size of the text
  • now having the text starting point I can compute the expected text rectangle and store it
  • call TextOut() for the same device context with the same starting point and the same text

and later restore the bitmap for the store rectangle.

It works fine when ClearType antialiasing is off. But with ClearType on the size returned by GetTextExtentPoint32() is slightly smaller than the size actually occupied by the text when TextOut() is called. So when I later restore the original bitmap some small stripes of the text remain in place and I have artifacts.

Is there any cure to this without disabling ClearType?

+1  A: 

Did you draw a string with ExtTextOut? Advances in typography and text rendering in Windows

adatapost
+2  A: 

You could also try DrawText with DT_CALCRECT to compute the string size. Maybe it works better.

Also you can then write the string with DrawText inside a rectangle with the sizes equal to the one you get with DT_CALCRECT and it will clip the text in case it is a bit larger.

Razvi
Using DrawText twice - first for size calculation, then for drawing works just fine, the text is not getting wider. Thank you.
sharptooth
The downside is that DrawText only supports fonts with zero escapement and orientation.
sharptooth