我有一个项目,涉及打开多个窗口客户端,然后在每个进程中模拟鼠标点击。我已经能够使用Win32应用编程接口和SendMessage成功地向多个Notepad实例发送消息。适用于我的代码如下:
Process[] notepads = Process.GetProcessesByName("notepad");
foreach (Process proc in notepads)
{
IntPtr handle = proc.Handle;
IntPtr child = FindWindowEx(proc.MainWindowHandle, new IntPtr(0), "Edit", null);
if (child != null)
{
MessageBox.Show("Child was found, sending text");
SendMessage(child, 0x000C, 0, "test");
}
}
}这将向我打开的记事本的每个实例发送"test“,不管有多少。正如您所看到的,我正在遍历流程的每个实例,并简单地循环消息。一点也不难。
我检索了窗口句柄,然后检索了子(Awesomium)类名,即Chrome_RenderWidgetHostHWND。在那里,我尝试通过重构Sendmessage中的变量类型来发送鼠标事件,以便系统能够进行汇编和lParam读取。以下是代码:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);int x = 834;
int y = 493;
IntPtr lParam = (IntPtr)((y << 16) | x);
IntPtr wParam = IntPtr.Zero;我正在使用全局鼠标钩子来检测所有子窗口中的按下事件,当我点击按钮迭代整个过程并单击时,根本不会发送任何内容。我知道我在做一些愚蠢的事情,这会导致鼠标点击不被发送。无论如何,下面是我所拥有的最终代码结构。任何建议或建议都将不胜感激。谢谢。
private void button2_Click(object sender, EventArgs e)
{
Process[] notepads = Process.GetProcessesByName("client");
foreach (Process proc in notepads)
{
IntPtr handle = proc.Handle;
string mine = Convert.ToString(proc);
MessageBox.Show(mine);
IntPtr child = FindWindowEx(proc.MainWindowHandle, new IntPtr(0), "Chrome_RenderWidgetHostHWND", null);
if (child != null)
{
int x = 834;
int y = 493;
IntPtr lParam = (IntPtr)((y << 16) | x);
IntPtr wParam = IntPtr.Zero;
SendMessage(child, 0x201, wParam, lParam);
SendMessage(child, 0x202, wParam, lParam);
}
}
}发布于 2013-08-05 01:14:03
或者,您可以使用SendInput,它可以在有鼠标挂钩的情况下正常工作;您的解决方案(直接发布消息)则不能。
更重要的是,如果进程拒绝激活(即,如果目标从WM_MOUSEACTIVATE返回0),则不会发送WM_LBUTTONDOWN。
https://stackoverflow.com/questions/17752176
复制相似问题