Graphic Delphi

Title: Hover Image Component
Question: This component is based upon the TImage component, except when your mouse pointer over the image it will change into another image and ofcourse when the mouse pointer leaves the image it will change back in to the original image. It can also change into another image when clicked upon.
Answer:
The ImageX Component
Copyright 2000, Geers Christophe. All rights reserved.
You may use this component freely.
This component is based upon the TImage component, except when your mouse pointer over the image it will change into another image and ofcourse when the mouse pointer leaves the image it will change back in to the original image. It can also change into another image when clicked upon.
unit ImageX;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TImageX = class(TImage)
private
{ Private declarations }
FOnMouseEnter : TNotifyEvent;
FOnMouseLeave : TNotifyEvent;
FOnLMouseButtonDown : TNotifyEvent;
FOnLMouseButtonUp : TNotifyEvent;
FHoverPicture : TPicture;
FHoverClick : TPicture;
OriginalGraphic : TPicture;
OnPicture : Integer;
OnClick : Integer;
FHoverEnabled : Boolean;
FChangeOnClick : Boolean;
procedure SetHoverPicture(Value: TPicture);
procedure SetHoverClick(Value: TPicture);
protected
{ Protected declarations }
procedure MouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure MouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure LMouseButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
procedure LMouseButtonUp(var Message: TMessage); message WM_LBUTTONUP;
procedure DoMouseEnter; virtual;
procedure DoMouseLeave; virtual;
procedure DoLMouseButtonDown; virtual;
procedure DoLMouseButtonUp; virtual;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
published
{ Published declarations }
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
property OnLMouseButtonDown: TNotifyEvent read FOnLMouseButtonDown write FOnLMouseButtonDown;
property OnLMouseButtonUp: TNotifyEvent read FOnLMouseButtonUp write FOnLMouseButtonUp;
property HoverPicture: TPicture read FHoverPicture write SetHoverPicture;
property HoverClick: TPicture read FHoverClick write SetHoverClick;
property HoverEnabled: Boolean read FHoverEnabled write FHoverEnabled default False;
property ChangeOnClick: Boolean read FChangeOnClick write FChangeOnClick default False;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyComponents', [TImageX]);
end;
{ TImageX }
constructor TImageX.Create(AOwner: TComponent);
begin
inherited;
FHoverPicture := TPicture.Create;
OriginalGraphic := TPicture.Create;
FHoverClick := TPicture.Create;
FHoverEnabled := False;
FChangeOnClick := False;
AutoSize := True;
OnPicture := 0;
OnClick := 0;
end;
destructor TImageX.Destroy;
begin
FHoverPicture.Free;
OriginalGraphic.Free;
FHoverClick.Free;
inherited Destroy;
end;
procedure TImageX.DoMouseEnter;
begin
If Assigned(FOnMouseEnter) Then
FOnMouseEnter(Self);
end;
procedure TImageX.DoMouseLeave;
begin
If Assigned(FOnMouseLeave) Then
FOnMouseLeave(Self);
end;
procedure TImageX.DoLMouseButtonDown;
begin
If Assigned(FOnLMouseButtonDown) Then
FOnLMouseButtonDown(Self);
end;
procedure TImageX.DoLMouseButtonUp;
begin
If Assigned(FOnLMouseButtonUp) Then
FOnLMouseButtonUp(Self);
end;
procedure TImageX.LMouseButtonDown(var Message: TMessage);
begin
If csDesigning in ComponentState Then
Exit;
If ((FChangeOnClick = True) and not (FHoverClick.Graphic.Empty)) Then
Begin
Picture.Assign(FHoverClick);
Picture.Graphic.Modified := False;
OnClick := 1;
End;
DoLMouseButtonDown;
inherited;
end;
procedure TImageX.LMouseButtonUp(var Message: TMessage);
begin
If csDesigning in ComponentState Then
Exit;
If ((FChangeOnClick = True) and not (FHoverClick.Graphic.Empty)) Then
Begin
OnClick := 0;
If (FHoverEnabled = False) Then
Begin
Picture.Assign(OriginalGraphic)
End
Else
Begin
If (OnPicture = 1) Then
Picture.Assign(FHoverPicture)
Else
Picture.Assign(OriginalGraphic);
End;
Picture.Graphic.Modified := False;
End;
DoLMouseButtonUp;
inherited;
end;
procedure TImageX.MouseEnter(var Message: TMessage);
begin
If csDesigning in ComponentState Then
Exit;
If (FHoverEnabled = True) Then
Begin
If not FHoverPicture.Graphic.Empty Then
Begin
If OnClick = 0 Then
Picture.Assign(FHoverPicture)
Else
Picture.Assign(FHoverClick);
OnPicture := 1;
End;
Picture.Graphic.Modified := False;
End;
DoMouseEnter;
inherited;
end;
procedure TImageX.MouseLeave(var Message: TMessage);
begin
If csDesigning in ComponentState Then
Exit;
If (FHoverEnabled = True) Then
Begin
If not HoverPicture.Graphic.Empty Then
Begin
OnPicture := 0;
If OnClick = 0 Then
Picture.Assign(OriginalGraphic)
Else
Picture.Assign(FHoverClick);
End;
Picture.Graphic.Modified := False;
End;
DoMouseLeave;
inherited;
end;
procedure TImageX.SetHoverClick(Value: TPicture);
begin
FHoverClick.Assign(Value);
end;
procedure TImageX.SetHoverPicture(Value: TPicture);
begin
FHoverPicture.Assign(Value);
end;
procedure TImageX.Paint;
begin
inherited Paint;
If csDesigning in ComponentState Then
Exit;
If (Picture.Graphic.Modified = True) Then
begin
OriginalGraphic.Assign(Picture);
Picture.Graphic.Modified := False;
end;
end;
end.