Mouse leave for windows form which has some controls not firing asdesired

  • Thread starter Thread starter vidishasharma
  • Start date Start date
V

vidishasharma

Hi,

I have a windows from with few controls over it. Now I have to see if
the cursor goes outside the bounds of the windows form then I have to
close the form.

I added mouse leave for the form however as soon as the cursor goes on
any control over the form form_ MouseLeave is fired.

I want to this to be fired only if it goes outside the bounds of the
form.

How to I control this. is there any way to achinve this.
 
Hi,

I have a windows from with few controls over it. Now I have to see if
the cursor goes outside the bounds of the windows form then I have to
close the form.

I added mouse leave for the form however as soon as the cursor goes on
any control over the form form_ MouseLeave is fired.

I want to this to be fired only if it goes outside the bounds of the
form.

How to I control this. is there any way to achinve this.

You could just check in your MouseLeave whether the mouse is still within
the form or not, e.g.:

private void Form1_MouseLeave(object sender, EventArgs e)
{
Point ptCursor = Cursor.Position;
ptCursor = PointToClient(ptCursor);
if (!ClientRectangle.Contains(ptCursor))
{
// Mouse has really left form
}
}

One problem is that you'll close the form if you move the mouse to the
non-client area (border, title bar, etc.) which may or may not be what you
want - but since the MouseLeave event fires when the mouse leaves the client
area I don't see a way round this.

Chris Jobson
 
I tried this however however ClientRectangle has always x=0, and y=0
therefore this is not working correctly.

Do I have to write something else.

I tried this.bounds.contains(ptCursor), however that is also giving
wrong behaviour
 
I tried this however however ClientRectangle has always x=0, and y=0
therefore this is not working correctly.

Do I have to write something else.

I tried this.bounds.contains(ptCursor), however that is also giving
wrong behaviour

The code I posted works fine for me. Did you remember to include the line
ptCursor = PointToClient(ptCursor);
to convert the cursor position to client coordinates?

I think that the reason why Bounds.Contains(ptCursor) won't work is that as
you move the mouse out of your client area onto the window border the
Mouseleave event fires. At this time the mouse is still within the Bounds
(which include the border) so the test for being outside the window fails.
When the mouse subsequently moves off the border to outside the window you
don't get another event because the mouse has already left the client area.

Some other things you could try are:
- Use the Win32 API function TrackMouseEvent and intercept the
WM_NCMOUSELEAVE message.
- Install a LowLevelMouseProc hook.

Although I know how to do both of these from a pure Win32 API application
I'm not sure how to do either from within a WinForms application, but no
doubt there's someone else on this forum who could help. Good luck!

Chris Jobson
 
I tried this however however ClientRectangle has always x=0, and y=0
therefore this is not working correctly.

Do I have to write something else.

I tried this.bounds.contains(ptCursor), however that is also giving
wrong behaviour

The code I posted works fine for me. Did you remember to include the line
ptCursor = PointToClient(ptCursor);
to convert the cursor position to client coordinates?

I think that the reason why Bounds.Contains(ptCursor) won't work is that as
you move the mouse out of your client area onto the window border the
Mouseleave event fires. At this time the mouse is still within the Bounds
(which include the border) so the test for being outside the window fails.
When the mouse subsequently moves off the border to outside the window you
don't get another event because the mouse has already left the client area.

Some other things you could try are:
- Use the Win32 API function TrackMouseEvent and intercept the
WM_NCMOUSELEAVE message.
- Install a LowLevelMouseProc hook.

Although I know how to do both of these from a pure Win32 API application
I'm not sure how to do either from within a WinForms application, but no
doubt there's someone else on this forum who could help. Good luck!

Chris Jobson
 
I tried this however however ClientRectangle has always x=0, and y=0
therefore this is not working correctly.

Do I have to write something else.

I tried this.bounds.contains(ptCursor), however that is also giving
wrong behaviour

The code I posted works fine for me. Did you remember to include the line
ptCursor = PointToClient(ptCursor);
to convert the cursor position to client coordinates?

I think that the reason why Bounds.Contains(ptCursor) won't work is that as
you move the mouse out of your client area onto the window border the
Mouseleave event fires. At this time the mouse is still within the Bounds
(which include the border) so the test for being outside the window fails.
When the mouse subsequently moves off the border to outside the window you
don't get another event because the mouse has already left the client area.

Some other things you could try are:
- Use the Win32 API function TrackMouseEvent and intercept the
WM_NCMOUSELEAVE message.
- Install a LowLevelMouseProc hook.

Although I know how to do both of these from a pure Win32 API application
I'm not sure how to do either from within a WinForms application, but no
doubt there's someone else on this forum who could help. Good luck!

Chris Jobson
 
Back
Top