473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.

Jun 27 '08 #1
5 3397
<vi***********@ gmail.comwrote in message
news:14******** *************** ***********@z24 g2000prf.google groups.com...
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_MouseLeav e(object sender, EventArgs e)
{
Point ptCursor = Cursor.Position ;
ptCursor = PointToClient(p tCursor);
if (!ClientRectang le.Contains(ptC ursor))
{
// 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
Jun 27 '08 #2
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.con tains(ptCursor) , however that is also giving
wrong behaviour
Jun 27 '08 #3
<vi***********@ gmail.comwrote in message
news:20******** *************** ***********@s33 g2000pri.google groups.com...
>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.con tains(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(p tCursor);
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 LowLevelMousePr oc 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
Jun 27 '08 #4
<vi***********@ gmail.comwrote in message
news:20******** *************** ***********@s33 g2000pri.google groups.com...
>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.con tains(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(p tCursor);
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 LowLevelMousePr oc 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
Jun 27 '08 #5
<vi***********@ gmail.comwrote in message
news:20******** *************** ***********@s33 g2000pri.google groups.com...
>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.con tains(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(p tCursor);
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 LowLevelMousePr oc 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
Jun 27 '08 #6

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

Similar topics

8
12689
by: NeoAsimov | last post by:
Hello, There is what I want to do. I worked on the problem since 6 hours but I had a problem (probably stupid) and I dont know how to resolve it. I need to have an Mouse Event Click when I click on the frame of a window form ( like the title bar of a window).
3
2560
by: AinO | last post by:
Hi, (VS2003/c# - System.Windows.Forms) Is there a way to know (have an event triggered) if the mouse leaves a form, if this form's client area is completly covered with child controls ? I tried various mouse move/leave event handling scenario's involving the form or the controls on it's client area but no
5
3722
by: Charles Law | last post by:
Sorry for reposting this question, but I did not get a single answer last time, and I'm sure you guys must have some thoughts on the matter. I have a user control which can be dragged and dropped onto a form in my application when it is running. I allow it to be clicked and dragged to a new location on the form. However, the user control has a check box on it, and if the user clicks over the checkbox to drag the user control, the check...
3
3212
by: Lance | last post by:
I've noticed that controls that are contained in MDI child forms fail to raise MouseLeave events if the MDI child form's MdiParent property is set to Nothing (after it was set to an existing MDI container form) or if the MDI child form's Visible property is set to False (after the MDI child form was shown). This is an enormous problem for my app because I must show different MDI child forms based on the state of my application and many of the...
0
1668
by: Nickneem | last post by:
I' m trying to disable all right mouse clicks by using the vbAccelerator Windows Hooks Library The small (systray / console) app. must catch all (right) mouseclicks before they are received by the users application (for instance IE). I' m not getting passed the stage where I write to the console catching clicks on my applications form.. I really doubt if this one's possible at all (especially for an API noob like me)..
4
9244
by: cb.brite | last post by:
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...
5
2395
by: moonie | last post by:
I have an msn style c# windows application with a form and panel on it. A news list is drawn from the database and dynamically added via labels, link lables during form loading. In this application at run time this list should be generated again as the filtering criteria changes. So I clear the controls(labels) on the panel, get the list from the database and generate the list again. The list is created and it works fine. But sometimes...
3
4337
by: Spam Catcher | last post by:
Hi all, I'm having some trouble with the Mouse Hover/Leave events on a user control. Say I have a user control with a panel covering the whole control. In turn, a label + picture box cover the panel. If the mouse is anywhere over the user control, I want the background colour to change.
22
6436
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.