473,473 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Figure out when mouse cursor enters/leaves form

Hello,

I have tried this using the MouseEnter/MouseLeave events. However these
events do not really refer to the rectangular shape of the form, but
the client area (form area minus children areas). This means that if
the mouse is currently inside the form's client area and it enters a
child, a MouseLeave event will be generated on the form.

One little trick to overcome this is to do a point-in-rectangle test
inside the MouseLeave handler:

if (!Bounds.Contains(MousePosition))
{
// mouse really left the form
}
This however doesn't always work, for example if you have another
window on top of yours, because that window's area will be treated just
like a child area.

Anyone knows how to do this cleanly?

Thanks in advance,
Cosmin.

Oct 16 '06 #1
4 9222

cb******@gmail.com wrote:
Hello,

I have tried this using the MouseEnter/MouseLeave events. However these
events do not really refer to the rectangular shape of the form, but
the client area (form area minus children areas). This means that if
the mouse is currently inside the form's client area and it enters a
child, a MouseLeave event will be generated on the form.

One little trick to overcome this is to do a point-in-rectangle test
inside the MouseLeave handler:

if (!Bounds.Contains(MousePosition))
{
// mouse really left the form
}
This however doesn't always work, for example if you have another
window on top of yours, because that window's area will be treated just
like a child area.

Anyone knows how to do this cleanly?

Thanks in advance,
Cosmin.
You could iterate through the control tree and add MouseEnter and
MouseLeave events to every single control. Then do the same for every
single ControlAdded and ControlRemoved, adding/removing all four event
bindings to every new control added anywhere in the tree. Just recurse
through the control.Controls collection.

Thus, you know which control the mouse is pointing at at any given
time.... the only worry is that I don't know what order things happen
in - when a mouse transitions from one control to the next, if it
"Leaves" control X before it "enters" control Y, there will be a
momentary "gap" where your app thinks it isn't pointing at the form.

If "Leave" happens first, then I guess you just use a hack with a
timer. The timer runs as fast as is reasonable for good UI feedback -
when, in two sequential cycles, the mouse is outside of all controls,
then you accept that it has left the form. If it ever inside of the
set of all controls, then it is still inside the form. Hacky, but it
should work.

Oct 16 '06 #2
Seems a bit overkill just just for this... :|

Martin Z wrote:
cb******@gmail.com wrote:
Hello,

I have tried this using the MouseEnter/MouseLeave events. However these
events do not really refer to the rectangular shape of the form, but
the client area (form area minus children areas). This means that if
the mouse is currently inside the form's client area and it enters a
child, a MouseLeave event will be generated on the form.

One little trick to overcome this is to do a point-in-rectangle test
inside the MouseLeave handler:

if (!Bounds.Contains(MousePosition))
{
// mouse really left the form
}
This however doesn't always work, for example if you have another
window on top of yours, because that window's area will be treated just
like a child area.

Anyone knows how to do this cleanly?

Thanks in advance,
Cosmin.

You could iterate through the control tree and add MouseEnter and
MouseLeave events to every single control. Then do the same for every
single ControlAdded and ControlRemoved, adding/removing all four event
bindings to every new control added anywhere in the tree. Just recurse
through the control.Controls collection.

Thus, you know which control the mouse is pointing at at any given
time.... the only worry is that I don't know what order things happen
in - when a mouse transitions from one control to the next, if it
"Leaves" control X before it "enters" control Y, there will be a
momentary "gap" where your app thinks it isn't pointing at the form.

If "Leave" happens first, then I guess you just use a hack with a
timer. The timer runs as fast as is reasonable for good UI feedback -
when, in two sequential cycles, the mouse is outside of all controls,
then you accept that it has left the form. If it ever inside of the
set of all controls, then it is still inside the form. Hacky, but it
should work.
Oct 16 '06 #3
Why? It might prove to be a performance killer - testing will show
that. But semantically, it's simple. Code one event-binding function
that sets up the events and applies itself to all children - then make
the reverse. This pair is what is called by the ControlAdded and
ControlRemoved handlers. Then you just have the MouseEnter write to a
shared "CurrentMouseControl" member - and the MouseLeave checks to make
sure that it's not leaving the "CurrentMouseControl" member (assuming
the MouseLeave-after-MouseEnter behaviour that I'm hoping for).

The complex, wasteful, overkill solution is only needed in the case
that Leave happens first.

