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