精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
介绍
This article presents a class, handling sending of input (mouse and keyboard) to any running Windows Control by its handle.
本文给出了一个类,通过句柄把(鼠标和键盘)的输入发送到任何运行Windows控件上。
Some time ago, I was making a program which would win a penalty shootout soccer flash game for me. My task in the application was repeatedly to do these things: 前一段时间,我写个程序帮我赢个点球大战足球Flash游戏。程序的任务是反复做这些事情:
I found some great articles concerning taking of screenshot like this one. 对于截屏,我发现一些非常棒的文章,比如这一个。
This library helped me with image processing a lot. 这个库帮助我处理了很多图像分析的事务。
On the other hand, I spent a lot of time trying to find out how to send mouse input to flash window correctly... and when I got how to do it, I decided to make this library. 另一方面,我花了很多时间试图找出如何正确发送鼠标输入给Flash窗口... 时,我如何做到这一点,我决定完成这个库。
In order to use the TakeOver class, you need to obtain the handle (int number) of the window you want to control. It's possible to use spy++ tool from Visual Studio and convert HEX value to DEC using Windows calculator. The other possibility is to implement your own Window Picker (like in the demo project). Further details about this problem are out of scope for this article.为了使用TakeOver 类,你需要获得你想控制的窗口句柄(int number)。可用Visual Studio工具spy++,使用Windows计算器转换十六进制到十进制。另一种可能性是实现自己的窗口拾取器(如本文演示项目)。关于这个问题的进一步的细节超出了本文范围。
The first thing you need to do to take control over the window is to create a TakeOver class instance. 要控制窗口,你需要做的第一件事就是创建一个TakeOver 类的实例。
Remo.TakeOver tO = new Remo.TakeOver(targetWindowHandle);
Sending of input messages to a window is quite self-explanatory. The class provides these methods for sendingmessages:
及时把输入消息发送给一个窗口比较明显易明。这个类提供了这些方法发送消息:
public void SendLeftButtonDown(int x,int y);
public void SendLeftButtonUp(int x,int y);
public void SendLeftButtonDblClick(int x,int y);
public void SendRightButtonDown(int x,int y);
public void SendRightButtonUp(int x,int y);
public void SendRightButtonDblClick(int x,int y);
public void SendMouseMove(int x,int y);
public void SendKeyDown(int key);
public void SendKeyUp(int key);
public void SendChar(char c);
public void SendString(string s);
Sending input usually requires a target window to be focused to work properly, however it's not always so. The demo application can be tested here. Normally, you will have the application running in the background with controlled window focused, so there should be no problem with sending input messages. Since keyboard messages are not very reliable (I do not know how to encode lParam of SendMessage correctly, any hints?), it is recommended for keyboard input to use the SendKeys class once the window is focused via the SetFocus() method. Sendkeys is part of the standard System.Windows.Forms namespace. To focus target window TakeOver class provides a method:发送输入通常需要一个目标窗口有焦点能正常工作,但它并不总是如此。演示应用程序可以在这里测试。通常情况下,有应用程序会在后台运行,也会有带焦点的前台控制窗口,所以应该没有问题,发送 输入消息。因为键盘消息不是很可靠(我不知道如何正确编码 SendMessage的lParam,有提示不?),对键盘输入建议一旦窗口通过SetFocus()设置上焦点后才使用SendKeys 类。 Sendkeys是标准System.Windows.Forms的命名空间的一部分。为了设置焦点,TakeOver 类提供了一种方法:
public void SetFocus();