在WPF应用程序中注入鼠标输入 [英] Injecting Mouse Input in WPF Applications

查看:124
本文介绍了在WPF应用程序中注入鼠标输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力将输入注入WPF应用程序.使这个项目变得困难的是,即使它在后台运行,我也需要能够将输入注入到应用程序中(即,另一个应用程序具有输入焦点).因此,就不可能使用 SendInput()函数.

I've been working on injecting input into a WPF application. What makes this project hard is that I need to be able to inject the input into the application even though it's running in the background (i.e. another application has the input focus). Using the SendInput() function is therefore out of the question.

到目前为止,我可以使用键盘输入,但是无法注入鼠标输入.

So far, I've got keyboard input working but am having trouble injecting mouse input.

我使用Spy ++观察物理上单击鼠标按钮时发送到WPF窗口的窗口消息.然后,我只需手工制作这些相同的鼠标消息(例如 WM_LBUTTONDOWN WM_LBUTTONUP ),然后将它们显式发送到WPF窗口以模拟鼠标输入.

I used Spy++ to observe the window messages that get sent to the WPF window when I physically click the mouse button. I then simply craft these same mouse messages (such as WM_LBUTTONDOWN and WM_LBUTTONUP) manually and send them explicitly to the WPF window to emulate mouse input.

不幸的是,这不能按预期方式工作(即使出于测试目的,我也没有将WPF窗口设置为前景窗口).

Unfortunately, this doesn't work as expected (not even when I, for testing purposes, have set the WPF window as the foreground window).

我已经在我的测试WPF窗口中添加了一个按钮,单击该按钮会显示一个消息框.但是,当我将光标手动定位在按钮上时,注入适当的鼠标消息并不会导致按钮被单击(即WPF框架未触发clicked事件).

I've added a button to my test WPF window which when clicked displays a message box. Injecting the appropriate mouse messages when I've manually positioned the cursor over the button doesn't cause the button to be clicked, however (i.e. the clicked event isn't fired by the WPF framework).

如果我在实际对话框(客户区域)上添加了一个用于单击鼠标的处理程序,则当我将光标置于对话框本身上方并注入与以前相同的窗口消息时,该处理程序就会被调用. :

If I add a handler for mouse clicks on the actual dialog (the client area), that handler does get called if I position the cursor over the dialog itself and inject the same window messages as before:

this.MouseLeftButtonDown += WndMouseDown;

public void WndMouseDown(object sender, EventArgs e)
{
   ...
}

奇怪的是,如果我将按钮的按下模式更改为按下"(即,它被认为是按下鼠标而不是默认按下鼠标),那么当我注入与以前相同的消息时,就会触发按钮单击事件. (值得一提的是,上面示例中的处理程序可正确触发鼠标上下移动,因此WPF框架似乎可以成功处理两条消息.)

Strangely enough, if I change the push mode of the button to Press (i.e. it's considered clicked on mouse down rather than the default mouse up), the button clicked event is now fired when I inject the same messages as before. (It's worth mentioning that the handler from the example above correctly fires for both mouse downs and ups, so it'd seem the WPF framework does process both messages successfully.)

为了使WPF框架触发鼠标单击事件,似乎还需要满足其他一些条件.有谁知道WPF内部如何处理鼠标输入,或者为什么它不将我的鼠标上下消息解释为单击按钮?

It seems like there are some other criteria that need to be fulfilled in order for a mouse clicked event to be fired by the WPF framework. Does anybody know how mouse input is handled internally in WPF, or why it's not interpreting my mouse up and down messages as a click on the button?

(值得一提的是,这种方法[发送窗口消息]在普通的Win32窗口(例如,开始"->运行"对话框)中可以很好地工作.区别在于,WPF仅具有一个物理Win32窗口,其余的则是WPF特定的,这意味着所有窗口消息都转到该顶层窗口,而不是实际按钮.)

(It's worth mentioning that this approach [sending window messages] works fine on ordinary Win32 windows, such as the Start->Run dialog. The difference here is that WPF only has one physical Win32 window and the rest is WPF specific, which means all window messages go to that top-level window rather than the actual button.)

我一直在高空寻找答案,希望能得到任何的想法或想法.

I've been searching high and low for an answer to this and would appreciate any thoughts or ideas.

推荐答案

我强烈建议您使用UIAutomation路线.您可以通过窗口句柄创建AutomationElement.爬到该按钮并调用它.我只想知道您是如何设法使键盘输入正常工作的.我目前正在尝试解决相反的问题.如何获取WPF窗口(我已经设法通过Win32调用来获取hWnd),以响应虚拟键盘消息.我已经在有问题的窗口上记录了++ spy会话,并复制了它的输入,但没有成功.

I'd highly suggest going the UIAutomation route. You create an AutomationElement by window handle. Crawl to the button and invoke it. I'd just like to know how you managed to get the keyboard input working. I am currently trying to resolve the converse issue. How to get a WPF window (I've managed to get a hWnd to it via Win32 calls), to respond to virtual keyboard messages. I've logged ++spy sessions on the window in question and replicated it's input without success.

这篇关于在WPF应用程序中注入鼠标输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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