I want to simulate a mouse click on Form1 at the same position where a real click occurred on Form2. Form1 is transparent and allows clicks to pass through it. I have code that sends the x and y coordinates from Form2 to Form1, and I am successfully receiving these coordinates. Now, I need to programmatically simulate a click on Form1 using these coordinates, but it is not working. And I don’t know is it possible at all? The goal is for Form1 to mimic all actions (clicks, hovers) performed on Form2.
Form2
namespace tryindtosendclicks
{
public partial class Form2 : Form
{
private Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_MouseClick(object sender, MouseEventArgs e)
{
// base.OnMouseClick(e);
// Send the mouse click coordinates to Form1
form1.ReceiveClick(e.X, e.Y);
}
}
}
Form1
namespace tryindtosendclicks
{
public partial class Form1 : Form
{
// Import the necessary Windows API functions
[DllImport("user32.dll")]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
public uint type;
public InputUnion u;
}
[StructLayout(LayoutKind.Explicit)]
private struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
}
[StructLayout(LayoutKind.Sequential)]
private struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
private const uint INPUT_MOUSE = 0;
private const uint MOUSEEVENTF_MOVE = 0x0001;
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
private const int WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
private const int GWL_EXSTYLE = -20;
private const int LWA_ALPHA = 0x2;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
// Make the window transparent and click-through
int initialStyle = GetWindowLong(this.Handle, GWL_EXSTYLE);
SetWindowLong(this.Handle, GWL_EXSTYLE, initialStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT);
SetLayeredWindowAttributes(this.Handle, 0, 128, LWA_ALPHA); // Adjust the transparency
}
// Method to receive mouse click coordinates from Form2
public void ReceiveClick(int x, int y)
{
label2.Text = "Mouse " + x + " and " + y + " are here";
// Set up the input structure for the mouse down event
INPUT mouseDownInput = new INPUT();
mouseDownInput.type = INPUT_MOUSE;
mouseDownInput.u.mi.dx = x;
mouseDownInput.u.mi.dy = y;
mouseDownInput.u.mi.mouseData = 0;
mouseDownInput.u.mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE;
mouseDownInput.u.mi.time = 0;
mouseDownInput.u.mi.dwExtraInfo = IntPtr.Zero;
// Set up the input structure for the mouse up event
INPUT mouseUpInput = new INPUT();
mouseUpInput.type = INPUT_MOUSE;
mouseUpInput.u.mi.dx = x;
mouseUpInput.u.mi.dy = y;
mouseUpInput.u.mi.mouseData = 0;
mouseUpInput.u.mi.dwFlags = MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE;
mouseUpInput.u.mi.time = 0;
mouseUpInput.u.mi.dwExtraInfo = IntPtr.Zero;
// Send the mouse down and up events
INPUT[] inputs = new INPUT[] { mouseDownInput, mouseUpInput };
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
}
}
}