Search code examples
c#.netwinformsrdptopmost

How to hide topmost window when rdp session is active


I wrote a .NET winform app and set TopMost property to true. But I found that when I loggin to a remote destop, the window is still showing.

How to detect whether I'm in remote destop(remote desktop is active, typing/working in remote desktop) using .net framework? I am using .NET framework 4.8 and remote to Windows 10 1809 from Windows 10 1903


Solution

  • You probably have to call the Windows API with P/Invoke to achieve this. I could imagine it working roughly like this:

    1. Monitor the EVENT_SYSTEM_FOREGROUND event.

      The foreground window has changed. The system sends this event even if the foreground window has changed to another window in the same thread. [Source]

    2. When the foreground window has changed, check whether the new foreground window belongs to the RDP application. You could do this by various means, for example by checking which process the window belongs to (I assume it would be mstsc.exe on Windows but could be wrong). Even easier (but dirty and error prone) would be to check the title of the window.
    3. If the RDP application is in the foreground, push your application behind it.

    Here's a good starting point on how to implement what I explained above: Detect active window changed using C# without polling.