Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to check if the mouse cursor is outside a TForm Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
28-Oct-02
Category
VCL-Forms
Language
Delphi 2.x
Views
145
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Peter Below

How can I find out if the cursor is leaving a Delphi form?

Answer:

Solve 1:

Add a handler for the CM_MOUSELEAVE message to the form:

1   unit Unit1;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
7   StdCtrls;
8   
9   type
10    TForm1 = class(TForm)
11      Memo1: TMemo;
12    private
13      { Private declarations }
14      procedure CMMouseEnter(var msg: TMessage); message CM_MOUSEENTER;
15      procedure CMMouseLeave(var msg: TMessage); message CM_MOUSELEAVE;
16    public
17      { Public declarations }
18    end;
19  
20  var
21    Form1: TForm1;
22  
23  implementation
24  
25  {$R *.DFM}
26  
27  procedure TForm1.CMMouseEnter(var msg: TMessage);
28  begin
29    if msg.lparam = 0 then
30      memo1.Lines.add('Entered ' + Name)
31    else
32      memo1.Lines.add('Entered ' + TControl(msg.lparam).Name);
33  end;
34  
35  procedure TForm1.CMMouseLeave(var msg: TMessage);
36  begin
37    if msg.lparam = 0 then
38      memo1.Lines.add('Left ' + Name)
39    else
40      memo1.Lines.add('Left ' + TControl(msg.lparam).Name);
41  end;
42  
43  end.



Solve 2:

Place the following code in your form's OnMouseMove event handler, and you'll see 
SetCapture/ ReleaseCapture in action (plus its side-effects):
44  
45  procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
46  begin
47    if (GetCapture < > Handle) then {OnMouseEnter}
48    begin
49      Beep;
50      Caption := 'Hello';
51      SetCapture(Handle);
52    end
53    else if (PtInRect(ClientRect, Point(X, Y))) then {OnMouseOver}
54      Caption := 'X=' + IntToStr(X) + ':Y=' + IntToStr(Y)
55    else {OnMouseOut}
56    begin
57      Beep;
58      Caption := 'Goodbye!';
59      ReleaseCapture;
60    end;
61  end;



Solve 3:

You can use a timer (the smaller the interval the more sensitive the program) and 
in the OnTimer event handler control the mouse position to check if the mouse is 
inside or outside the form:

62  procedure TForm1.Timer1Timer(Sender: TObject);
63  var
64    pt: TPoint;
65  begin
66    GetCursorPos(pt);
67    if (pt.x < Left) or (pt.x > left + Width) then
68      Caption := 'Out'
69    else if (pt.y < Top) or (pt.y > Top + Height) then
70      Caption := 'Out'
71    else
72      Caption := 'In';
73  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC