我一直在读书,但似乎没有找到这个问题的答案。目标是:我在Windows窗体项目中有一个WebBrowser
。有一个HTML页面加载,在该HTML页面中有一个Flash对象。我的目标是将鼠标单击发送到该Flash对象的(x,y)位置。
(所需)必需品
示例
想象一下,我在WebBrowser中打开任何Tower Defense游戏。我想模拟点击购买和放置新塔。在进程期间,用户不得放松对鼠标的控制。如果在像素(x,y)=(30,50)是一个按钮,我想点击该按钮,我可以通过调用类似WebBrowser.simulateLeftClick(30, 50)
的内容。
答案 0 :(得分:1)
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
可以通过Spy++
public static void MouseClick(int x, int y, IntPtr handle, string Class)
{
StringBuilder className = new StringBuilder(100);
while (className.ToString() != Class)
{
handle = GetWindow(handle, 5);
GetClassName(handle, className, className.Capacity);
}
IntPtr lParam = (IntPtr)((y << 16) | x);
IntPtr wParam = IntPtr.Zero;
const uint downCode = 0x201;
const uint upCode = 0x202;
SendMessage(handle, downCode, wParam, lParam);
SendMessage(handle, upCode, wParam, lParam);
}
WebBrowser句柄
IntPtr handle = webBrowser1.Handle