1

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));
    }
KPTH
  • 91
  • 1
  • 2
  • 12
  • how do you detect if it is working or not? do you have a notepad window at (500, 500) and expect it to show its context menu? Be aware that the coordinates are relative to the client coordinates of the window, not absolute screen coordinates. you could use a tool like Windows Spy to see if the messages are received. Also: [multi-monitor caveats](https://msdn.microsoft.com/de-de/library/windows/desktop/ms646242(v=vs.85).aspx) to consider. – Cee McSharpface Sep 19 '17 at 18:02
  • I check window of notepad(fullscreen) at (500,500) is textarea. On my code it must to show menubox (undo,cut,copy,paste,delete,..etc) but it's not show. – KPTH Sep 19 '17 at 18:09
  • is the return value of `SedMessage` zero (indicating successful delivery of the message)? If it is nonzero, call `GetLastError`. [this answer](https://stackoverflow.com/a/3721053/1132334) suggests that the wParam value should be MK_RBUTTON instead of 0. – Cee McSharpface Sep 19 '17 at 18:15
  • I use int x = SendMessage(window, WM_RBUTTONDOWN ,0, MAKELPARAM(500,500)); and after call my function x = 0 – KPTH Sep 19 '17 at 18:23
  • You are sending it the wrong window. The editable area of Notepad that can respond to a right-click is a child of the window that FindWindow returned. Use the Spy++ utility to get insight. – Hans Passant Sep 19 '17 at 18:55

2 Answers2

1

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

KPTH
  • 91
  • 1
  • 2
  • 12
0

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.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77