How can I override the ClearType parameter when drawing text using the Win32 API?

I wrote a small application that draws text on images in memory and writes them to files. The main Delphi code looks like this:

var Canvas : tCanvas; Text : WideString; TextRect : tRect; begin Canvas := Bitmap.Canvas; Canvas.Brush.Color := clBlack; Canvas.Pen.Color := clBlack; Canvas.Font.Name := 'Courier New'; Canvas.Font.Size := 11; Canvas.Font.Color := clWhite; TextRect := ...; // calculate text position DrawTextW(Canvas.Handle, PWideChar(Text), Length(Text), TextRect, DT_NOCLIP or DT_NOPREFIX or DT_SINGLELINE or DT_CENTER or DT_VCENTER); end; 

Unfortunately, the text drawn differs depending on the ClearType parameter of the computer on which the application is running. I would like to have consistent output in my application regardless of the local ClearType parameter (the output does not appear on the screen in any case). Is there a Win32 API option to override local ClearType settings?

+8
windows winapi delphi delphi-2006
source share
2 answers

The font smoothing of the text is determined by the font that you select in the device. For information on the options offered by the raw Win32 interface, read the LOGFONT documentation.

In Delphi, the core Win32 API ends with the TFont class. The property related to this issue is Quality . The default value is fqDefault , which uses the system font smoothing setting. You want to set Quality to fqAntialiased or fqNonAntialiased .

Older versions of Delphi do not have this property. In this case, you will need to call CreateFontIndirect to create the HFONT with the required quality settings. You can call this function just before you start drawing text:

 procedure SetFontQuality(Font: TFont; Quality: Byte); var LogFont: TLogFont; begin if GetObject(Font.Handle, SizeOf(TLogFont), @LogFont) = 0 then RaiseLastOSError; LogFont.lfQuality := Quality; Font.Handle := CreateFontIndirect(LogFont); end; 

Pass either NONANTIALIASED_QUALITY or ANTIALIASED_QUALITY depending on your needs.

+12
source share

I believe that you can create a new logical font that does not use ClearType. Be sure to pass the NONANTIALIASED_QUALITY flag as the fdwQuality CreateFont parameter:

The font is never smoothed, that is, the font is not smoothed.

+6
source share

All Articles