unit HotImage;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
THotImage = class(TImage)
private
{ Private declarations }
FMouseInRect: Boolean;
FPictureFileName1: TFileName;
FPictureFileName2: TFileName;
procedure DrawBackground(const FileName: TFileName);
procedure SetPictureFileName1(const Value: TFileName);
procedure SetPictureFileName2(const Value: TFileName);
protected
{ Protected declarations }
procedure CMMouseEnter(var Msg: TMessage); message CM_MouseEnter;
procedure CMMouseLeave(var Msg: TMessage); message CM_MouseLeave;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property PictureFileName1: TFileName read FPictureFileName1 write SetPictureFileName1;
property PictureFileName2: TFileName read FPictureFileName2 write SetPictureFileName2;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Additional', [THotImage]);
end;
{ THotImage }
procedure THotImage.CMMouseEnter(var Msg: TMessage);
begin
if not FMouseInRect then
begin
FMouseInRect:=True;
DrawBackground(FPictureFileName1)
end;
inherited
end;
procedure THotImage.CMMouseLeave(var Msg: TMessage);
begin
if FMouseInRect then
begin
FMouseInRect:=False;
DrawBackground(FPictureFileName2)
end;
inherited
end;
constructor THotImage.Create(AOwner: TComponent);
var
Point: TPoint;
begin
inherited;
GetCursorPos(Point);
FMouseInRect:=PtInRect(ClientRect, Point);
FPictureFileName1:='';
FPictureFileName2:=''
end;
procedure THotImage.DrawBackground(const FileName: TFileName);
begin
try
Picture.LoadFromFile(FileName)
except
end
end;
procedure THotImage.SetPictureFileName1(const Value: TFileName);
begin
if FPictureFileName1<>Value then
begin
FPictureFileName1:=Value;
if FMouseInRect then
DrawBackground(FPictureFileName1)
end
end;
procedure THotImage.SetPictureFileName2(const Value: TFileName);
begin
if FPictureFileName2<>Value then
begin
FPictureFileName2:=Value;
if not FMouseInRect then
DrawBackground(FPictureFileName2)
end
end;
end.
见笑见笑
在设计期图片不会转变,不知道为什么?