I use C#, i have an app with a button1, button2 and a panel1, if i click to button1 it open an external program to panel1, then if i click to button2 it send a background click to the window, works fine. But the problem is this click position is not relative to the panel and not go to right place. It send the click where are the cursor actually.
Code pieces what i use for that:
PerformRightClick(proc.MainWindowHandle, new Point(54, 42));
void PerformRightClick(IntPtr hwnd, Point point)
{
var pointPtr = MakeLParam(point.X, point.Y);
SendMessage(hwnd, WM_MOUSEMOVE, IntPtr.Zero, pointPtr);
SendMessage(hwnd, WM_RBUTTONDOWN, IntPtr.Zero, pointPtr);
SendMessage(hwnd, WM_RBUTTONUP, IntPtr.Zero, pointPtr);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
IntPtr MakeLParam(int x, int y) => (IntPtr)((y << 16) | (x & 0xFFFF));
Based on my test, we need to use panel.Handle
when we want to send click to panel.
Here is a code example you can refer to.
private const Int32 WM_MOUSEMOVE = 0x0200;
private const Int32 WM_RBUTTONDOWN = 0x0204;
private const Int32 WM_RBUTTONUP = 0x0205;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
IntPtr MakeLParam(int x, int y) => (IntPtr)((y << 16) | (x & 0xFFFF));
private void button1_Click(object sender, EventArgs e)
{
PerformRightClick(panel1.Handle, new Point(20, 30));
}
void PerformRightClick(IntPtr hwnd, Point point)
{
var pointPtr = MakeLParam(point.X, point.Y);
SendMessage(hwnd, WM_MOUSEMOVE, IntPtr.Zero, pointPtr);
SendMessage(hwnd, WM_RBUTTONDOWN, IntPtr.Zero, pointPtr);
SendMessage(hwnd, WM_RBUTTONUP, IntPtr.Zero, pointPtr);
}
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("Panel is clicked");
}
private void button2_Click(object sender, EventArgs e)
{
PerformRightClick(this.Handle, new Point(20, 30));
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("Form is clicked");
}
Pic:
Like the above picture, when we click button1, the panel is clicked and the Form is clicked when we click button2.