问题
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