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

再學GDI+[42]: 文字字號單位系統 - TUnit 七種度量單位完整指南

     編輯:Delphi

前言

TGPFont 建構子接受 TUnit 參數指定字號單位。GDI+ 支援七種度量單位,從像素到毫米,滿足螢幕顯示、列印輸出、文檔排版等多樣需求。

七大度量單位完整對照

Delphi 常數微軟常數定義典型用途
UnitWorldWorld世界座標單位(自定義)向量圖形、CAD
UnitDisplayDisplay顯示設備單位(螢幕像素/印表機 1/100 吋)裝置無關繪圖
UnitPixelPixel設備像素(預設)螢幕 UI、遊戲
UnitPointPoint印刷點(1/72 吋)傳統排版、CSS pt
UnitInchInch英吋實體尺寸設計
UnitDocumentDocument文檔單位(1/300 吋)高解析列印
UnitMillimeterMillimeter毫米公制設計、工程圖

RadioGroup + TrackBar 即時切換介面

  • RadioGroup:選擇單位(禁用 World/Display)
  • TrackBar:調整字號數值 (1~100)
  • 即時繪製 "Delphi" 文字觀察實際大小變化
procedure TForm1.FormPaint(Sender: TObject);
var
  g: TGPGraphics;
  b: TGPBrush;
  font: TGPFont;
begin
  g := TGPGraphics.Create(Canvas.Handle);
  b := TGPSolidBrush.Create(aclDarkOrange);
  font := TGPFont.Create('Arial Black',
    TrackBar1.Position,
    FontStyleBoldItalic,
    TUnit(RadioGroup1.ItemIndex)  {關鍵:指定字號單位}
  );
  g.DrawString('Delphi', -1, font, MakePoint(5.0, 5), b);
  b.Free; font.Free; g.Free;
end;

執行效果預覽

字號單位切換效果

單位換算關鍵知識

  • 96 DPI 螢幕:1 inch = 96 px,1 pt = 1.333 px,1 mm = 3.78 px
  • 列印輸出:建議使用 Point/Inch/Millimeter 確保實體尺寸正確
  • 螢幕 UI:Pixel 最直觀,但受 DPI 縮放影響
  • World 單位:配合 Graphics.SetTransform 實現任意縮放座標系

表單定義檔

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 147
  ClientWidth = 336
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object RadioGroup1: TRadioGroup ... {右側單位選擇}
  object TrackBar1: TTrackBar ... {底部字號滑桿}
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved