第一種構建方式: TGPLinearGradIEntBrush.Create(點1, 點2, 顏色1, 顏色2);
本例測試效果圖:
為了方便測試, 在 Unit2 單元從 TShape 繼承了一個 TMyShape 類, 用來模擬一個可活動的顏色點.
其主要功能: 1、可用鼠標左鍵拖動; 2、可用鼠標右鍵改變顏色.
Unit2 的代碼:
unit Unit2;
interface
uses
Classes, Controls, ExtCtrls, Dialogs;
type
TMyShape = class(TShape)
private
fMouseFlag: Boolean;
fx, fy: Integer;
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TMyShape }
constructor TMyShape.Create(AOwner: TComponent);
begin
inherited;
Parent := TWinControl(AOwner);
Width := 9 ;
Height := 9;
Pen.Color := $FFFFFF;
end;
procedure TMyShape.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if Button = mbLeft then
begin
fx := X;
fy := Y;
fMouseFlag := True;
end;
end;
procedure TMyShape.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
inherited;
if fMouseFlag then
begin
Left := Left + X - fx;
Top := Top + Y - fy;
TWinControl(Owner).Repaint;
end;
end;
procedure TMyShape.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
fMouseFlag := False;
if Button = mbRight then
begin
with TColorDialog.Create(Owner) do if Execute then Brush.Color := Color;
end;
TWinControl(Owner).Repaint;
end;
end.
Unit1 代碼:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses GdiPlus, GdiPlusHelpers, Unit2;
var
Shape1, Shape2: TMyShape;
procedure TForm1.FormCreate(Sender: TObject);
begin
Shape1 := TMyShape.Create(Self);
Shape1.Left := 10;
Shape1.Top := 10;
shape1.Brush.Color := clRed;
Shape2 := TMyShape.Create(Self);
Shape2.Left := ClIEntWidth - 10 - Shape2.Width;
Shape2.Top := ClIEntHeight - 10 - Shape2.Height;
Shape2.Brush.Color := clBlue;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
Brush: IGPLinearGradIEntBrush;
Pt1,Pt2: TGPPoint;
Color1, Color2: TGPColor;
Rect: TRect;
begin
Pt1.Initialize(Shape1.Left + Shape1.Width div 2, Shape1.Top + Shape1.Height div 2);
Pt2.Initialize(Shape2.Left + Shape2.Width div 2, Shape2.Top + Shape2.Height div 2);
Color1 := TGPColor.CreateFromColorRef(Shape1.Brush.Color);
Color2 := TGPColor.CreateFromColorRef(Shape2.Brush.Color);
Brush := TGPLinearGradIEntBrush.Create(Pt1, Pt2, Color1, Color2);
Rect := ClIEntRect;
InflateRect(Rect, -ClientWidth div 10, -ClIEntHeight div 10);
Canvas.ToGPGraphics.FillEllipse(Brush, TGPRect.Create(Rect));
end;
end.