0

I am trying to remove the scroll bars from an RDP window that my C# code launches. My code launches mstsc.exe passing the appropriate path the the rdp file. But once it's open, my code needs to remove the horizontal and vertical scroll bars. My style that I am using below seems to work for removing the thick frame, removing the min and max buttons, but the WS_HSCROLL and WS_VSCROLL seems to be ignored.

                        Process m_proc = new Process { StartInfo = { FileName = "mstsc.exe", Arguments = rdpFullPath } };
                        m_proc.Start();

                        //Wait until process is started and main rdp window is created.
                        while (m_proc.MainWindowHandle == IntPtr.Zero)
                        {
                            Thread.Sleep(2000);
                        }

                        // Remove thick frame to disallow resizing of the RDP window, and remove minimize and maximize buttons.
                        var currentStyle = UnsafeNativeMethods.GetWindowLong(m_proc.MainWindowHandle, UnsafeNativeMethods.GWL_STYLE);
                        currentStyle &= ~UnsafeNativeMethods.WS_THICKFRAME;
                        currentStyle &= ~UnsafeNativeMethods.WS_MINIMIZEBOX;
                        currentStyle &= ~UnsafeNativeMethods.WS_MAXIMIZEBOX;
                        currentStyle &= ~UnsafeNativeMethods.WS_HSCROLL;
                        currentStyle &= ~UnsafeNativeMethods.WS_VSCROLL;
                        UnsafeNativeMethods.SetWindowLong(m_proc.MainWindowHandle, UnsafeNativeMethods.GWL_STYLE, currentStyle);

UnsafeNativeMethods.SetWindowPos(m_proc.MainWindowHandle,
                                UnsafeNativeMethods.HWND_TOPMOST, 0, 0, 0, 0,
                                UnsafeNativeMethods.SWP_NOMOVE | UnsafeNativeMethods.SWP_NOSIZE | UnsafeNativeMethods.SWP_NOZORDER | UnsafeNativeMethods.SWP_NOOWNERZORDER | UnsafeNativeMethods.SWP_FRAMECHANGED);
                   

I have the following definitions:

    public const int WS_HSCROLL = 0x00100000;
    public const int WS_VSCROLL = 0x00200000;

    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy,
        SetWindowPosFlags uFlags);
Ray
  • 4,679
  • 10
  • 46
  • 92
  • Calling SetWindowPos() when you make changes to the window frame is [required](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptra#remarks) – Hans Passant Feb 16 '23 at 18:32
  • I added the SetWindowPos to my code (see my code above), but it still does not remove scrollbars. I added the call to SetWindowPos to make my window TopMost. Should the call be different? – Ray Feb 16 '23 at 22:02
  • Use SWP_FRAMECHANGED. – Hans Passant Feb 16 '23 at 22:46
  • @HansPassant I added SWP_FRAMECHANGED but I am still getting scroll bars. See my updated code above. – Ray Feb 27 '23 at 22:02
  • i note that you are not checking the return codes of any API call – pm100 Feb 27 '23 at 22:05
  • The call to SetWindowLong is returning 382664960 and the call to SetWindowPos is returning true. – Ray Feb 27 '23 at 22:21

0 Answers0