首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

再學GDI+[45]: 矩形區域文字格式化輸出(二) - StringFormat 旗標與對齊完整控制

     編輯:Delphi

前言

TGPStringFormat 是 GDI+ 文字排版的核心控制類別。本文延續上篇,深入探討格式旗標與雙軸對齊的組合效果,提供三組 RadioGroup 即時切換:水平對齊、垂直對齊、格式旗標。

雙軸對齊枚舉

Delphi微軟水平含義垂直含義
StringAlignmentNearNear靠左 / 靠近原點靠上 / 靠近原點
StringAlignmentCenterCenter置中置中
StringAlignmentFarFar靠右 / 遠離原點靠下 / 遠離原點

九大格式旗標完整對照

Delphi 常數微軟常數功能說明
StringFormatFlagsDirectionRightToLeftDirectionRightToLeft右到左閱讀順序(阿拉伯文/希伯來文)
StringFormatFlagsDirectionVerticalDirectionVertical垂直排版(日文/中文直排)
StringFormatFlagsNoFitBlackBoxNoFitBlackBox允許字符超出版面矩形(預設裁剪)
StringFormatFlagsDisplayFormatControlDisplayFormatControl顯示控制字符(如 LRM/RLM 標記)
StringFormatFlagsNoFontFallbackNoFontFallback缺字不回退替換字體,顯示缺字符號
StringFormatFlagsMeasureTrailingSpacesMeasureTrailingSpaces測量包含行尾空格
StringFormatFlagsNoWrapNoWrap禁用自動換行(單行模式)
StringFormatFlagsLineLimitLineLimit僅顯示完整行,截斷部分行
StringFormatFlagsNoClipNoClip允許超出版面矩形繪製(預設裁剪)

三組 RadioGroup 互動介面

  • RadioGroup1:水平對齊
  • RadioGroup2:垂直對齊
  • RadioGroup3:格式旗標(單選一項)
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);  {RadioGroup3 選中的旗標}
  rect := MakeRect(0, 0, PaintBox1.ClientWidth, PaintBox1.ClientHeight);
  g.DrawString('Delphi', -1, font, rect, sf, b);
  sf.Free; font.Free; b.Free; g.Free;
end;

執行效果預覽

文字格式化對齊旗標效果

旗標組合實戰建議

  • 單行截斷:NoWrap + LineLimit + NoClip
  • 垂直排版:DirectionVertical + Center/Center
  • 右到左語言:DirectionRightToLeft + Far/Near
  • 像素精確對齊:NoFitBlackBox + 關閉抗鋸齒
  • 預設安全組合:NoClip(避免字符被意外裁剪)

表單定義檔

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 ... {繪圖區}
  object RadioGroup1: TRadioGroup ... {水平對齊}
  object RadioGroup2: TRadioGroup ... {垂直對齊}
  object RadioGroup3: TRadioGroup ... {9 項旗標}
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved