I created the following code which will mouse down an automationelement’s clickable point and then mouseup on another element’s clickable point. This should have the effect of a drag and drop but it doesn’t do that. It behaves in a strange way. It seems to just select items instead of dragging.
public static void Main(String[] args)
{
contactsGrid.getCell("Cell Data").drag();
navTree.getNode("Tree Data").drop();
}
public void drag()
{
element.SetFocus();
ScreenClick.leftDown(element);
}
public void drop()
{
element.SetFocus();
ScreenClick.leftUp(element);
}
public static void leftDown(AutomationElement element)
{
while (!element.Current.IsKeyboardFocusable)
element = TreeWalker.RawViewWalker.GetFirstChild(element);
Point p;
element.TryGetClickablePoint(out p);
leftDown((int)p.X, (int)p.Y);
}
public static void leftUp(AutomationElement element)
{
while (!element.Current.IsKeyboardFocusable)
element = TreeWalker.RawViewWalker.GetFirstChild(element);
Point p;
element.TryGetClickablePoint(out p);
leftUp((int)p.X, (int)p.Y);
}
public static void leftDown(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y);
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
}
public static void leftUp(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
Try using mouse_event (MOUSEEVENTF_MOVE|…_ABSOLUTE) to do the move instead of Cursor.Position.
(Note that the coordinates it takes are different to plain screen coordinates, so you’ll have to do some mapping/scaling, treating the whole screen as a 0,0 to 65535,65535 space – more details in the Remarks section for mouse_event.)
Typically apps implement drag by waiting for a WM_LBUTTONDOWN, and usually expect some WM_MOUSEMOVE messages also, otherwise they’ll just consider a plain UP/DOWN to be a click, not a drag. My guess is that by using Cursor.Position (which is basically a wrapper for SetCursorPos), you’re “changing the location of the the cursor without moving it” – no input messages are generated, so the underlying app never gets any WM_MOUSEMOVE messages and never realizes that it’s supposed to be doing a drag.