c# - How can I send a right-click event to an AutomationElement using WPF's UI automation? -


using wpf's built-in ui automation, it's easy (though pretty verbose) send left-click event automationelement (such button):

invokepattern invokepattern = (invokepattern) element.getcurrentpattern(invokepattern.pattern); invokepattern.invoke(); 

however, there seems no built-in way send right-clicks same element. i've resigned myself using p/invoke call sendinput, can't work. code below, when call rightclick(), context menu pops right cursor is, rather @ element expect right-clicked. seems it's ignoring coordinates hand in , using current cursor location.

public static void rightclick(this automationelement element) {     point p = element.getclickablepoint();      nativestructs.input input = new nativestructs.input     {         type = nativeenums.sendinputeventtype.mouse,         mouseinput = new nativestructs.mouseinput         {             dx = (int) p.x,             dy = (int) p.y,             mousedata = 0,             dwflags = nativeenums.mouseeventflags.absolute | nativeenums.mouseeventflags.rightdown,             time = 0,             dwextrainfo = intptr.zero,         },     };      nativemethods.sendinput(1, ref input, marshal.sizeof(input));      input.mouseinput.dwflags = nativeenums.mouseeventflags.absolute | nativeenums.mouseeventflags.rightup;      nativemethods.sendinput(1, ref input, marshal.sizeof(input)); }  internal static class nativemethods {     [dllimport("user32.dll", setlasterror = true)]     internal static extern uint sendinput(uint ninputs, ref nativestructs.input pinputs, int cbsize); }  internal static class nativestructs {     [structlayout(layoutkind.sequential)]     internal struct input     {         public nativeenums.sendinputeventtype type;         public mouseinput mouseinput;     }      [structlayout(layoutkind.sequential)]     internal struct mouseinput     {         public int dx;         public int dy;         public uint mousedata;         public nativeenums.mouseeventflags dwflags;         public uint time;         public intptr dwextrainfo;     } }  internal static class nativeenums {     internal enum sendinputeventtype : int     {         mouse = 0,         keyboard = 1,         hardware = 2,     }      [flags]     internal enum mouseeventflags : uint     {         move = 0x0001,         leftdown = 0x0002,         leftup = 0x0004,         rightdown = 0x0008,         rightup = 0x0010,         middledown = 0x0020,         middleup = 0x0040,         xdown = 0x0080,         xup = 0x0100,         wheel = 0x0800,         absolute = 0x8000,     } } 

from understand, you're correct sendinput being necessary simulate right click on uia element.

as how force cursor move element before right click, might try adding mouseeventf_move flag dwflags.

if still doesn't work, perhaps try calling sendinput twice - once move mouse (with "dwflags = mouseeventf_absolute | mouseeventf_move") , second time perform right click way you're doing now.

also, have seen project?

http://inputsimulator.codeplex.com/

not sure how complete mouse input support @ moment, might useful.

also, question might useful too:

using mouse sendinput in c


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -