javascript - C# webbrowser - trigger right click -
i'm writing code on visual studio in order information web page. need open context menu of html element , looking that:
webbrowser.navigate("javascript: document.getelementsbyclassname('classname')[1].rightclick();void(0);");
but, unfortunately, noticed click() function exists in javascript. there workaround?
thank all!
you can flowing steps:
get position of
web-browser control
on form:point controlloc = this.pointtoscreen(webbrowser1.location);
get position
htmlelement
on web-browser controlx= element.offsetrectangle.left; y =element.offsetrectangle.top;
sum positions:
controlloc.x = controlloc.x + element.offsetrectangle.left; controlloc.y = controlloc.y + element.offsetrectangle.top;
set mouse position new location:
cursor.position = controlloc;
simulate mouse right-click:
mousesimulator.clickrightmousebutton();
complete code:
public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { webbrowser1.documenttext = "<button class=\"mybtn\" type=\"submit\"> right click"; webbrowser1.documentcompleted += new webbrowserdocumentcompletedeventhandler(webbrowser1_documentcompleted); } void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { foreach (htmlelement element in webbrowser1.document.getelementsbytagname("button")) { if (element.getattribute("classname") == "mybtn") { point controlloc = this.pointtoscreen(webbrowser1.location); //get element posation controlloc.x= controlloc.x + element.offsetrectangle.left; controlloc.y = controlloc.y + element.offsetrectangle.top; cursor.position = controlloc; mousesimulator.clickrightmousebutton(); } } } } public class mousesimulator { [dllimport("user32.dll", setlasterror = true)] static extern uint sendinput(uint ninputs, ref input pinputs, int cbsize); [structlayout(layoutkind.sequential)] struct input { public sendinputeventtype type; public mousekeybdhardwareinputunion mkhi; } [structlayout(layoutkind.explicit)] struct mousekeybdhardwareinputunion { [fieldoffset(0)] public mouseinputdata mi; [fieldoffset(0)] public keybdinput ki; [fieldoffset(0)] public hardwareinput hi; } [structlayout(layoutkind.sequential)] struct keybdinput { public ushort wvk; public ushort wscan; public uint dwflags; public uint time; public intptr dwextrainfo; } [structlayout(layoutkind.sequential)] struct hardwareinput { public int umsg; public short wparaml; public short wparamh; } struct mouseinputdata { public int dx; public int dy; public uint mousedata; public mouseeventflags dwflags; public uint time; public intptr dwextrainfo; } [flags] enum mouseeventflags : uint { mouseeventf_move = 0x0001, mouseeventf_leftdown = 0x0002, mouseeventf_leftup = 0x0004, mouseeventf_rightdown = 0x0008, mouseeventf_rightup = 0x0010, mouseeventf_middledown = 0x0020, mouseeventf_middleup = 0x0040, mouseeventf_xdown = 0x0080, mouseeventf_xup = 0x0100, mouseeventf_wheel = 0x0800, mouseeventf_virtualdesk = 0x4000, mouseeventf_absolute = 0x8000 } enum sendinputeventtype : int { inputmouse, inputkeyboard, inputhardware } public static void clickrightmousebutton() { input mousedowninput = new input(); mousedowninput.type = sendinputeventtype.inputmouse; mousedowninput.mkhi.mi.dwflags = mouseeventflags.mouseeventf_rightdown; sendinput(1, ref mousedowninput, marshal.sizeof(new input())); input mouseupinput = new input(); mouseupinput.type = sendinputeventtype.inputmouse; mouseupinput.mkhi.mi.dwflags = mouseeventflags.mouseeventf_rightup; sendinput(1, ref mouseupinput, marshal.sizeof(new input())); } }
result:
Comments
Post a Comment