Saturday 15 March 2014

mouseevent - Perform a mouse Click event on another Application using C# -



mouseevent - Perform a mouse Click event on another Application using C# -

what need need command application installed on same machine using custom application. illustration if needed utilize standard windows calculator send input events calculator. have used code snippets create possible , have triggered both mouse , keyboard events. problem can sure keyboard event nail target application because has process handle. cannot sure mouse. , if target application goes background, cannot initiate mouse clicks on it. need help find way create sure mouse click done on application only.

i need send mouse co-ordinates , click well. illustration "sendmouseclick("notepad", 100, 400); send click notepad, though stays minimized.

***************************************************** of import note***************************************************** similar question answered in reference first finding state of other application , sending inputs either keyboard or mouse, need send application set of instructions must work whether application in foreground or not. "the other guys":: if dont want help or cant help, thats okay please know havent stolen question or anything. want accomplish task in c#.

the code have simulate keyboard key press is:

using system; using system.diagnostics; using system.runtime.interopservices; using system.drawing; using system.windows.forms; namespace simulatekeypress { partial class form1 : form { private button button1 = new button(); private button button2 = new button(); private button button3 = new button(); [stathread] public static void main() { application.enablevisualstyles(); application.run(new form1()); } public form1() { button1.location = new point(10, 10); button1.tabindex = 1; button1.text = "click automate calculator"; button1.autosize = true; button1.click += new eventhandler(button1_click); button2.location = new point(150, 140); button2.tabindex = 0; button2.text = "click exit calculator"; button2.autosize = true; button2.location = new point(80, 80); button2.tabindex = 2; button2.text = "click run calculator"; button2.autosize = true; button2.click += new eventhandler(button2_click); this.doubleclick += new eventhandler(form1_doubleclick); this.controls.add(button1); this.controls.add(button2); // this.controls.add(button3); } // handle application window. [dllimport("user32.dll", charset = charset.unicode)] public static extern intptr findwindow(string lpclassname, string lpwindowname); // activate application window. [dllimport("user32.dll")] public static extern bool setforegroundwindow(intptr hwnd); // send series of key presses calculator application. private void button1_click(object sender, eventargs e) { // handle calculator application. window class // , window name obtained using spy++ tool. intptr calculatorhandle = findwindow("calcframe", "calculator"); //process firstproc = new process(); //firstproc.startinfo.filename = "calc.exe"; //firstproc.enableraisingevents = true; //firstproc.start(); // verify calculator running process. if (calculatorhandle == intptr.zero) { messagebox.show("calculator not running."); return; } // create calculator foreground application , send // set of calculations. setforegroundwindow(calculatorhandle); sendkeys.sendwait("1024"); sendkeys.sendwait("*"); sendkeys.sendwait("32"); sendkeys.sendwait("="); } private void button2_click(object sender, eventargs e) { system.diagnostics.process.start("calc.exe"); } private void button3_click(object sender,eventargs e) { process [] proc =process.getprocessesbyname("calculator"); proc[0].kill(); } // send key button when user double-clicks anywhere // on form. private void form1_doubleclick(object sender, eventargs e) { // send come in key button, raises click // event button. works because tab stop of // button 0. sendkeys.send("{enter}"); } } }

the previous help on stack overflow, msdn , other sites provides code simulate mouse click in same application. need send mouse hits application.

maybe help you

code

the task

getting mouse's current position sending mouse event windows forms ... using system.runtime.interopservices; namespace yournamespace { public partial class yourclassname { [dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)] public static extern void mouse_event(uint dwflags, uint dx, uint dy, uint cbuttons, uint dwextrainfo); private const int mouseeventf_leftdown = 0x02; private const int mouseeventf_leftup = 0x04; private const int mouseeventf_rightdown = 0x08; private const int mouseeventf_rightup = 0x10; int x = cursor.position.x; int y = cursor.position.y; mouse_event(mouseeventf_leftdown | mouseeventf_leftup, x, y, 0, 0); } } wpf

things bit harder in wpf

double mousepointx; double mousepointy; [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool getcursorpos(out point lppoint); [structlayout(layoutkind.sequential)] public struct point { public int x; public int y; public point(int x, int y) { this.x = x; this.y = y; } } private void writepoint(object sender, routedeventargs e) { point p; if (getcursorpos(out p)) { system.console.writeline(convert.tostring(p.x) + ";" + convert.tostring(p.y)); } } [dllimport("user32.dll")] static extern intptr getdc(intptr hwnd); [dllimport("gdi32.dll")] static extern int getdevicecaps(intptr hdc, int nindex); [dllimport("user32.dll")] static extern bool releasedc(intptr hwnd, intptr hdc); private point convertpixelstounits(int x, int y) { // scheme dpi intptr ddc = getdc(intptr.zero); // desktop dc int dpi = getdevicecaps(ddc, 88); bool rv = releasedc(intptr.zero, ddc); // wpf's physical unit size calculated taking // "device-independant unit size" (always 1/96) // , scaling scheme dpi double physicalunitsize = (1d / 96d) * (double)dpi; point wpfunits = new point(physicalunitsize * (double)x, physicalunitsize * (double)y); homecoming wpfunits; } private void writemousecoordinatesinwpfunits() { point p; if (getcursorpos(out p)) { point wpfpoint = convertpixelstounits(p.x, p.y); system.console.writeline(convert.tostring(wpfpoint.x) + ";" + convert.tostring(wpfpoint.y)); mousepointy = wpfpoint.y; mousepointx = wpfpoint.x } }

now of import part of code

[dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)] public static extern void mouse_event(uint dwflags, uint dx, uint dy, uint cbuttons, uint dwextrainfo); private const int mouseeventf_leftdown = 0x02; private const int mouseeventf_leftup = 0x04; private const int mouseeventf_rightdown = 0x08; private const int mouseeventf_rightup = 0x10; ... mouse_event(mouseeventf_leftdown | mouseeventf_leftup, convert.touint32(mousepointx), convert.touint32(mousepointy), 0, 0); ... warning the code tested the code not "copy & paste code

c# mouseevent simulate

No comments:

Post a Comment