I want to send mouse click with SendMessage but it's not working, What wrong with my code?

7.8k Views Asked by At

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));
    }
2

There are 2 best solutions below

0
On

The "textarea" is a child window of the notepad application's main window.

enter image description here

(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:

if(IsWindow(hWndChild))
{
    SendMessage(hWndChild, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(p.X, p.Y));
    SendMessage(hWndChild, WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM(p.X, p.Y));
}

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.

0
On

I Try to do this.

  1. I use spy++ find Handle(Red circle) of Edit Area in editor area of Notepad (Red rectangle) enter image description here
  2. From 1. I use Handle from Red Circle in argument of SendMessage enter image description here

Do i correctly understand? After i click button .. my notepad not update anything.

...This work for meenter image description here