快捷搜索:

再学GDI+[56]: 路径 - Warp

经由过程路径的 Warp 措施可以让路径在一个范围内(四个点抉择的范围)变换;

第一个参数可所以 3 个点或 4 个点的数组, 假如是 3 个的数组, 那么第 4 个点将自动跟 随一个平行四边形;

第二个参数用来指定点数组的个数, 本例便是经由过程这个参数节制了点的个数;

第三个参数是一个矩形, 这个矩形应该是路径的外接矩形, 之后的参数都是可选的;

第四个参数也是一个矩阵变换, 起因专门的 例子 , 这里没有考试测验;

第五个参数是 TWarpMode(WarpModePerspective, WarpModeBilinear) , 默认前者;

第六个参数可以指定 Flatten 变换, 前面有 例子 , 这里也没有考试测验.

本例效果图:

代码文件:unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, ComCtrls, StdCtrls;

type

TForm1 = class(TForm)

CheckBox1: TCheckBox;

procedure FormCreate(Sender: TObject);

procedure FormPaint(Sender: TObject);

procedure CheckBox1Click(Sender: TObject);

procedure FormMouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure FormMouseUp(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

uses GDIPOBJ, GDIPAPI;

var

ptfs: array[0..3] of TGPPointF;

ptCount: Integer = 4;

pti: Integer;

R: TRect;

flag: Boolean;

procedure TForm1.FormCreate(Sender: TObject);

begin

CheckBox1.Caption := '只用 3 个点节制';

R := Rect(20, 20, ClientWidth-20, ClientHeight - 40);

ptfs[0] := MakePoint(R.Left*1.0, R.Top);

ptfs[1] := MakePoint(R.Right*1.0, R.Top);

ptfs[2] := MakePoint(R.Left*1.0, R.Bottom);

ptfs[3] := MakePoint(R.Right*1.0, R.Bottom);

end;

procedure TForm1.FormPaint(Sender: TObject);

var

g: TGPGraphics;

p: TGPPen;

b: TGPBrush;

path: TGPGraphicsPath;

fontFamily: TGPFontFamily;

StringFormat: TGPStringFormat;

rectf: TGPRectF;

i: Integer;

begin

g := TGPGraphics.Create(Canvas.Handle);

p := TGPPen.Create(aclRed, 1);

b := TGPHatchBrush.Create(HatchStyleShingle, aclDarkCyan);

path := TGPGraphicsPath.Create;

path.AddEllipse(MakeRect(R));

fontFamily := TGPFontFamily.Create('Arial Black');

StringFormat := TGPStringFormat.Create;

StringFormat.SetAlignment(StringAlignmentCenter);

StringFormat.SetLineAlignment(StringAlignmentCenter);

path.GetBounds(rectf);

path.AddString('Delphi', -1, fontFamily, FontStyleRegular, 48, rectf, StringFormat);

path.Warp(PGPPointF(@ptfs), Length(ptfs), rectf);

g.FillPath(b, path);

for i := 0 to ptCount - 1 do

g.DrawRectangle(p, MakeRect(ptfs[i].X-3, ptfs[i].Y-3, 6, 6));

StringFormat.Free;

fontFamily.Free;

path.Free;

b.Free;

p.Free;

g.Free;

end;

procedure TForm1.CheckBox1Click(Sender: TObject);

begin

ptCount := 4;

if CheckBox1.Checked then ptCount := 3;

Repaint;

end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

var

i: Integer;

begin

for i := 0 to 3 do

if PtInRect(Bounds(Trunc(ptfs[i].X-3), Trunc(ptfs[i].Y-3), 6, 6), Point(X, Y)) then

begin

pti := i;

flag := True;

Exit;

end;

end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,

Y: Integer);

begin

if flag then

begin

ptfs[pti].X := X;

ptfs[pti].Y := Y;

Repaint;

end;

end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

flag := False;

end;

end.

您可能还会对下面的文章感兴趣: