Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Events Mouse
Hello....
How I can close the form with outside Mouse Click ?
I have mainForm...and every time when I open some other forms, I need to close them when I click outside theirs boundaries.
 
P.S. Sorry for my English
Posted 8-Apr-12 13:54pm
Comments
SAKryukov at 8-Apr-12 20:43pm
   
Is System.Windows.Forms? Tag it! Without doing it, your question is not correct.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Outside mouse events might be dispatched to some other application, so they might not logically exist for a regular .NET application. My first response would be this: you don't really need it. The behavior you want would contradict the accepted UI model of the OS; it would also drives your customers crazy.
 
However, just to make the answer complete, I can tell that you can technically do it. You can use one of the two approaches.
 
First one would provide limited behavior. You did not say what's supposed to be below your form in Z order. It could be another f of the form of the same application. You could have that form filling the entire screen. It can be maximized, but not necessarily. If this is the case, handle the click in this form underneath and write a handle to close a form on top. It also could be the event System.Windows.Forms.Form.Activated or the overridden virtual method OnActivated of the form underneath. Handle it to close the form on top if it was there during activation of the form underneath.
 
The second approach is universal, but will only work on Windows because it required some code written in native Windows API. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^].
 
You can use a hook in two ways: using P/Invoke or a mixed-mode C++/CLI + C++ project (managed+unmanaged), where you can always use Windows APU in native code and wrap it in a managed "ref" C++/CLI classes.
 
There is one reason to prefer C++ with C++/CLI. Here is why: you need to handle an event outside your application with a hook. It means that you need to install a global hook. According to Microsoft documentation, a global hook can only work if you call the function SetWindowsHookEx in a native Windows DLL which you cannot write in C# with P/Invoke anyway. You could write this executable module in pure C++ (native code) and provide some API for .NET via P/Invoke. If you would use unmanaged C++, why not using also C++/CLI to cover interoperability with .NET? Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx[^].
 
If you need learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].
 
This CodeProject article can also be useful:
Essential P/Invoke[^].
 
About C++/CLI, please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].
 
[EDIT]
 
You might ask: "Which way would be the best?". My answer is: the first one. Not implementing this ill behavior at all is by far superior to any other decision.
 
Good luck,
—SA
  Permalink  
v5
Comments
Wes Aday at 8-Apr-12 21:21pm
   
5 for correctly identifying that he doesn't need it (most likely) and will annoy his users.
SAKryukov at 8-Apr-12 21:28pm
   
Thank you, Wes.
I really need to emphasize that "you don't need it" is a primary part of this answer.
--SA
VJ Reddy at 8-Apr-12 21:43pm
   
Good advice and answer. +5
SAKryukov at 8-Apr-12 21:49pm
   
Thank you, VJ.
--SA
Philippe Mori at 8-Apr-12 23:42pm
   
Well if the user click outside the form it will typically be desactivated and it would be possible to use that to close the form if the form should more or less simulate the behavior of a combo box drop down list.
SAKryukov at 9-Apr-12 0:23am
   
I though about it -- not exactly. OP wants it only on click, not on Alt-Tab, for example. Again, the whole idea to have such behavior is wrong.
--SA
Nelek at 9-Apr-12 6:31am
   
+5
SAKryukov at 9-Apr-12 9:52am
   
Thank you, Nelek.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use Windows Hook API for that. Have a look at WindowsHook Library Global Windows Hooks[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 BillWoodruff 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 10 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100