本例效果圖:
代碼文件: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.object Form1: TForm1
文本對齊方式:
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 348
ClIEntWidth = 383
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object PaintBox1: TPaintBox
Left = 8
Top = 195
Width = 367
Height = 145
OnPaint = PaintBox1Paint
end
object RadioGroup1: TRadioGroup
Left = 239
Top = 8
Width = 136
Height = 81
Caption = 'RadioGroup1'
TabOrder = 0
OnClick = RadioGroup1Click
end
object RadioGroup2: TRadioGroup
Left = 239
Top = 104
Width = 136
Height = 85
Caption = 'RadioGroup2'
TabOrder = 1
OnClick = RadioGroup2Click
end
object RadioGroup3: TRadioGroup
Left = 8
Top = 8
Width = 225
Height = 181
Caption = 'RadioGroup3'
TabOrder = 2
OnClick = RadioGroup3Click
end
end
文本格式化標志:
Delphi 微軟 說明 StringFormatFlagsDirectionRightToLeft DirectionRightToLeft 按從右向左的順序顯示文本。 StringFormatFlagsDirectionVertical DirectionVertical 文本垂直對齊。 StringFormatFlagsDisplayFormatControl DisplayFormatControl 控制字符(如從左到右標記)隨具有代表性的標志符號一起顯示在輸出中。 StringFormatFlagsFitBlackBox FitBlackBox 允許部分字符延伸該字符串的布局矩形。默認情況下,將重新定位字符以避免任何延伸。 StringFormatFlagsLineLimit LineLimit 在格式化的矩形中只布置整行。默認情況下,這種布置要繼續到文本的結尾為止,或者到由於剪輯而不再有可見的行為止,看哪一種情況先發生。注意,此默認設置允許不是行高整數倍的格式化矩形將最後一行部分地遮住。若要確保看到的都是整行,請指定此值,並仔細地提供格式化矩形,使其高度至少為一個行高。 StringFormatFlagsMeasureTrailingSpaces MeasureTrailingSpaces 包括每一行結尾處的尾隨空格。在默認情況下,MeasureString 方法返回的邊框都將排除每一行結尾處的空格。設置此標記以便在測定時將空格包括進去。 StringFormatFlagsNoClip NoClip 允許顯示標志符號的伸出部分和延伸到邊框外的未換行文本。在默認情況下,延伸到邊框外側的所有文本和標志符號部分都被剪裁。 StringFormatFlagsNoFontFallback NoFontFallback 對於請求的字體中不支持的字符,禁用回退到可選字體。缺失的任何字符都用缺失標志符號的字體顯示,通常是一個空的方塊。 StringFormatFlagsNoWrap NoWrap 在矩形內設置格式時,禁用文本換行功能。當傳遞的是點而不是矩形時,或者指定的矩形行長為零時,已隱含此標記。