如何实现当鼠标在Tspeedbutton上显示一张位图,鼠标离开显示另一个位图(200分)

  • 主题发起人 主题发起人 yyss
  • 开始时间 开始时间
Y

yyss

Unregistered / Unconfirmed
GUEST, unregistred user!
用mousemove能显示但闪烁,不好,其他的好方法谁知道,急啊!
 
结合网上前人代码并适当修改,搞定。
构建一个组件方便使用,源代码如下,方便后来者。
unit MySpeedButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons;

type
TMySpeedButton = class(TSpeedButton)
private
{ Private declarations }
FEnterImage:TBitmap;
FLeaveImage:TBitmap;
procedure cmmouseenter(var msg:tmessage);message CM_MOUSEENTER;
procedure cmmouseleave(var msg:tmessage);message CM_MOUSELEAVE;
Function GetEnterBitmap:TBitmap;
procedure SetEnterBitmap(value:TBitmap);
Function GetLeaveBitmap:TBitmap;
procedure SetLeaveBitmap(value:TBitmap);
protected
{ Protected declarations }
public
{ Public declarations }
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
property EnterImage:TBitmap read GetEnterBitmap write SetEnterBitmap;
property LeaveImage:TBitmap read GetLeaveBitmap write SetLeaveBitmap;
{ Published declarations }
end;

procedure Register;

implementation
var a:integer=0;
procedure Register;
begin

RegisterComponents('ActiveX', [TMySpeedButton]);
end;

{ TMySpeedButton }
procedure TMySpeedButton.cmmouseenter(var msg: tmessage);
begin

if a<1 then
//解决图像闪烁问题
begin
Glyph:=FEnterImage;
end;
a:=a+1;
end;

procedure TMySpeedButton.cmmouseleave(var msg: tmessage);
begin

Glyph:=FLeaveImage;
a:=0;
end;

constructor TMySpeedButton.Create(AOwner: TComponent);
begin

inherited;
FEnterImage:=TBitmap.Create;
FLeaveImage:=TBitmap.Create;
end;

destructor TMySpeedButton.Destroy;
begin

FEnterImage.Free;
FLeaveImage.Free;
inherited;
end;

function TMySpeedButton.GetEnterBitmap: TBitmap;
begin

Result:=FEnterImage;
end;

function TMySpeedButton.GetLeaveBitmap: TBitmap;
begin

Result:=FLeaveImage;
end;

procedure TMySpeedButton.SetEnterBitmap(value: TBitmap);
begin
FEnterImage.Assign(value);
end;

procedure TMySpeedButton.SetLeaveBitmap(value: TBitmap);
begin

FLeaveImage.Assign(value);
end;

end.
 
接受答案了.
 
后退
顶部