在 Delphi 中使用 TextRect (又名 GDI32 中的 ExtTextOut)时,有没有办法禁用字体抗锯齿?

发布于 2024-09-19 20:04:08 字数 316 浏览 8 评论 0原文

我正在使用一个自定义仪表,基于 Delphi (5 Enterprise) 附带的示例。对于那些不知道的人来说,它就像一个平滑的进度条,但在组件的中心(垂直和水平)显示百分比或值。

为了确保文本在仪表已满和为空时都可读,文本将使用反色显示。

当使用字体抗锯齿功能时,这些反转的颜色会导致字体边缘显示出非常疯狂的颜色,从而破坏组件的外观。

有什么方法可以仅针对这一组件禁用字体平滑/抗锯齿,或者禁用它,绘制文本,然后重新启用它?

我当前的解决方法是使用不会平滑的字体,例如“MS Sans Serif”,但我想使用与 UI 其余部分相同的字体以保持一致性。

I'm using a custom gauge, based on the example that came with Delphi (5 Enterprise). For those that don't know, it's like a smooth progress bar, but displays the percentage or value in the centre (vertically and horizontally) of the component.

To make sure the text is readable both when the gauge is filled and when it's empty, the text is displayed using inverted colours.

When font anti-aliasing is used, these inverted colours cause the edge of the font to appear in really crazy colours, ruining the look of the component.

Is there any way to disable font smoothing / anti aliasing for just this one component, or disable it, draw the text, then re-enable it?

My current workaround is to use a font that doesn't get smoothed, like "MS Sans Serif", but I'd like to use the same font as the rest of the UI for consistency.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-09-26 20:04:23

LOGFONT 结构中指定 NONANTIALIASED_QUALITY 应关闭抗锯齿功能:

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;

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  FontQualities: array[Boolean] of Byte = (DEFAULT_QUALITY, NONANTIALIASED_QUALITY);
var
  Canvas: TCanvas;
begin
  Canvas := (Sender as TPaintBox).Canvas;
  SetFontQuality(Canvas.Font, FontQualities[CheckBox1.Checked]);
  Canvas.TextOut(12, 12, 'Hello, world!');
end;

Specifying NONANTIALIASED_QUALITY in the LOGFONT structure should turn antialiasing off:

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;

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  FontQualities: array[Boolean] of Byte = (DEFAULT_QUALITY, NONANTIALIASED_QUALITY);
var
  Canvas: TCanvas;
begin
  Canvas := (Sender as TPaintBox).Canvas;
  SetFontQuality(Canvas.Font, FontQualities[CheckBox1.Checked]);
  Canvas.TextOut(12, 12, 'Hello, world!');
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文