随笔 - 2146  文章 - 97 评论 - 11761 trackbacks - 253


第一种构建方式: 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.

posted on 2009-12-14 15:43 万一 阅读(...) 评论(...) 编辑 收藏