• 再学 GDI+[45]: 文本输出 在矩形中格式化输出


    本例效果图:



    代码文件:
    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 微软 说明
    StringAlignmentCenter  Center  指定文本在布局矩形中居中对齐。
    StringAlignmentFar Far 指定文本远离布局矩形的原点位置对齐。在左到右布局中,远端位置是右。在右到左布局中,远端位置是左。
    StringAlignmentNear Near 指定文本靠近布局对齐。在左到右布局中,近端位置是左。在右到左布局中,近端位置是右。


    文本格式化标志:

    Delphi 微软 说明
    StringFormatFlagsDirectionRightToLeft DirectionRightToLeft 按从右向左的顺序显示文本。
    StringFormatFlagsDirectionVertical DirectionVertical 文本垂直对齐。
    StringFormatFlagsDisplayFormatControl DisplayFormatControl 控制字符(如从左到右标记)随具有代表性的标志符号一起显示在输出中。
    StringFormatFlagsFitBlackBox FitBlackBox 允许部分字符延伸该字符串的布局矩形。默认情况下,将重新定位字符以避免任何延伸。
    StringFormatFlagsLineLimit LineLimit 在格式化的矩形中只布置整行。默认情况下,这种布置要继续到文本的结尾为止,或者到由于剪辑而不再有可见的行为止,看哪一种情况先发生。注意,此默认设置允许不是行高整数倍的格式化矩形将最后一行部分地遮住。若要确保看到的都是整行,请指定此值,并仔细地提供格式化矩形,使其高度至少为一个行高。
    StringFormatFlagsMeasureTrailingSpaces  MeasureTrailingSpaces  包括每一行结尾处的尾随空格。在默认情况下,MeasureString 方法返回的边框都将排除每一行结尾处的空格。设置此标记以便在测定时将空格包括进去。
    StringFormatFlagsNoClip NoClip 允许显示标志符号的伸出部分和延伸到边框外的未换行文本。在默认情况下,延伸到边框外侧的所有文本和标志符号部分都被剪裁。
    StringFormatFlagsNoFontFallback NoFontFallback 对于请求的字体中不支持的字符,禁用回退到可选字体。缺失的任何字符都用缺失标志符号的字体显示,通常是一个空的方块。
    StringFormatFlagsNoWrap NoWrap 在矩形内设置格式时,禁用文本换行功能。当传递的是点而不是矩形时,或者指定的矩形行长为零时,已隐含此标记。


  • 相关阅读:
    DateTimePicker控件在WinXP下的BUG
    SmartAssembly .net混淆后,无法找到部分类型
    RPC_E_SERVERFAULT excel com操作错误
    System.Reflection.ReflectionTypeLoadException
    login SMTP send mail error : Unable to read data from the transport connection: net_io_connectionclosed
    静态类无法实现接口
    Combobox出现System.Data.DataRowView的原因
    .net 4.0 : Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.****'
    EOS 主节点同步
    使用tcpdump抓取EOS帐户创建与交易数据
  • 原文地址:https://www.cnblogs.com/del/p/1224191.html
Copyright © 2020-2023  润新知