472,353 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 9110

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 =...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.