程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 再學GDI+[45]: 文本輸出 - 在矩形中格式化輸出 <2>

再學GDI+[45]: 文本輸出 - 在矩形中格式化輸出 <2>

編輯:Delphi

本例效果圖:

代碼文件: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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved