Send mouse clicks to X Y coordinate of another application

前端 未结 2 1245
生来不讨喜
生来不讨喜 2021-02-10 21:27

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 22:00

    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);
        }
    

提交回复
热议问题