模拟鼠标左键单击目标,然后将鼠标移动到C#.net

时间:2017-03-08 04:45:36

标签: c# .net click cursor

我需要在代码(c#.net)中的某些场景中模拟鼠标左键单击。 必须完成此模拟,但必须对用户不可见。因此,当左键单击将要发生时,我保存光标位置。左键单击后,我应将光标位置设置为保存值。 这儿存在一个问题。

                int save_cursur_x = Cursor.Position.X;
                int save_cursur_y = Cursor.Position.Y;
                int current_node_x = newGraphEditor.getCurrentNodeGlobalPosition_x();
                int current_node_y = newGraphEditor.getCurrentNodeGlobalPosition_y();
                LeftMouseClick(current_node_x, current_node_y);
                Cursor.Position = new Point(save_cursur_x, save_cursur_y);

当删除最后一行(Cursor.Position = new Point(save_cursur_x,save_cursur_y))时,左击会在目标位置上正确发生。但当我添加该行时,左键单击发生在未知位置(或者左键单击不会发生)

这是 LeftMouseClick 方法:

            //This is a replacement for Cursor.Position in WinForms
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            static extern bool SetCursorPos(int x, int y);

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

            public const int MOUSEEVENTF_LEFTDOWN = 0x02;
            public const int MOUSEEVENTF_LEFTUP = 0x04;

            //This simulates a left mouse click
            public static void LeftMouseClick(int xpos, int ypos)
            {
                SetCursorPos(xpos, ypos);
                mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
            }

更新 有一个选择代表,其中设置了目标位置。当您单击目标时,此委托应该完全运行。我认为问题在这里。左键单击目标后,该代表不会运行。将Cursor位置设置为上一个值后,将运行委托。所以目标位置没有正确设置,因为现在光标不在目标上。(它的位置已经改变)

ItemEventHandler<IModelItem> selectionDelegate =

                    delegate (object source, ItemEventArgs<IModelItem> args)
                  {
                      var tag = ((INode)(args.Item)).Tag;
                      if (tag is HostInnerNodeTag)
                      {
                          currentNodeGlobalPosition_x = Cursor.Position.X;
                          currentNodeGlobalPosition_y = Cursor.Position.Y;
                       }
                  }

1 个答案:

答案 0 :(得分:0)

您可以改用SendMessage。

    [DllImport("user32.dll")]
    public static extern int SendMessage(
                      IntPtr hWnd,      // handle to destination window
                      uint Msg,       // message
                      IntPtr wParam,  // first message parameter
                      IntPtr lParam   // second message parameter
                      );

    public const uint WM_LBUTTONDOWN = 0x0201;
    public const uint WM_LBUTTONUP = 0x0202;       

    //This simulates a left mouse click
    public static void LeftMouseClick(IntPtr hwnd, uint xpos, uint ypos)
    {
        SendMessage(hwnd, WM_LBUTTONDOWN, new IntPtr(xpos), new IntPtr(ypos));
        SendMessage(hwnd, WM_LBUTTONUP, new IntPtr(xpos), new IntPtr(ypos));
    }

    private void send()
    {            
        LeftMouseClick(newGraphEditor.Handle, 10, 10);
    }