Web Automation鼠标单击

时间:2013-07-19 17:32:36

标签: c# .net winapi pinvoke sendmessage

我有一个项目涉及打开多个窗口客户端,然后在每个进程中模拟鼠标点击。我已经能够使用Win32 API和SendMessage成功地向记事本的多个实例发送消息。适用于我的代码如下:

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”,无论多少。正如您所看到的,我正在遍历流程的每个实例,并简单地循环消息。一点都不难......

最终目标不是记事本,而是Microsoft扩展Awesomium。我检索了窗口句柄,然后是子(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);

通过一些例子我发现我通过这个组装了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);
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

或者您可以使用SendInput代替,它可以在鼠标挂钩的情况下正常工作;你的解决方案(直接发布消息)不会。

更重要的是,如果进程拒绝激活(即,如果目标从WM_MOUSEACTIVATE返回0),则不会发送WM_LBUTTONDOWN。