我也接触这方面不久,觉得如下就可以符合你的要求啦/!要有其他的要求,再补充!
{
初始:蓝色;
鼠标左键按下:红色;
鼠标放开:黑色;
鼠标移开:蓝色;
}
unit ColorBtn;
interface
uses Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,
Graphics,Dialogs;
type
TColorBtn = class(TGraphicControl)
private
OnMDown: TMouseEvent;
OnMUp: TMouseEvent;
protected
procedure Paint;override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);override;
procedure MouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure Click;override;
public
constructor Create(AOwner: TComponent);override;
published
property OnMouseDown: TMouseEvent read OnMDown write OnMDown;
property OnMouseUp: TMouseEvent read OnMUp write OnMup;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyTest', [TColorBtn]);
end;
{ TColorBtn }
procedure TColorBtn.Click;
begin
inherited;
end;
constructor TColorBtn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 30;
Height := 30;
end;
procedure TColorBtn.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseDown(Button, Shift, x, y);
canvas.Brush.Color := clRed;
canvas.FillRect(Rect(0,0,30,30));
end;
procedure TColorBtn.MouseLeave(var Message: TMessage);
begin
Paint;
end;
procedure TColorBtn.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseUp(Button, Shift, x, y);
canvas.Brush.Color := clBlack;
canvas.FillRect(Rect(0,0,30,30));
end;
procedure TColorBtn.Paint;
begin
inherited Paint;
canvas.Brush.Color := clBlue;
canvas.FillRect(Rect(0,0,30,30));
end;
end.
哈哈,是不是觉得太简单了啊...