cb******@gmail.com wrote:
Seems a bit overkill just just for this... :|

Martin Z wrote:
cb******@gmail.com wrote:
Hello,
>
I have tried this using the MouseEnter/MouseLeave events. However these
events do not really refer to the rectangular shape of the form, but
the client area (form area minus children areas). This means that if
the mouse is currently inside the form's client area and it enters a
child, a MouseLeave event will be generated on the form.
>
One little trick to overcome this is to do a point-in-rectangle test
inside the MouseLeave handler:
>
if (!Bounds.Contains(MousePosition))
{
// mouse really left the form
}
>
>
This however doesn't always work, for example if you have another
window on top of yours, because that window's area will be treated just
like a child area.
>
Anyone knows how to do this cleanly?
>
Thanks in advance,
Cosmin.
You could iterate through the control tree and add MouseEnter and
MouseLeave events to every single control. Then do the same for every
single ControlAdded and ControlRemoved, adding/removing all four event
bindings to every new control added anywhere in the tree. Just recurse
through the control.Controls collection.

Thus, you know which control the mouse is pointing at at any given
time.... the only worry is that I don't know what order things happen
in - when a mouse transitions from one control to the next, if it
"Leaves" control X before it "enters" control Y, there will be a
momentary "gap" where your app thinks it isn't pointing at the form.

If "Leave" happens first, then I guess you just use a hack with a
timer. The timer runs as fast as is reasonable for good UI feedback -
when, in two sequential cycles, the mouse is outside of all controls,
then you accept that it has left the form. If it ever inside of the
set of all controls, then it is still inside the form. Hacky, but it
should work.
Oct 16 '06 #4
To hook up the event handlers try something like

SetEventHandlers(this, new EventHandler(Form1_MouseEnter), new
EventHandler(Form1_MouseLeave));
..
..
..
private void SetEventHandlers(Control control, EventHandler mouseEnter,
EventHandler mouseLeave)
{
control.MouseEnter += mouseEnter;
control.MouseLeave += mouseLeave;

foreach (Control ctl in control.Controls)
{
SetEventHandlers(ctl, mouseEnter, mouseLeave);
}
}

Also, to check the non-client area (and the client area if you wish) you
could override WndProc.

// From WinUser.h
private const int WM_NCHITTEST = 0x0084;
private const int WM_MOUSEMOVE = 0x0200;
private const int WM_MOUSELEAVE = 0x02A3;
private const int WM_NCMOUSEMOVE = 0x00A0;
private const int WM_NCMOUSELEAVE = 0x02A2;

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

switch (m.Msg)
{
case WM_NCMOUSELEAVE:
// Your code here
break;
Oct 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Maria | last post by:
Hello ! I try to change the cursor on the form like this : Me.Cursor = Cursors.WaitCursor 'reading from database 'showing file Me.Cursor = Cursors.Arrow
3
by: jcrouse | last post by:
I have created a form designer type application (with a lot of you peoples helpJ). It has label controls that are draggable at runtime. The user is also allowed to change some properties such as...
2
by: Jim Frazer | last post by:
Hi, I'm working on an application in C# that will allow the user to create simple CAD drawings on a CEPC system. I would like to be able to change the cursor shape depending on the drawing mode...
1
by: wanwan | last post by:
I'm trying to get the cursor changed to a hand whenever it stops in the form, and have it changed back to default whenever the mouse moves. So the common sense is to use the mousehover and...
6
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I want to implement the following: If the user clicks on the border of a form, then I want to show a box around the form that represents the form's bounds. As the user moves the mouse only the...
1
Ericks
by: Ericks | last post by:
This must be a piece of cake and I'm sure it has been addressed somewhere in this forum. I have a form with a control that when clicked on opens another form. So I want the cursor to change to a...
4
by: mike | last post by:
I have the opportunity to rescue a project that uses a mouse to sense the relative position of a machine. The hardware is built...just needs to be programmed. Stop snickering!!! I didn't do it...I...
6
by: c.k. | last post by:
I was wondering if there was a way to give mouse movement on an empty form some lag/delay? For instance, user moves mouse, and the cursor is 20ms (possible adjustable from some option) behind the...
0
by: Frank Rizzo | last post by:
I have a form with a lot of controls on it. How can I detect when the mouse leaves the form? I've tried wiring up a MouseLeave event for every single control, but that does not work because those...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.