Attempting to Simulate Mouse Click / Drag

后端 未结 3 1195
既然无缘
既然无缘 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:12

    The following code should return true if the left mouse button is down, false if it is up, Control being System.Windows.Forms.Control:

        Control.MouseButtons.HasFlag(MouseButtons.Left)
    

    p.s. documentation for this can be found on MSDN here.

    0 讨论(0)
  • 2021-01-12 17:22

    The Easiest answer was infact to use a bool and just check to see what's going on.

    I started it on a new thread so it didn't break everything else.

    Idealy you'd tidy this up a little bit.

        public static void Grab(int xPos, int yPos)
        {
            _dragging = true;
    
            Cursor.Position = new Point(xPos, yPos + offSet);
            mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);
    
            var t = new Thread(CheckMouseStatus);
            t.Start();
        }
        public static void Release(int xPos, int yPos)
        {
            _dragging = false;
            Cursor.Position = new Point(xPos, yPos + offSet);
            mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
        }
    
        private static void CheckMouseStatus()
        {
            do
            {
                Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
            } 
            while (_dragging);
        }
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
提交回复
热议问题