2006. augusztus 23., szerda

How to create a hotspot


Problem/Question/Abstract:

I need something like a hotspot that would lie above everything else and under certain conditions trap mouse events and under other conditions, let them through. I understand, to get the mouse events before anybody else it must be a TWinControl descendant? How do I make it transparent then?

Answer:

Here's one I wrote a couple of years ago. It isn't a TWinControl descendant, TGraphicControl instead, but it handles MouseEnter and MouseLeave. If you want others, you need to add them

{
TEXSHotSpot:
HotSpot Component which allows developers to insert a "HotSpot" on top of an image or panel but lets the background show through and handle mouse entering and leaving the rectangle region of the component.

New Property:
ShowBorder- Use this if you want a border to show around the hotspot when the mouse is over it. Similar to the flat speedbutton look.
}

unit EXSHotSpot;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  extctrls, StdCtrls;

type
  TEXSHotSpot = class(TGraphicControl)
  private
    { Private declarations }
    FMouseInControl: Boolean;
    FShowBorder: Boolean;
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
    procedure DrawBorder;
    function GetShowBorder: Boolean;
    procedure SetShowBorder(Value: Boolean);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property Align;
    property Enabled;
    property OnClick;
    property OnDblClick;
    property OnMouseDown;
    property OnMouseUp;
    property OnMouseMove;
    property OnDragDrop;
    property OnEndDrag;
    property ShowBorder: Boolean read GetShowBorder write SetShowBorder;
    property ShowHint;
    property Visible;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TEXSHotSpot]);
end;

procedure TEXSHotSpot.DrawBorder;
var
  R: TRect;
begin
  R := ClientRect;
  InflateRect(R, -1, -1);
  Frame3D(Canvas, R, clBtnHighlight, clBtnShadow, 1);
end;

procedure TEXSHotSpot.CMMouseEnter(var Message: TMessage);
begin
  inherited;
  if not FMouseInControl and Enabled then
  begin
    FMouseInControl := True;
    if FShowBorder then
      DrawBorder;
  end;
end;

procedure TEXSHotSpot.CMMouseLeave(var Message: TMessage);
begin
  inherited;
  if FMouseInControl and Enabled then
  begin
    FMouseInControl := False;
    Invalidate;
  end;
end;

constructor TEXSHotSpot.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csReplicatable];
  Height := 105;
  Width := 105;
end;

destructor TEXSHotSpot.Destroy;
begin
  inherited Destroy;
end;

function TEXSHotSpot.GetShowBorder: Boolean;
begin
  Result := FShowBorder;
end;

procedure TEXSHotSpot.SetShowBorder(Value: Boolean);
begin
  if Value <> FShowBorder then
    FShowBorder := Value;
  Repaint;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése