怎么就非要用ToolButton?
1.ImageList在ToolBar中的表现有些NT下和98下不一样;
2.ImageList如果图像做大就不好玩了(如360x100)。
还是看我的吧(其实和Another_eye的方法是一回事)。
unit ImageBtns;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TImageButton = class(TGraphicControl)
private
{ Private declarations }
FGlyph: TBitmap;
FOnMouseEnter : TNotifyEvent;
FOnMouseLeave : TNotifyEvent;
procedure SetGlyph(Value: TBitmap);
procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
protected
{ Protected declarations }
procedure DoMouseEnter; virtual;
procedure DoMouseLeave; virtual;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
published
{ Published declarations }
property Glyph: TBitmap read FGlyph write SetGlyph;
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
property PopupMenu;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
implementation
constructor TImageButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FGlyph := TBitmap.Create;
SetGlyph(nil);
end;
destructor TImageButton.Destroy;
begin
FGlyph.Free;
inherited Destroy;
end;
procedure TImageButton.SetGlyph(Value: TBitmap);
begin
FGlyph.Width := 16;
FGlyph.Height := 16;
if Value <> nil then begin
FGlyph.Assign(Value);
Width := FGlyph.Width div 2;
Height := FGlyph.Height;
end;
Invalidate;
end;
procedure TImageButton.CMMouseEnter(var Msg: TMessage);
begin
DoMouseEnter;
end;
procedure TImageButton.CMMouseLeave(var Msg: TMessage);
begin
DoMouseLeave;
end;
procedure TImageButton.DoMouseEnter;
begin
if Assigned(FOnMouseEnter) then FOnMouseEnter(Self)
else Canvas.CopyRect(ClientRect, FGlyph.Canvas, Rect(Width, 0, Width * 2, Height))
end;
procedure TImageButton.DoMouseLeave;
begin
if Assigned(FOnMouseLeave) then FOnMouseLeave(Self)
else Invalidate;
end;
procedure TImageButton.Paint;
begin
Canvas.CopyRect(ClientRect, FGlyph.Canvas, Rect(0, 0, Width, Height))
end;
procedure Register;
begin
RegisterComponents('Bartons', [TImageButton]);
end;
end.