Directing mouse events [DllImport(“user32.dll”)] click, double click

后端 未结 3 1514
遇见更好的自我
遇见更好的自我 2020-12-08 12:13

I tried [DllImport(\"user32.dll\")] static extern bool SetCursorPos(int X, int Y);

and it works pretty fine to move the cursor to the desired point. I have never tri

3条回答
  •  心在旅途
    2020-12-08 12:28

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
    private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
    private const uint MOUSEEVENTF_LEFTUP = 0x04;
    private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const uint MOUSEEVENTF_RIGHTUP = 0x10;
    

    You should Import and Define these Constant's to work with Mouse using Win32API

    Use method's below to do Mouse Operation's

    void sendMouseRightclick(Point p)
    {
        mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
    }
    
    void sendMouseDoubleClick(Point p)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
    
    Thread.Sleep(150);
    
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
    }
    
    void sendMouseRightDoubleClick(Point p)
    {
        mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
    
        Thread.Sleep(150);
    
        mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
    }
    
    void sendMouseDown()
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN, 50, 50, 0, 0);
    }
    
    void sendMouseUp()
    {
        mouse_event(MOUSEEVENTF_LEFTUP, 50, 50, 0, 0);
    }
    

    If you want to do a Mouse Drag you should First Send MouseDown(Mouse Click) and keep it Clicked While Changing the Mouse Position than Send MouseUp(Release Click) something like this .

    sendMouseDown();
    Cursor.Position = new Point(30,30);
    sendMouseUp();
    

提交回复
热议问题