[Closed] Trying to simulate a mouse click in dotnet WebBrowser
Hi all,
I am trying translate C# code to simulate a mouse left click in dotnet WebBrowser,searched google found too many examples:
https://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c
https://social.msdn.microsoft.com/Forums/vstudio/en-US/960411ab-d7c1-4f8f-99ac-dc273f50ace2/emulate-mouse-clicks-and-key-typing?forum=csharpgeneral
http://www.programmingforums.org/thread15427.html https://stackoverflow.com/questions/2736965/how-to-programatically-trigger-a-mouse-left-click-in-c
But tried a whole day,none was work in maxscript,does any one has c# knowledge can help me to solve this problem?Below is one code I’ve tried,thanks:
Fn Pressing_mouse =
(
source = ""
source += "using System.Runtime.InteropServices;
"
source += "using System.Windows.Forms;
"
source += "namespace mouse_click
"
source += "{
"
source += "public static class VirtualMouse
"
source += "{
"
source += "[DllImport(\"user32.dll\")]
"
source += "static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
"
source += "private const int MOUSEEVENTF_MOVE = 0x0001;
"
source += " private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
"
source += "private const int MOUSEEVENTF_LEFTUP = 0x0004;
"
source += "private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
"
source += "private const int MOUSEEVENTF_RIGHTUP = 0x0010;
"
source += "private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
"
source += "private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
"
source += "private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
"
source += "public static void LeftClick()
"
source += "{
"
source += "mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
"
source += "mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
"
source += "}
"
source += "}
"
source += "}
"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.GenerateInMemory = on
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
)
if pressing_now == undefined do global pressing_now = Pressing_mouse()
I’d suggest you to use Spy++ and investigate what is happening when you click browser manually. Doing so will save you a lot of time.
ps. make sure to click after the page is loaded completely. And I suppose x and y should be relative to the browser window.
fn makeparam w l = ( bit.or (bit.and w 0xFFFF) (bit.shift l 16) )
mouseParam = makeparam mouse.pos.x mouse.pos.y
windows.postMessage hwnd 0x201 0x1 mouseParam
windows.postMessage hwnd 0x202 0 mouseParam
Hi Serejah,thanks for your good suggestion,I’ve never tried Spy++ before,I’ll try later,for your code,I just run and got error:
– Unable to convert: undefined to type: IntegerPtr
How to fix it?Thanks a lot!
Do you know what hwnd stands for? It is a window handle. You need to provide it in order to send your message to a particular window
rollout X "" (
button btn "press me" across:2
button send "send"
on btn pressed do messageBox "I'm mpressed!"
on send pressed do (
windows.postMessage btn.hwnd[1] 0x201 0 0
windows.postMessage btn.hwnd[1] 0x202 0 0
)
)
createDialog X
windows.postMessage X.btn.hwnd[1] 0x201 0 0
windows.postMessage X.btn.hwnd[1] 0x202 0 0