Attempting to Simulate Mouse Click / Drag

后端 未结 3 1196
既然无缘
既然无缘 2021-01-12 16:33

So I\'m trying to simulate the left mouse click and the left mouse release to do some automated dragging and dropping.

It\'s currently in a C# Winforms (Yes, winform

3条回答
  •  有刺的猬
    2021-01-12 17:36

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);
    
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);
    
        const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
        const uint MOUSEEVENTF_LEFTUP = 0x0004;
        const uint MOUSEEVENTF_MOVE = 0x0001;
    
        static void Drag(int startX,int startY,int endX,int endY)
        {
            endX = endX - startX;
            endY = endY - startY;
            SetCursorPos(startX, startY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
    

提交回复
热议问题