Send mouse clicks to X Y coordinate of another application

前端 未结 2 2025
野趣味
野趣味 2021-02-10 21:29

I am trying to send a simulated mouse click to another application. I understand how to actually send the key click, this is not the issue. I need to send the mouse click to the

相关标签:
2条回答
  • 2021-02-10 21:45

    Set the cursor position AND also set 0,0 as X and Y in the mouse_event routine:

    SetCursorPos(x, y);
    mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
    mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
    

    Working fine for me now.

    0 讨论(0)
  • 2021-02-10 21:56

    You can use the GetWindowInfo API:

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
    
        [StructLayout(LayoutKind.Sequential)]
        struct WINDOWINFO
        {
            public uint cbSize;
            public RECT rcWindow;
            public RECT rcClient;
            public uint dwStyle;
            public uint dwExStyle;
            public uint dwWindowStatus;
            public uint cxWindowBorders;
            public uint cyWindowBorders;
            public ushort atomWindowType;
            public ushort wCreatorVersion;
    
            public WINDOWINFO(Boolean? filler)
                : this()   // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
            {
                cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
            }
    
        }
        [StructLayout(LayoutKind.Sequential)]
        struct RECT
        {
            public int left, top, right, bottom;
        }
    
    
        private void button1_Click_1(object sender, EventArgs e)
        {
            var p = System.Diagnostics.Process.GetProcessesByName("mspaint");
    
            if (p.Length == 0) return;
    
            WINDOWINFO wi = new WINDOWINFO(false);
            GetWindowInfo(p[0].MainWindowHandle, ref wi);
    
            SendLeftClick((wi.rcWindow.left + wi.rcWindow.right) / 2, (wi.rcWindow.top + wi.rcWindow.bottom) / 2);
        }
    
    0 讨论(0)
提交回复
热议问题