拖放uiautomation

时间:2012-05-24 14:35:17

标签: drag-and-drop ui-automation

我创建了以下代码,该代码将鼠标按下自动化元素的可点击点,然后鼠标悬停在另一个元素的可点击点上。这应该具有拖放的效果,但它不会这样做。它表现得很奇怪。它似乎只是选择项而不是拖动。

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);
        }

1 个答案:

答案 0 :(得分:0)

尝试使用mouse_event(MOUSEEVENTF_MOVE | ..._ ABSOLUTE)来移动而不是Cursor.Position。

(请注意,它所采用的坐标与普通屏幕坐标不同,因此您必须进行一些映射/缩放,将整个屏幕视为0到65535,65535空间 - more details in the Remarks section for mouse_event。 )

通常,应用程序通过等待WM_LBUTTONDOWN来实现拖动,并且通常也会期望一些WM_MOUSEMOVE消息,否则他们只会将普通的UP / DOWN视为点击而不是拖动。我的猜测是通过使用Cursor.Position(它基本上是SetCursorPos的包装器),你“在没有移动它的情况下改变光标的位置” - 没有生成输入消息,所以底层应用程序永远不会获得任何WM_MOUSEMOVE消息,并且永远不会意识到它应该正在进行拖动。