How to get real text sizes when ClearType is enabled?

I have a Win32 GUI application that uses GDI havily. He should draw the text over the bitmap with the given coordinates, and then erase it and replace it with the original bitmap.

I do the following:

  • select the font (GetStockObject (DEFAULT_GUI_FONT)), brush, other things in the device context.
  • call GetTextExtentPoint32 () to calculate the text size
  • Now, having the starting point of the text, I can calculate the expected rectangle of the text and save it
  • calling TextOut () for the same device context with the same starting point and the same text

and then restore the bitmap for the storage rectangle.

It works great when ClearType anti-aliasing is disabled. But with ClearType the size returned by GetTextExtentPoint32 () is slightly smaller than the size actually occupied by the text when TextOut () is called. Therefore, when I later restore the original bitmap, some small stripes of text remain in place, and I have artifacts.

Is there any cure for this without turning off ClearType?

+4
source share
1 answer

You can also try DrawText with DT_CALCRECT to calculate the row size. It may work better.

You can also write a line with DrawText inside the rectangle with the dimensions equal to the sizes that you get with DT_CALCRECT, and it will cut the text if it is a bit larger.

+2
source

All Articles