I want to send mouse click with SendMessage but it's not working, What wrong with my code?
Int window is not 0 but it still not working.
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
private const uint WM_RBUTTONDOWN = 0x0204;
private const uint WM_RBUTTONUP = 0x0205;
private int MAKELPARAM(int p, int p_2)
{
return ((p_2 << 16) | (p & 0xFFFF));
}
public void ClickOnPoint(System.Drawing.Point p)
{
int window = FindWindow(null, "Untitled - Notepad");
//System.Windows.Forms.MessageBox.Show(window + "\n" );
SendMessage(window, WM_RBUTTONDOWN, 0, MAKELPARAM(500,500));
SendMessage(window, WM_RBUTTONUP, 0, MAKELPARAM(500, 500));
}
The "textarea" is a child window of the notepad application's main window.
(Screenshot taken on Windows 10 using Spy++)
We need the window handle of that child window, which has no caption and the "EDIT" class, and send the mouse click messages to that child window to bring up its context menu.
Use this answer to obtain the child window handle from the one you already got.
Be sure to call the API functions with valid handles only:
Warning: This is implementation specific. You can never know if notepad will continue to be built like this in future versions. Although, IMO, of all Windows applications it is probably the most likely one to remain unchanged.