Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting mouse outside a form

1,094 views
Skip to first unread message

"philippe_chessa"_pchessa[at]peaktime.fr

unread,
Jan 14, 2004, 4:45:32 AM1/14/04
to

Hi everybody,

I am using a TForm to display large strings (like a hint but resizable).
I want to make it very easy to dismiss: no button to click, no key to press. For that, mu idea is to detect that the mouse cursor is outside the form.
Is there a way to detect this event?

Thanks in advance

Philippe

Jeremy Collins

unread,
Jan 14, 2004, 6:39:35 AM1/14/04
to

You can implement a CM_MOUSELEAVE message handler...

--
jc

Remove the -not from email

"philippe_chessa"_pchessa[at]peaktime.fr

unread,
Jan 14, 2004, 7:42:50 AM1/14/04
to

Thanks Jeremy,

This event fires when the mouse leaves from one control of the form to an other, but not when the mouse leaves the main form.

Kursat

unread,
Jan 14, 2004, 9:30:52 AM1/14/04
to
You can use a timer (the smaller the interval the sensitive the program) and
in the OnTimer event handler you can control the mouse position to decide
the mouse is whether inside or outside the form :

procedure TForm1.Timer1Timer(Sender: TObject);
var
pt : TPoint;
begin
GetCursorPos (pt);
if (pt.x < Left) or (pt.x > left + Width) then
Caption := 'Out'
else if (pt.y < Top) or (pt.y > Top + Height) then
Caption := 'Out'
else
Caption := 'In';
end;


David Knaack

unread,
Jan 14, 2004, 11:06:11 AM1/14/04
to
Kursat wrote:
> You can use a timer (the smaller the interval the sensitive the program) and
> in the OnTimer event handler you can control the mouse position to decide
> the mouse is whether inside or outside the form :

I use a similar method, but I usually expand the test area a bit beyond
the borders of the window in case the user is aiming the mouse for
scrollbars or other items near the edges of the window and overshoots.

DK

Kursat

unread,
Jan 14, 2004, 11:15:22 AM1/14/04
to
> I use a similar method, but I usually expand the test area a bit beyond
> the borders of the window in case the user is aiming the mouse for
> scrollbars or other items near the edges of the window and overshoots.

You are right David. Original poster may expand the logic for special cases
(such as non rectangular forms)


Rob Kennedy

unread,
Jan 14, 2004, 11:47:25 AM1/14/04
to
"Philippe Chessa" pchessa[at]peaktime.fr wrote:
> This event fires when the mouse leaves from one control of the form
> to an other, but not when the mouse leaves the main form.

So use the wm_MouseLeave message instead. You'll have to ask Windows to
send you that message when appropriate. Call TrackMouseEvent for that.

--
Rob

Brett Watters

unread,
Jan 14, 2004, 3:44:19 PM1/14/04
to
Philippe,

Look at the SetCapture and ReleaseCapture API functions.

Thanks,

Brett

<Philippe Chessa pchessa[at]peaktime.fr> wrote in message
news:40051dcc$1...@newsgroups.borland.com...

Uwe

unread,
Jan 14, 2004, 4:10:12 PM1/14/04
to
Hi Philippe

Check out the Delphi Pool at http://www.lmc-mediaagentur.de/dpool.htm

Under Delphi -> Tips & Tricks -> User Interface -> TForm -> 0013 you will
find what you're looking for.

Cheers

Uwe
The Delphi Pool


<Philippe Chessa pchessa[at]peaktime.fr> wrote in message
news:40051dcc$1...@newsgroups.borland.com...
>

Rob Kennedy

unread,
Jan 14, 2004, 4:46:12 PM1/14/04
to
Uwe wrote:
> Check out the Delphi Pool at http://www.lmc-mediaagentur.de/dpool.htm
>
> Under Delphi -> Tips & Tricks -> User Interface -> TForm -> 0013 you will
> find what you're looking for.

The method that uses cm_MouseLeave won't work if you move the mouse too
fast. The method that sets the mouse capture can make other controls on
the form unresponsive since the form steals all the mouse events.

--
Rob

Uwe

unread,
Jan 14, 2004, 5:29:25 PM1/14/04
to
Hi Rob

This might well be. I didn't test the code and I would only see it as an
alternative to the solution Kursat provided. But thanks for pointing out the
potential drawbacks.

Cheers
Uwe

0 new messages