• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

How to catch the enter of mouse on certain area of the form?

How can I create a "hot spot" somewhere on the form that would trigger some action whenever the mouse enters this hot spot?
0
JackKuti
Asked:
JackKuti
1 Solution
 
JackKutiAuthor Commented:
Additional note: this hot spot should catch mouse enter even with mouse button being pressed - something, what event OnMouseEnter cannot.
0
 
ZhaawZSoftware DeveloperCommented:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormPaint(Sender: TObject);
  end;

var
  Form1: TForm1;
  hook : cardinal;

implementation

{$R *.dfm}

function MouseHookProc (nCode, wParam, lParam : integer) : integer; stdcall;
type
  TStruct = record
    pt : TPoint;
    wnd : cardinal;
    wHitTestCode : cardinal;
    dwExtraInfo : pointer;
  end;
var
  struct : ^TStruct absolute lParam;
  pnt : TPoint;
begin
if wParam = wm_mousemove then begin
  pnt := struct^.pt;
  windows.ScreenToClient(Form1.Handle, pnt);
  if PtInRect(rect(100, 100, 200, 200), pnt) then with pnt do Form1.Caption := format('x: %d, y: %d', [x, y]);
end;
result := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
hook := SetWindowsHookEx(wh_mouse, @MouseHookProc, hInstance, GetCurrentThreadId);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnhookWindowsHookEx(hook);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Canvas.Rectangle(100, 100, 200, 200);
end;

end.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now