如何在使用Win32 API绘制文本时覆盖ClearType设置?

8

我写了一个小应用程序,可以在内存中的图片上绘制文本,并将其写入文件。基本的 Delphi 代码类似于:

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;

不幸的是,根据运行应用程序的计算机的ClearType设置而异的绘制文本。我希望在我的应用程序中有一致的输出,无论本地ClearType设置如何(输出也不直接显示到屏幕上)。是否有一些Win32 API选项来覆盖本地ClearType设置?

2个回答

12
The font smoothing of text is determined by the font that you select into the device. To learn about the options offered by the raw Win32 interface, read the LOGFONT documentation.
在Delphi中,底层的Win32 API字体API由TFont类封装。对于这个问题相关的属性是Quality。默认值为fqDefault,使用全局字体平滑设置。您需要将Quality设置为fqAntialiasedfqNonAntialiased
旧版本的Delphi没有这个属性。在这种情况下,您需要调用CreateFontIndirect创建具有所需质量设置的HFONT。您可以在开始绘制文本之前立即调用此函数
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;

根据您的需求,传递NONANTIALIASED_QUALITYANTIALIASED_QUALITY


6
我相信你可以创建一个新的逻辑字体,不使用任何ClearType。请确保将NONANTIALIASED_QUALITY标志作为CreateFontfdwQuality参数传递:

字体从未进行抗锯齿处理,即不进行字体平滑处理。


4
您还可以使用抗锯齿质量,这将呈现旧式抗锯齿 - 即没有ClearType技术。这样可以得到体面的文字质量,比完全没有字体平滑处理的文字要好看得多。 - David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接