1
I’m like a problem to use the functions SendMessage
and PostMessage
in C# to simulate clicks mouse in a minimized program (in the background).
I used this code but it didn’t work, I believe it’s the makeparam
.
public enum WMessages : int
{
WM_LBUTTONDOWN = 0X201,
WM_LBUTTONUP = 0x202,
WM_LBUTTONBCLICK = 0x203,
WM_RBUTTONDOWN = 0x204,
WM_RBUTTONUP = 0x205,
WM_BUTTONBCLICK = 0x206,
}
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowsEx(IntPtr parentHendle, IntPtr childAfter, string className, IntPtr windowsTitle);
[DllImport("user32.dll")]
public static extern IntPtr FindWindows(String sClassName, String sAppName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int SetActiveWindows(IntPtr hwnd);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
public static IntPtr findme()
{
Process k = Process.GetProcessesByName("Nome do programa minimizado")[0];
return k.MainWindowHandle;
}
private static int MAKELPARAM(int p, int p_2)
{
return ((p_2 << 16) | (p & 0xFFFF));
}
public static void clickMouseLeft(int x, int y)
{
IntPtr myHandle = findme();
Point pt = new Point(x, y);
IntPtr Handle = WindowFromPoint(pt.X, pt.Y);
int lnglParam = (pt.Y * 0x10000) + pt.X;
SetForegroundWindow(myHandle);
SendMessage(Handle, (int)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(pt.X,pt.Y));
SendMessage(Handle, (int)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(pt.X, pt.Y));
}
This code below is a code I made in C++ that worked perfectly
cursorPos.x = ui->lineEdit_x->text().toInt();
cursorPos.y = ui->lineEdit_y->text().toInt();
HWND hwnd = FindWindow(NULL, L"Nome do programa minimizado");
DWORD dw = MAKEWORD(cursorPos.x, cursorPos.y);
SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, dw);
SendMessage(hwnd, WM_LBUTTONUP, MK_LBUTTON, dw);
I have tried, only the program needs is in the foreground, and in my case there is no way. So I have to use the function Sendmessage or Postmessage.
– Igor Bittencourt