本文介绍了Delphi中的字体平滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有理由需要一个Delphi表单上的一个大字体的标签,并注意到
它的曲线仍然是微小的锯齿状。我将这个与MSWord中相同大小的
和字体进行了比较,这更加顺畅。经过研究,我发现代码
,让我平滑我的字体,但它是凌乱的,我想知道如果
有更好的方法?看看VCL的来源,TFont似乎融入了
NONANTIALIASED_QUALITY这是非常令人沮丧的...

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

感谢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;


推荐答案

你可以欺骗VCL创建自己的类继承自 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.

为您自己创建一个新的单元, code> TLabel class:

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.

现在修改包含标签的表单单元,添加 AntiAliasedLabel unit after StdCtrls 。这将导致您自己的类正在创建的AntiAliasedLabel.TLabel 正常 StdCtrls.TLabel 将被创建。

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.

这篇关于Delphi中的字体平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 12:20