Click here to Skip to main content
13,048,584 members (91,107 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 12:54pm
SAKryukov 8-Apr-12 20:43pm
Is System.Windows.Forms? Tag it! Without doing it, your question is not correct.
Rate this: bad
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:[^].

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:[^].

If you need learn P/Invoke, please see:[^],[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

About C++/CLI, please see:[^],[^],[^].


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,
Wes Aday 8-Apr-12 21:21pm
5 for correctly identifying that he doesn't need it (most likely) and will annoy his users.
SAKryukov 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.
VJ Reddy 8-Apr-12 21:43pm
Good advice and answer. +5
SAKryukov 8-Apr-12 21:49pm
Thank you, VJ.
Philippe Mori 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 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.
Nelek 9-Apr-12 6:31am
SAKryukov 9-Apr-12 9:52am
Thank you, Nelek.
Rate this: bad
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[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Apr 2012
Copyright © CodeProject, 1999-2017
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