本例效果圖:
代碼文件:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
RadioGroup1: TRadioGroup;
RadioGroup2: TRadioGroup;
RadioGroup3: TRadioGroup;
PaintBox1: TPaintBox;
procedure FormCreate(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure RadioGroup2Click(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure RadioGroup3Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI, TypInfo;
var
sfFlag: Integer;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to 2 do
RadioGroup1.Items.Add(GetEnumName(TypeInfo(TStringAlignment), i));
RadioGroup1.ItemIndex := 0;
RadioGroup2.Items := RadioGroup1.Items;
RadioGroup2.ItemIndex := 0;
RadioGroup3.Items.CommaText := 'StringFormatFlagsDirectionRightToLeft,' +
'StringFormatFlagsDirectionVertical,' +
'StringFormatFlagsNoFitBlackBox,' +
'StringFormatFlagsDisplayFormatControl,' +
'StringFormatFlagsNoFontFallback,' +
'StringFormatFlagsMeasureTrailingSpaces,' +
'StringFormatFlagsNoWrap,' +
'StringFormatFlagsLineLimit,' +
'StringFormatFlagsNoClip';
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
g: TGPGraphics;
b: TGPBrush;
font: TGPFont;
sf: TGPStringFormat;
rect: TGPRectF;
begin
g := TGPGraphics.Create(PaintBox1.Canvas.Handle);
b := TGPSolidBrush.Create($FF000000);
g.SetTextRenderingHint(TextRenderingHintAntiAlias);
font := TGPFont.Create('Arial Black', 22);
sf := TGPStringFormat.Create;
sf.SetFormatFlags(StringFormatFlagsNoClip);
sf.SetAlignment(TStringAlignment(RadioGroup1.ItemIndex));
sf.SetLineAlignment(TStringAlignment(RadioGroup2.ItemIndex));
sf.SetFormatFlags(sfFlag);
rect.X := 0;
rect.Y := 0;
rect.Width := PaintBox1.ClientWidth;
rect.Height := PaintBox1.ClientHeight;
g.DrawString('Delphi', -1, font, rect, sf, b);
sf.Free;
font.Free;
b.Free;
g.Free;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
PaintBox1.Repaint;
end;
procedure TForm1.RadioGroup2Click(Sender: TObject);
begin
PaintBox1.Repaint;
end;
procedure TForm1.RadioGroup3Click(Sender: TObject);
begin
case RadioGroup3.ItemIndex of
0: sfFlag := StringFormatFlagsDirectionRightToLeft;
1: sfFlag := StringFormatFlagsDirectionVertical;
2: sfFlag := StringFormatFlagsNoFitBlackBox;
3: sfFlag := StringFormatFlagsDisplayFormatControl;
4: sfFlag := StringFormatFlagsNoFontFallback;
5: sfFlag := StringFormatFlagsMeasureTrailingSpaces;
6: sfFlag := StringFormatFlagsNoWrap;
7: sfFlag := StringFormatFlagsLineLimit;
8: sfFlag := StringFormatFlagsNoClip;
end;
PaintBox1.Repaint;
end;
end.