• GdiPlus[44]: IGPGraphics (三) 关于文本绘制续 IGPStringFormat



    IGPStringFormat 的方法和属性:

    //方法:
    IGPStringFormat.GetTabStops();                  { 获取制表位 }
    IGPStringFormat.SetTabStops();                  { 设置制表位 }
    IGPStringFormat.SetDigitSubstitution();         { 指定用本地数字替换西方数字时使用的语言和方法 }
    IGPStringFormat.SetMeasurableCharacterRanges(); { 指定 IGPCharacterRanges 结构的数组 }
    
    //属性:
    IGPStringFormat.FormatFlags;                    { 获取或设置 TGPStringFormatFlags 集合 }
    IGPStringFormat.Alignment;                      { 获取或设置水平对齐 }
    IGPStringFormat.LineAlignment;                  { 获取或设置垂直对齐 }
    IGPStringFormat.HotkeyPrefix;                   { 获取或设置 TGPHotkeyPrefix 枚举 }
    IGPStringFormat.DigitSubstitutionLanguage;      { 获取或设置用本地数字替换西方数字时使用的语言 }
    IGPStringFormat.DigitSubstitutionMethod;        { 获取或设置要用于数字替换的方法 }
    IGPStringFormat.Trimming;                       { 获取或设置 TGPStringTrimming 枚举 }
    IGPStringFormat.MeasurableCharacterRangeCount   { 指定的 IGPCharacterRanges 结构中的字符总数 }
    
    //类函数:
    TGPStringFormat.GenericDefault;      { 默认的 TGPStringFormat 对象 }
    TGPStringFormat.GenericTypographic;  { 一般的版式 TGPStringFormat 对象 }
    

    IGPStringFormat.Alignment 测试:



    uses GdiPlus;
    
    procedure TForm1.FormPaint(Sender: TObject);
    const
      str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var
      Graphics: IGPGraphics;
      Brush: IGPSolidBrush;
      Pen: IGPPen;
      Font: IGPFont;
      StringFormat: IGPStringFormat;
      Rect: TGPRectF;
    begin
      Graphics := TGPGraphics.Create(Handle);
      Brush := TGPSolidBrush.Create(TGPColor.Red);
      Pen := TGPPen.Create(TGPColor.Black);
      Font := TGPFont.Create('Arial', 16, [FontStyleBold]);
      StringFormat := TGPStringFormat.Create;
    
      Rect.Initialize(10, 10, 180, 60);
      Graphics.DrawString(str, Font, Rect, nil, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    
      Rect.Y := Rect.Y + 60 + 20;
      StringFormat.Alignment := StringAlignmentCenter;
      Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    
      Rect.Y := Rect.Y + 60 + 20;
      StringFormat.Alignment := StringAlignmentFar;
      Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    end;
    

    IGPStringFormat.LineAlignment 测试:



    uses GdiPlus;
    
    procedure TForm1.FormPaint(Sender: TObject);
    const
      str = 'ABCDEFG';
    var
      Graphics: IGPGraphics;
      Brush: IGPSolidBrush;
      Pen: IGPPen;
      Font: IGPFont;
      StringFormat: IGPStringFormat;
      Rect: TGPRectF;
    begin
      Graphics := TGPGraphics.Create(Handle);
      Brush := TGPSolidBrush.Create(TGPColor.Red);
      Pen := TGPPen.Create(TGPColor.Black);
      Font := TGPFont.Create('Arial', 16, [FontStyleBold]);
      StringFormat := TGPStringFormat.Create;
    
      Rect.Initialize(10, 10, 180, 60);
      Graphics.DrawString(str, Font, Rect, nil, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    
      Rect.Y := Rect.Y + 60 + 20;
      StringFormat.LineAlignment := StringAlignmentCenter;
      Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    
      Rect.Y := Rect.Y + 60 + 20;
      StringFormat.LineAlignment := StringAlignmentFar;
      Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    end;
    

    IGPStringFormat.SetTabStops 测试:



    uses GdiPlus;
    
    procedure TForm1.FormPaint(Sender: TObject);
    const
      Tabs: array [0..2] of Single = (150, 100, 100);
      Str = 'Name'#9'Test 1'#9'Test 2'#9'Test 3'#13#10 +
            'Joe'#9'95'#9'88'#9'91'#13#10 +
            'Mary'#9'98'#9'84'#9'90'#13#10 +
            'Sam'#9'42'#9'76'#9'98'#13#10 +
            'Jane'#9'65'#9'73'#9'92';
    var
      Graphics: IGPGraphics;
      Brush: IGPSolidBrush;
      Pen: IGPPen;
      Font: IGPFont;
      StringFormat: IGPStringFormat;
      Rect: TGPRectF;
    begin
      Graphics := TGPGraphics.Create(Handle);
      Brush := TGPSolidBrush.Create($FF000000);
      Pen := TGPPen.Create($FFFF0000);
      Font := TGPFont.Create('Courier New', 12);
    
      StringFormat := TGPStringFormat.Create;
      StringFormat.SetTabStops(0, [150, 100, 100]);
    //  StringFormat.SetTabStops(50, [100, 100, 100]); //同上
    
      Rect.Initialize(10, 10, 420, 100);
      Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
      Graphics.DrawRectangle(Pen, Rect);
    end;
    

    IGPStringFormat.HotkeyPrefix 测试:



    uses GdiPlus;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var
      Graphics: IGPGraphics;
      Font: IGPFont;
      Pt: TGPPointF;
      Brush: IGPSolidBrush;
      StringFormat: IGPStringFormat;
    begin
      Graphics := TGPGraphics.Create(Handle);
      Font := TGPFont.Create(Canvas.Font.Name, 12);
      Pt.Initialize(10, 10);
      Brush := TGPSolidBrush.Create($FF000000);
    
      StringFormat := TGPStringFormat.Create;
      StringFormat.HotkeyPrefix := HotkeyPrefixShow;
    
      Graphics.DrawString('&Button', Font, Pt, StringFormat, Brush);
    
      Pt.Y := Pt.Y * 2 + Font.GetHeight(Graphics);
      Graphics.DrawString('Butt&on', Font, Pt, StringFormat, Brush);
    end;
    

    IGPStringFormat.Trimming 测试:



    uses GdiPlus;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var
      Graphics: IGPGraphics;
      Font: IGPFont;
      Rect: TGPRectF;
      Brush: IGPSolidBrush;
      StringFormat: IGPStringFormat;
      str: string;
      i: Integer;
    begin
      Graphics := TGPGraphics.Create(Handle);
      Graphics.Clear($FFFFFFFF);
      Brush := TGPSolidBrush.Create($FF000000);
      Font := TGPFont.Create(Canvas.Font.Name, 10);
    
      Rect.Initialize(20, 20, ClientWidth - 40, Font.GetHeight(Graphics));
    
      StringFormat := TGPStringFormat.Create;
      str := Application.ExeName;
      for i := 0 to 5 do
      begin
        StringFormat.Trimming := TGPStringTrimming(i);
        Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
        Graphics.TranslateTransform(0, Rect.Y + Rect.Height);
      end;
    end;
    

    另, 关于 TGPStringFormatFlags (对应 FormatFlags 属性或用于建立参数):
    http://msdn.microsoft.com/zh-cn/library/system.drawing.stringformatflags(VS.80).aspx
  • 相关阅读:
    ASP.NET2.0服务器控件之自定义状态管理
    sql中RIGHT和LEFT(小技巧)
    微软云计算的思考
    枚举转化和判断方法的总结
    sql中的split(小技巧)
    ASP.net实现无扩展名的URL重写。简单、方便、无需ISAPI
    浅析值类型与引用类型的内存分配
    ASP.Net Web Page深入探讨
    const和static readonly 区别
    程序中一种用in的sql防注入的方法(小技巧)
  • 原文地址:https://www.cnblogs.com/del/p/1629987.html
Copyright © 2020-2023  润新知