Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Created August 3, 2012 16:56
Show Gist options
  • Save DexterHaslem/3249480 to your computer and use it in GitHub Desktop.
Save DexterHaslem/3249480 to your computer and use it in GitHub Desktop.
AutomationElement pinvoke right click
// add references WindowsBase, UIAutomationClient, UIAutomationTypes
// using System.Windows.Automation;
// using System.Windows;
[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public void RightClick(AutomationElement element)
{
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
Point point = element.GetClickablePoint();
object processId = element.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty);
AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.ProcessIdProperty, processId));
Int32 hWnd = window.Current.NativeWindowHandle;
double x = point.X;
double y = point.Y;
Int32 value = ((int)x)<<16 + (int)y;
SendMessage(new IntPtr(hWnd), WM_LBUTTONDOWN, IntPtr.Zero, new IntPtr(value));
SendMessage(new IntPtr(hWnd), WM_LBUTTONUP, IntPtr.Zero, new IntPtr(value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment