Delphi 中的字体平滑

发布于 2024-07-22 03:40:29 字数 717 浏览 8 评论 0原文

我有理由需要在 Delphi 表单上使用大字体的标签,并注意到 它的曲线仍然略有锯齿状。 我把它与相同尺寸的进行了比较 MSWord 中的字体更加流畅。 经过研究我发现了代码 这让我可以平滑我的字体,但它很混乱,我想知道是否 还有更好的办法吗? 查看 VCL 源代码,TFont 似乎致力于 NONANTIALIASED_QUALITY 这相当令人沮丧......

谢谢 Bri

procedure TForm1.SetFontSmoothing(AFont: TFont);
var
  tagLOGFONT: TLogFont;
begin
  GetObject(
    AFont.Handle,
    SizeOf(TLogFont),
    @tagLOGFONT);
  tagLOGFONT.lfQuality  := ANTIALIASED_QUALITY;
  AFont.Handle := CreateFontIndirect(tagLOGFONT);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I : integer;
begin
  For I :=0 to ComponentCount-1 do
    If Components[I] is TLabel then
      SetFontSmoothing( TLabel( Components[I] ).Font );
end;

I had cause to need a label with a large font on a Delphi form and noticed that
its curves were still slightly jagged. I compared this with the same size
and font in MSWord which was much smoother. After research I found code
that allowed me to smooth my fonts but it's messy and I was wondering if
there was a better way? Looking in the VCL source, TFont seems wedded to
NONANTIALIASED_QUALITY which is rather frustrating...

Thanks Bri

procedure TForm1.SetFontSmoothing(AFont: TFont);
var
  tagLOGFONT: TLogFont;
begin
  GetObject(
    AFont.Handle,
    SizeOf(TLogFont),
    @tagLOGFONT);
  tagLOGFONT.lfQuality  := ANTIALIASED_QUALITY;
  AFont.Handle := CreateFontIndirect(tagLOGFONT);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I : integer;
begin
  For I :=0 to ComponentCount-1 do
    If Components[I] is TLabel then
      SetFontSmoothing( TLabel( Components[I] ).Font );
end;

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

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

发布评论

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

评论(4

删除→记忆 2024-07-29 03:40:29

您可以欺骗 VCL 创建您自己的继承自 TLabel 的类。 这是概念验证代码,使用 Delphi 4 进行了测试,应该可以帮助您入门。

为您自己的 TLabel 类创建一个新单元:

unit AntiAliasedLabel;

interface

uses
  Windows, Messages, SysUtils, Controls, StdCtrls, Graphics;

type
  TLabel = class(StdCtrls.TLabel)
  private
    fFontChanged: boolean;
  public
    procedure Paint; override;
  end;

implementation

procedure TLabel.Paint;
var
  LF: TLogFont;
begin
  if not fFontChanged then begin
    Win32Check(GetObject(Font.Handle, SizeOf(TLogFont), @LF) <> 0);
    LF.lfQuality := ANTIALIASED_QUALITY;
    Font.Handle := CreateFontIndirect(LF);
    fFontChanged := TRUE;
  end;
  inherited;
end;

end.

现在修改您的表单单元包含标签,在StdCtrls之后添加AntiAliasedLabel单元。 这会导致在通常创建 StdCtrls.TLabel 的位置创建您自己的类 AntiAliasedLabel.TLabel

You can trick the VCL into creating your own class that inherits from TLabel. This is proof-of-concept code, tested with Delphi 4, which should get you started.

Create a new unit for your own TLabel class:

unit AntiAliasedLabel;

interface

uses
  Windows, Messages, SysUtils, Controls, StdCtrls, Graphics;

type
  TLabel = class(StdCtrls.TLabel)
  private
    fFontChanged: boolean;
  public
    procedure Paint; override;
  end;

implementation

procedure TLabel.Paint;
var
  LF: TLogFont;
begin
  if not fFontChanged then begin
    Win32Check(GetObject(Font.Handle, SizeOf(TLogFont), @LF) <> 0);
    LF.lfQuality := ANTIALIASED_QUALITY;
    Font.Handle := CreateFontIndirect(LF);
    fFontChanged := TRUE;
  end;
  inherited;
end;

end.

Now modify your form unit that contains the label, adding the AntiAliasedLabel unit after StdCtrls. This results in your own class AntiAliasedLabel.TLabel being created where normally StdCtrls.TLabel would be created.

避讳 2024-07-29 03:40:29

恕我直言,VCL 应该检查系统默认字体平滑并在运行时将其应用为默认值。 如果不是,至少应该默认为更合理的平滑。 在这种情况下,有人可能会争辩说,考虑到 > ClearType 将是更好的默认值。 如今 50% 的显示器是 LCD(超过 50% 的机器运行 XP 或更高版本)。

这是一个公认的黑客行为(正如 Ken White 提到的,如果有替代方案,这不是最好的方法),但我需要一种方法来全局修复包含数百个第三方组件类型的表单(使组件继承不切实际)。

我更改了 Graphics.pas、TFont.GetHandle 中的默认字体质量,如下所示:

// lfQuality := DEFAULT_QUALITY;

lf 质量 := 5; // (HACK) CLEARTYPE_QUALITY,强制使用cleartype

IMHO, the VCL should be checking the System default font smoothing and applying that as the default at run-time. If not, at least it should default to a more reasonable smoothing. One could argue, in this case, that ClearType would be a better default, considering > 50% of monitors these days are LCD (and greater than 50% of machines are running XP or better).

This is an acknowledged hack (and as Ken White mentions, not the best approach if there are alternatives), but I needed a way to fix this globally for forms containing literally hundreds of 3rd-party component types (making component inheritance unrealistic).

I changed the default Font Quality in Graphics.pas, TFont.GetHandle as follows:

// lfQuality := DEFAULT_QUALITY;

lfQuality := 5; // (HACK) CLEARTYPE_QUALITY, forces cleartype

不如归去 2024-07-29 03:40:29

来自:http://objectmix.com/delphi/725245-tlabel-antialiasing-possibile-3。 html

“只需使用支持字体平滑的字体就可以解决此问题。Delphi
默认使用(或至少习惯使用)MS Sans Serif,这并不
支持平滑(ClearType 或其他)。 如果你将字体设置为
Tahoma(最适合 XP)或 Segoe UI(最适合 Vista),您将
根据系统设置自动获得字体平滑
Delphi 应用程序。”

已确认,它在使用 Delphi XE 和 Win7 时工作得非常好。现在正忙于更改我所有的字体;-)

From: http://objectmix.com/delphi/725245-tlabel-antialiasing-possibile-3.html

"simply using a font that supports font smoothing should fix this. Delphi
uses (or at least used to use) MS Sans Serif as default, which does not
support smoothing (ClearType or otherwise). if you set your font to
Tahoma (best for XP) or Segoe UI (best for Vista), you will
automatically get font smoothing according to system settings in your
Delphi app."

Confirmed, it works beautifully using Delphi XE and Win7. Busy changing all my fonts right now ;-)

帅冕 2024-07-29 03:40:29

最简单的方法是基于 TLabel 创建您自己的组件,例如 TSmoothLabel 或 TAntiAliasedLabel,并向其中添加您的平滑代码。 然后您使用您的组件而不是标准 TLabel。

The easiest way is to create your own component based on TLabel, such as TSmoothLabel or TAntiAliasedLabel, and add your smoothing code to it. Then you use your component instead of the standard TLabel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文