C# WebBrowser send Mouse Click Event to Flash Object

我怕爱的太早我们不能终老 提交于 2020-01-03 05:16:12

问题


I've been reading around but didn't seem to find an answer to this question. The objective is: I have a WebBrowser in a Windows Forms Project. There is a HTML page loaded, and in that HTML page there is a Flash Object. My goal is to send a Mouse Click to (x, y) positions of that Flash Object.

The (desired) Requisites

  • The window with the Windows Forms executable doesn't need to be active in order for this to work
  • The mouse must not be captured
  • Send a mouse click based on coordinates (x, y), relative to the Flash Object or the Web Page

Example

Imagine I open any Tower Defense game in the WebBrowser. I want to simulate clicks to buy and place new towers. During the proccess the user must not loose control of the mouse. If at pixel (x, y) = (30, 50) was a button and I wanted to click that button, I could just by calling something like WebBrowser.simulateLeftClick(30, 50).


回答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);

Class can be defined by means of 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 handle

IntPtr handle = webBrowser1.Handle


来源:https://stackoverflow.com/questions/21176778/c-sharp-webbrowser-send-mouse-click-event-to-flash-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!