Created
August 3, 2012 22:24
-
-
Save DexterHaslem/3252136 to your computer and use it in GitHub Desktop.
C# Right click Pinvoke w/ coord conversion for UI automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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