C#,窗体外的鼠标事件 [英] C#, Mouse Event outside the Form

查看:264
本文介绍了C#,窗体外的鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好......
如何使用外部鼠标单击来关闭表单?
我有mainForm ...并且每次打开其他窗体时,都需要在它们的边界之外单击时将其关闭.

P.S.对不起,我的英语

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

推荐答案

外部鼠标事件可能被分派到其他一些应用程序,因此对于常规.NET应用程序,在逻辑上它们不存在.我的第一个回答是:您真的不需要它.您想要的行为将与操作系统可接受的UI模型相抵触;这也会使您的客户发疯.

但是,仅是为了使答案完整,我可以告诉您在技术上您可以做到.您可以使用两种方法之一.

第一个将提供有限的行为.您没有按Z顺序说出应该在表格下方的内容.可能是同一应用程序形式的另一个f.您可以将该表格填满整个屏幕.它可以最大化,但不是必须的.如果是这种情况,请在下面处理此表单中的单击,并写一个句柄以关闭顶部的表单.也可能是事件System.Windows.Forms.Form.Activated或下面形式的重写虚拟方法OnActivated.如果在激活下面的表单时将其放在顶部,请对其进行处理以将其关闭.

第二种方法是通用的,但仅在Windows上有效,因为它需要一些用本机Windows API编写的代码.请参阅:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms632589%28v=vs.85%29.aspx [
有一个理由更喜欢将C ++与C ++/CLI结合使用.原因如下:您需要使用钩子在应用程序外部处理事件.这意味着您需要安装全局挂钩.根据Microsoft文档,仅当您在本机Windows DLL中调用函数SetWindowsHookEx时,全局挂钩才起作用,而该Windows DLL无论如何都无法使用P/Invoke在C#中编写.您可以使用纯C ++(本机代码)编写此可执行模块,并通过P/Invoke为.NET提供一些API.如果要使用非托管C ++,为什么不同时使用C ++/CLI来覆盖与.NET的互操作性呢?请参阅:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms644990%28v=vs.85%29.aspx [ http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

此CodeProject文章也可能有用:
基本P/调用 [ 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 [ ^ ].



您可能会问:哪种方法是最好的?".我的答案是:第一个.完全不执行这种不良行为要优于其他任何决定.

祝你好运,
—SA
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[^].



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


您可以为此使用Windows Hook API.看看WindowsHook库全局Windows挂钩 [
You can use Windows Hook API for that. Have a look at WindowsHook Library Global Windows Hooks[^]


这篇关于C#,窗体外的鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