c# - Unable to restore and resize the RDP instance

Nadeem Bader 21 Reputation points
2021-10-06T21:07:44.033+00:00

Hi

Assuming I already have an opened RDP instance for a machine...
I'm trying to resize it and put it is specific location on window using the below code:

ShowWindow(mstscInstance.MainWindowHandle, SW_RESTORE);
SetForegroundWindow(mstscInstance.MainWindowHandle);
MoveWindow(mstscInstance.MainWindowHandle, machine.Left, machine.Top, machine.Width, machine.Height, true);

But, unfortunately it is not working (The RDP instance still maximized).
Can you please assist?

Best Regards,
Nadeem Bader

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,197 questions
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2021-10-07T08:01:01.99+00:00

    WM_SYSCOMMAND works for me (Windows 10, 1909) :

    Before MoveWindow :
    (mstscInstance.MainWindowHandle is "TscShellContainerClass" class on my OS)

    PostMessage(mstscInstance.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, IntPtr.Zero); 
    

    with :

        [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
    
        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_CLOSE = 0xF060;
        public const int SC_RESTORE = 0xF120;
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Nadeem Bader 21 Reputation points
    2021-10-07T16:38:28.827+00:00

    Hi

    Thank you TimonYang and Castorix31 for your help
    The solution which provided by Castorix31 fixed the described issue.

    After placing the rdp instance in the required position, I'm wonder if it is possible to allow the following operations manually (Only):
    * Maximize window
    * Restore
    All other options, such as moving the RDP instance, minimizing it and resizing it should be disabled.

    Best Regards,
    Nadeem Bader

    0 comments No comments