Estou como um problema para usar as funções SendMessage
e PostMessage
em C# para simular clicks do mouse em um programa minimizado(em background).
Usei esse código mas não deu certo, acredito que seja o 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));
}
Este código abaixo e um código que eu fiz em C++ que funcionou perfeitamente
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);