unit CsImage;
interface
uses
SysUtils, Classes, Controls, ExtCtrls, Graphics;
type
{ -------------------------------------------------------------------------- }
{ TCsImage ----------------------------------------------------------------- }
{ -------------------------------------------------------------------------- }
TCsImage =
class(TImage)
private
FPicture: TPicture;
FHoverPicture: TPicture;
FDisabledPicture: TPicture;
FOnMouseEnter: TNotifyEvent;
FOnMouseLeave: TNotifyEvent;
FDrawHovered: Boolean;
protected
procedure SetPicture(Value: TPicture);
procedure SetHoverPicture(Value: TPicture);
procedure SetDisabledPicture(Value: TPicture);
function GetEnabled: Boolean;
override;
procedure SetEnabled(Value: Boolean);
override;
procedure HandleMouseEnter(Sender: TObject);
procedure HandleMouseLeave(Sender: TObject);
procedure RefreshPicture;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure Loaded;
override;
published
property Picture: TPicture
read FPicture
write SetPicture;
property DisabledPicture: TPicture
read FDisabledPicture
write SetDisabledPicture;
property Enabled: Boolean
read GetEnabled
write SetEnabled;
property HoverPicture: TPicture
read FHoverPicture
write SetHoverPicture;
property OnMouseEnter: TNotifyEvent
read FOnMouseEnter
write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent
read FOnMouseLeave
write FOnMouseLeave;
end;
implementation
{ ---------------------------------------------------------------------------- }
{ TCsImage ------------------------------------------------------------------- }
{ ---------------------------------------------------------------------------- }
constructor TCsImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPicture := TPicture.Create;
FHoverPicture := TPicture.Create;
FDisabledPicture := TPicture.Create;
inherited OnMouseEnter := HandleMouseEnter;
inherited OnMouseLeave := HandleMouseLeave;
end;
destructor TCsImage.Destroy;
begin
FreeAndNil(FPicture);
FreeAndNil(FHoverPicture);
FreeAndNil(FDisabledPicture);
inherited Destroy;
end;
procedure TCsImage.Loaded;
begin
inherited Loaded;
{if not (csDesigning in ComponentState) then
SetPicture(FPicture);}
end;
procedure TCsImage.SetPicture(Value: TPicture);
begin
FPicture.Assign(Value);
RefreshPicture;
end;
procedure TCsImage.SetHoverPicture(Value: TPicture);
begin
FHoverPicture.Assign(Value);
end;
function TCsImage.GetEnabled: Boolean;
begin
Result :=
inherited Enabled;
end;
procedure TCsImage.SetEnabled(Value: Boolean);
begin
inherited SetEnabled(Value);
RefreshPicture;
end;
procedure TCsImage.SetDisabledPicture(Value: TPicture);
begin
FDisabledPicture.Assign(Value);
RefreshPicture;
end;
procedure TCsImage.HandleMouseEnter(Sender: TObject);
begin
FDrawHovered := True;
RefreshPicture;
if Assigned(FOnMouseEnter)
then
OnMouseEnter(Sender);
end;
procedure TCsImage.HandleMouseLeave(Sender: TObject);
begin
FDrawHovered := False;
RefreshPicture;
if Assigned(FOnMouseLeave)
then
OnMouseLeave(Sender);
end;
procedure TCsImage.RefreshPicture;
begin
if (
not Enabled)
and Assigned(FDisabledPicture)
then
inherited Picture.Assign(FDisabledPicture)
else if FDrawHovered
and Assigned(FHoverPicture)
then
inherited Picture.Assign(FHoverPicture)
else
inherited Picture.Assign(FPicture);
end;
end.