IGPMatrix 的屬性與方法:
//方法
IGPMatrix.Reset; { 重置 Matrix 得到單位矩陣(斜對角線是1其他是0) }
IGPMatrix.Translate(); { 平移 }
IGPMatrix.Scale(); { 放縮 }
IGPMatrix.Rotate(); { 旋轉(沿(0,0)點, 順時針) }
IGPMatrix.RotateAt(); { 沿指定點, 順時針旋轉 }
IGPMatrix.Shear(); { 剪切(或叫錯切、斜切) }
IGPMatrix.Invert; { 反轉 }
IGPMatrix.Multiply(); { 與指定的 Matrix 相乘 }
IGPMatrix.TransformPoint(); { 對指定點應用此 Matrix 變換 }
IGPMatrix.TransformPoints(); { 對點數組應用此 Matrix 變換 }
IGPMatrix.TransformVector(); { 對指定點應用此 Matrix 變換中的縮放和旋轉變換 }
IGPMatrix.TransformVectors(); { 對點數組應用此 Matrix 變換中的縮放和旋轉變換 }
IGPMatrix.SetElements(); { 設置 Matrix 各元素 }
IGPMatrix.Clone; { 克隆副本 }
IGPMatrix.Equals(); { 判斷是否相等 }
//屬性
IGPMatrix.Elements; { 獲取 Matrix 各元素構成的數組 }
IGPMatrix.OffsetX; { 獲取 Matrix.Elements.DX }
IGPMatrix.OffsetY; { 獲取 Matrix.Elements.DY }
IGPMatrix.IsInvertible; { 判斷 Matrix 是否可逆轉 }
IGPMatrix.IsIdentity; { 判斷 Matrix 是否是單位矩陣(斜對角線是1其他是0) }
下面的例子用到了其中的 Reset、Translate、Scale、Rotate、Shear、Invert 方法:
代碼:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
GdiPlus;
var
Matrix: IGPMatrix;
//用字符串的形式返回 Matrix 數據, 用於查看
function VIEwMatrix(Matrix: IGPMatrix): string;
const
fmt = '%f'#9'%f'#9'0'#13#10#13#10'%f'#9'%f'#9'0'#13#10#13#10'%f'#9'%f'#9'1';
begin
Result := Format(fmt, [Matrix.Elements.M11,
Matrix.Elements.M12,
Matrix.Elements.M21,
Matrix.Elements.M22,
Matrix.Elements.DX,
Matrix.Elements.DY]);
end;
//初識化
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '重置 (Reset)';
Button2.Caption := '反轉 (Invert)';
Button3.Caption := '平移 (Translate)';
Button4.Caption := '比例 (Scale)';
Button5.Caption := '旋轉 (Rotate)';
Button6.Caption := '錯切 (Shear)';
Matrix := TGPMatrix.Create;
end;
//繪圖
procedure TForm1.FormPaint(Sender: TObject);
var
Image: IGPImage;
Graphics: IGPGraphics;
Rect: TGPRect;
begin
Image := TGPImage.Create('C:\GdiPlusImg\Apple.gif');
Rect.Initialize(20, 20, 80, 80);
Graphics := TGPGraphics.Create(Handle);
Graphics.MultiplyTransform(Matrix);
Graphics.DrawImage(Image, Rect);
Graphics.DrawRectangle(TGPPen.Create($FFFF0000, 2), Rect);
end;
//重置到單位矩陣
procedure TForm1.Button1Click(Sender: TObject);
begin
Matrix.Reset;
Memo1.Text := VIEwMatrix(Matrix);
Repaint;
end;
//反轉變換
procedure TForm1.Button2Click(Sender: TObject);
begin
Matrix.Invert;
Memo1.Text := VIEwMatrix(Matrix);
Repaint;
end;
//平移變換
procedure TForm1.Button3Click(Sender: TObject);
begin
Matrix.Translate(1.5, 3);
Memo1.Text := VIEwMatrix(Matrix);
Repaint;
end;
//比例變換
procedure TForm1.Button4Click(Sender: TObject);
begin
Matrix.Scale(0.75, 1.5);
Memo1.Text := VIEwMatrix(Matrix);
Repaint;
end;
//旋轉變換
procedure TForm1.Button5Click(Sender: TObject);
begin
Matrix.Rotate(15);
Memo1.Text := VIEwMatrix(Matrix);
Repaint;
end;
//錯切變換
procedure TForm1.Button6Click(Sender: TObject);
begin
Matrix.Shear(0.2, 0.5);
Memo1.Text := VIEwMatrix(Matrix);
Repaint;
end;
end.
窗體:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 200
ClIEntWidth = 377
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 185
Top = 8
Width = 90
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 281
Top = 8
Width = 90
Height = 25
Caption = 'Button2'
TabOrder = 1
OnClick = Button2Click
end
object Button3: TButton
Left = 185
Top = 39
Width = 90
Height = 25
Caption = 'Button3'
TabOrder = 2
OnClick = Button3Click
end
object Button4: TButton
Left = 281
Top = 39
Width = 90
Height = 25
Caption = 'Button4'
TabOrder = 3
OnClick = Button4Click
end
object Button5: TButton
Left = 185
Top = 70
Width = 90
Height = 25
Caption = 'Button5'
TabOrder = 4
OnClick = Button5Click
end
object Memo1: TMemo
Left = 185
Top = 111
Width = 181
Height = 81
Lines.Strings = (
'Memo1')
TabOrder = 5
end
object Button6: TButton
Left = 281
Top = 70
Width = 90
Height = 25
Caption = 'Button6'
TabOrder = 6
OnClick = Button6Click
end
end