Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Created August 3, 2012 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterHaslem/3252136 to your computer and use it in GitHub Desktop.
Save DexterHaslem/3252136 to your computer and use it in GitHub Desktop.
C# Right click Pinvoke w/ coord conversion for UI automation
public static class NativeMethods
{
[DllImport("user32.dll")]
internal static extern void mouse_event([In]uint dwFlags, [In]int dx, [In]int dy, [In]uint dwData, [In]uint dwExtraInfo);
[Flags]
internal enum MOUSEEVENTF : uint
{
MOVE = 0x00001,
LEFTDOWN = 0x00002,
LEFTUP = 0x00004,
RIGHTDOWN = 0x00008,
RIGHTUP = 0x00010,
MIDDLEDOWN = 0x00020,
MIDDLEUP = 0x00040,
XDOWN = 0x00080,
XUP = 0x00100,
WHEEL = 0x00800,
HWHEEL = 0x01000,
MOVE_NOCOALESCE = 0x02000,
VIRTUALDESK = 0x04000,
ABSOLUTE = 0x08000,
}
private static int MouseCoordToNormalized(int coord, int width_or_height)
{
return ((65536 * coord) / width_or_height) + 1;
}
public static void RightClickPoint(int x, int y)
{
int totalWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width; //NativeMethods.GetSystemMetrics(NativeMethods.SM_CXSCREEN);
int totalHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height; //NativeMethods.GetSystemMetrics(NativeMethods.SM_CYSCREEN);
x = MouseCoordToNormalized(x, totalWidth);
y = MouseCoordToNormalized(y, totalHeight);
mouse_event((uint)(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.RIGHTDOWN), x, y, 0, 0);
mouse_event((uint)(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.RIGHTUP), x, y, 0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment