question

NadeemBader-5701 avatar image
0 Votes"
NadeemBader-5701 asked NadeemBader-5701 answered

c# - Unable to restore and resize the RDP instance

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

dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@NadeemBader-5701 Judging from my current testing, this seems impossible.
As long as the RDP instance is in the full screen state, the code cannot change its size.
My thought is, can we close the current rdp, and then use Process to call the mstsc command to open a new non-full-screen instance?
At this point, we can use MoveWindow to modify its position and size. Is this acceptable?

0 Votes 0 ·

Thank you for reply. In my code, I do run the mstsc using Process.
But that doesn't work

0 Votes 0 ·

@NadeemBader-5701
Are you unable to create an RDP instance using Process?
These codes can work for me:

             Process process = new Process()
             {
                 StartInfo = new ProcessStartInfo()
                 {
                     FileName = "mstsc.exe",
                     Arguments = @"C:\xxx\Desktop\VM.rdp /w: 500 /h: 300"
                 }
             };
             process.Start();

In my test, as long as the RDP instance is not full screen, the MoveWindow function will work. Is it different for you now?

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered

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;



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NadeemBader-5701 avatar image
0 Votes"
NadeemBader-5701 answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.