Jeg havde engang brug for det samme og lavede et imagebutton component med mouseover og mousedown, det er rimelig simpelt at lave. Da det var det første komponent jeg lavede er koden ikke skide flot, men du kan nok bruge det til at lave en forbedret version.
unit ImageButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TOnMouseEvent = procedure( Msg: TWMMouse ) of object;
TImageButton = class(TImage)
private
bNormal: boolean;
bOver: boolean;
bDown: boolean;
bOnMouseEnter: TOnMouseEvent;
bOnMouseLeave: TOnMouseEvent;
bOnMouseDown: TOnMouseEvent;
bOnMouseUp: TOnMouseEvent;
pPic: TPicture;
pPicOver: TPicture;
pPicDown: TPicture;
procedure AssignPicture;
procedure ImageChange(Sender : TObject);
protected
procedure WMMouseEnter(var Msg: TWMMouse); message CM_MOUSEENTER;
procedure WMMouseLeave(var Msg: TWMMouse); message CM_MOUSELEAVE;
procedure WMLButtonUp(var Msg: TWMLButtonUp); message WM_LBUTTONUP;
procedure WMLButtonDown(var Msg: TWMLButtonUp); message WM_LBUTTONDOWN;
procedure SetPic(value: TPicture);
procedure SetPicOver(value: TPicture);
procedure SetPicDown(value: TPicture);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property PictureNormal: TPicture read pPic write SetPic;
property PictureOver: TPicture read pPicOver write SetPicOver;
property PictureDown: TPicture read pPicDown write SetPicDown;
property OnMouseEnter: TOnMouseEvent read bOnMouseEnter write bOnMouseEnter;
property OnMouseLeave: TOnMouseEvent read bOnMouseLeave write bOnMouseLeave;
property OnMouseDown: TOnMouseEvent read bOnMouseDown write bOnMouseDown;
property OnMouseUp: TOnMouseEvent read bOnMouseUp write bOnMouseUp;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Decko', [TImageButton]);
end;
constructor TImageButton.Create;
begin
inherited;
pPic:= TPicture.Create;
pPic.OnChange := ImageChange;
pPicOver:= TPicture.Create;
pPic.OnChange := ImageChange;
pPicDown:= TPicture.Create;
pPic.OnChange := ImageChange;
bNormal:= true;
bOver:= false;
bDown:= false;
AssignPicture;
end;
destructor TImageButton.Destroy;
begin
pPic.Free;
pPicOver.Free;
pPicDown.Free;
inherited;
end;
procedure TImageButton.WMMouseEnter(var Msg: TWMMouse);
begin
bNormal:= false;
bOver:= true;
bDown:= false;
AssignPicture;
if Assigned(bOnMouseEnter) then bOnMouseEnter(Msg);
end;
procedure TImageButton.WMMouseLeave(var Msg: TWMMouse);
begin
bNormal:= true;
bOver:= false;
bDown:= false;
AssignPicture;
if Assigned(bOnMouseLeave) then bOnMouseLeave(Msg);
end;
procedure TImageButton.WMLButtonDown(var Msg: TWMMouse);
begin
inherited;
bDown:= true;
bNormal:= false;
bOver:= true;
AssignPicture;
if Assigned(bOnMouseDown) then bOnMouseDown(Msg);
end;
procedure TImageButton.WMLButtonUp(var Msg: TWMMouse);
begin
inherited;
bNormal:= false;
bOver:= true;
bDown:= false;
AssignPicture;
if Assigned(bOnMouseUp) then bOnMouseUp(Msg);
end;
procedure TImageButton.SetPic(Value: TPicture);
begin
pPic.Assign(Value);
AssignPicture;
end;
procedure TImageButton.SetPicOver(Value: TPicture);
begin
pPicOver.Assign(Value);
AssignPicture;
end;
procedure TImageButton.SetPicDown(Value: TPicture);
begin
pPicDown.Assign(Value);
AssignPicture;
end;
procedure TImageButton.AssignPicture;
begin
if bNormal then
Picture.Assign(pPic);
if bOver then
Picture.Assign(pPicOver);
if bDown then
Picture.Assign(pPicDown);
Invalidate;
end;
procedure TImageButton.ImageChange(Sender: TObject);
begin
AssignPicture;
end;
end.
Hehe, kan godt lide du har WebCoder kørende i baggrunden til at kigge efter
.