0

I have code that will send left and right clicks to applications running locally in the background and it works flawlessly. I am trying to manage an application on another machine using TeamViewer running in the background. Even when all that is showing in the TeamViewer window is Firefox, I can't send a right click. Is there a way to tell if SendMessage is even sending a click to TeamViewer?

private void BtnRightClickTeamViewer_Click(object sender, EventArgs e)
{
    IntPtr handle = WinGetHandle("voyager");
    if (handle != IntPtr.Zero)
    {
        MoveWindow(handle, MainOffsetX, MainOffsetY, HorizontalPixels, VerticalPixels, true);
        int X = Convert.ToInt32(TxtX.Text);
        int Y = Convert.ToInt32(TxtY.Text);
        Point clickHere = new Point(X, Y);
        var panelParent = FindWindowEx(handle, IntPtr.Zero, "ATL:00007FF74F803610", null);
        var panelGrandChild = FindWindowEx(panelParent, IntPtr.Zero, "TV_REMOTEDESKTOP_CLASS", null);
        DoRightClick(panelGrandChild, clickHere);
    }
}

void DoRightClick(IntPtr hwnd, Point point)
{
    var pointPtr = MakeLParam(point.X, point.Y);
    SendMessage(hwnd, WM_MOUSEMOVE, IntPtr.Zero, pointPtr);
    SendMessage(hwnd, WM_RBUTTONDOWN, IntPtr.Zero, pointPtr);
    SendMessage(hwnd, WM_RBUTTONUP, IntPtr.Zero, pointPtr);
}
Patrick
  • 351
  • 1
  • 6
  • 20
  • 1
    _[`SendMessage` - The return value specifies the result of the message processing; it depends on the message sent](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage#return-value)_; _["`WM_RBUTTONDOWN` - If an application processes this message, it should return zero."](https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-rbuttondown#return-value)_, MSDN –  Jan 24 '22 at 07:50
  • 2
    Ever since Windows Vista apps are encouraged not to rely on posting messages to other processes due to possible blocks from elevated processes. Either way, consider using _Microsoft UI Automation_ instead. It exposes a nicer object model and works with native and web apps. –  Jan 24 '22 at 08:00
  • Can I suggest you find a more reliable inter-process communication system than sending clicks via TeamViewer? – Charlieface Jan 24 '22 at 09:33
  • Thanks for the suggestion of Microsoft UI Automation and for answering my question MickyD. I can't find any examples of Microsoft UI Automation working in background so I'll do up a small test project now and see. – Patrick Jan 24 '22 at 11:20
  • @Patrick [here's a blatant self-promotion on my part for a MS UIA example](https://stackoverflow.com/a/51625994/585968) :) Also [this](https://stackoverflow.com/a/38219848/585968) and [this groovy one](https://stackoverflow.com/a/41993816/585968) –  Jan 25 '22 at 03:20
  • I tried your examples MickyD and all I get is "The type or namespace name 'Automation' does not exist in the namespace 'System.Windows' " I can't find a Nuget for it. Am I missing something? – Patrick Jan 26 '22 at 07:33

0 Answers0