//在矩形范圍中顯示文本var
//設置對齊方式
g: TGPGraphics;
fontFamily: TGPFontFamily;
font: TGPFont;
rectF: TGPRectF;
p: TGPPen;
sb: TGPSolidBrush;
str: WideString;
begin
g := TGPGraphics.Create(Canvas.Handle);
fontFamily:= TGPFontFamily.Create('宋體');
font:= TGPFont.Create(fontFamily, 9, FontStyleRegular, UnitPoint);
p := TGPPen.Create(aclRed);
sb:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 0));
rectF:= MakeRect(10.0, 10.0, 116.0, 100.0); {包含文本的矩形范圍}
str := '萬一萬一萬一萬一萬一萬一萬一萬一萬一萬一萬一萬一萬一萬一萬一';
g.DrawRectangle(p, rectF);
g.DrawString(str, -1, font, rectF, nil, sb);
{參數4不能用TGPRect,只能是TGPRectF; 參數5是文字格式對象,後面舉例}
fontFamily.Free;
font.Free;
p.Free;
sb.Free;
g.Free;
end;var
//使用格式化標志創建文本格式化對象
g: TGPGraphics;
str: String;
fontFamily: TGPFontFamily;
font: TGPFont;
rectF: TGPRectF;
strFormat: TGPStringFormat;
sb: TGPSolidBrush;
p: TGPPen;
begin
g := TGPGraphics.Create(Canvas.Handle);
fontFamily:= TGPFontFamily.Create('Arial');
font:= TGPFont.Create(fontFamily, 12, FontStyleBold, UnitPoint);
rectF:= MakeRect(30.0, 10.0, 120.0, 140.0);
sb:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 255));
strFormat:= TGPStringFormat.Create;
strFormat.SetAlignment(StringAlignmentCenter); {設置左右對齊方式}
strFormat.SetLineAlignment(StringAlignmentCenter); {設置垂直對齊方式}
str := 'wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy';
g.DrawString(str, -1, font, rectF, strFormat, sb);
p:= TGPPen.Create(MakeColor(255, 0, 0, 0));
g.DrawRectangle(p, rectF);
fontFamily.Free;
font.Free;
strFormat.Free;
sb.Free;
p.Free;
g.Free;
end;var
文本對齊方式:
g: TGPGraphics;
str: String;
fontFamily: TGPFontFamily;
font: TGPFont;
rectF: TGPRectF;
strFormat: TGPStringFormat;
sb: TGPSolidBrush;
p: TGPPen;
begin
g := TGPGraphics.Create(Canvas.Handle);
fontFamily:= TGPFontFamily.Create('Arial');
font:= TGPFont.Create(fontFamily, 12, FontStyleBold, UnitPoint);
rectF:= MakeRect(30.0, 10.0, 120.0, 140.0);
sb:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 255));
strFormat:= TGPStringFormat.Create(StringFormatFlagsDirectionRightToLeft);
{使用格式化標志創建文本格式化對象}
str := 'wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy wy';
g.DrawString(str, -1, font, rectF, strFormat, sb);
p:= TGPPen.Create(MakeColor(255, 0, 0, 0));
g.DrawRectangle(p, rectF);
fontFamily.Free;
font.Free;
strFormat.Free;
sb.Free;
p.Free;
g.Free;
end;