简体   繁体   English

使用Windows窗体控制OS鼠标事件 - C#

[英]Control OS Mouse Event using Windows Form - C#

The program I am trying to do is to simulate the mouse event of a operating system using keyboard with Windows Form. 我正在尝试的程序是使用带有Windows窗体的键盘模拟操作系统的鼠标事件。 Right now I am able to change the cursor and do different actions like mouse clicking inside the Form (when the Form is on the Top). 现在我可以更改光标并执行不同的操作,例如鼠标在窗体内单击(当窗体位于顶部时)。

The problem is I would like to extend it to the whole Operating System, which means even if my Windows Form is not at the top, I am still able to control my cursor and do all sorts of mouse event on other applications while the Form is running. 问题是我想将它扩展到整个操作系统,这意味着即使我的Windows窗体不在顶部,我仍然可以控制我的光标并在其他应用程序上执行各种鼠标事件,而Form是运行。 How should I do to implement this ? 我该怎么做才能实现这个目标?

您可能希望查看.NET中的这个库Global System Hooks,它使用全局系统挂钩来检测所有鼠标和键盘事件,包括应用程序之外的事件。

You can synthesize OS-wide keystrokes, mouse motions, and button clicks using the Win32 SendInput() API. 您可以使用Win32 SendInput() API合成操作系统范围的击键,鼠标移动和按钮单击。 You can call it from C# using P/Invoke . 您可以使用P / Invoke从C#中调用它。 Sample code can be found here: SendInput on PInvoke.net . 示例代码可以在这里找到: PInvoke.net上的SendInput

I remember back in the day I used the SendInput (and a screenshot API) to create a Minesweeper bot in C# (2.0 I think). 我记得当天我使用SendInput(和屏幕截图API)在C#中创建了一个Minesweeper机器人(我认为是2.0)。 It could solve an Expert puzzle in about one second. 它可以在大约一秒钟内解决一个专家拼图。 I wish I still had the source code to sample here, but I don't. 我希望我仍然有源代码在这里采样,但我没有。

EDIT: It appears someone has already created a nice .NET warpper for the SendInput() : Windows Input Simulator on CodePlex . 编辑:似乎有人已经为SendInput()创建了一个很好的.NET warpper: CodePlex上的Windows输入模拟器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM