Anandu Ramachandran
Anandu Ramachandran

Reputation: 23

Disable/hide the close, maximize, minimize buttons on other applications' windows (SetWindowLong not working)

I am able to remove the whole section containing the close, minimize, maximize of a windows application using this C# code

        private const int SWP_NOMOVE = 0x0002;
        private const int SWP_NOSIZE = 0x0001;
        private const int SWP_FRAMECHANGED = 0x0020;
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        private const int WS_CAPTION = 0xC00000;

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

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
public void RemoveMenuAndTitle(IntPtr hwnd)
        {
            if (hwnd != IntPtr.Zero)
            {
                IntPtr stylePtr;

                if (IntPtr.Size == 4) // 32-bit system
                {
                    stylePtr = new IntPtr(GetWindowLongPtr(hwnd, GWL_STYLE).ToInt32());
                }
                else // 64-bit system
                {
                    stylePtr = GetWindowLongPtr(hwnd, GWL_STYLE);
                }

                long style = stylePtr.ToInt64();

                style &= ~(WS_SYSMENU | WS_CAPTION);

                // Set the modified style back to the window
                IntPtr result = (IntPtr)SetWindowLongPtr(hwnd, GWL_STYLE, new IntPtr(style));

                if (result == IntPtr.Zero)
                {
                    int error = Marshal.GetLastWin32Error();
                    Console.WriteLine("SetWindowLongPtr failed with error code: {0}", error);
                }
                else
                {
                    // Force a redraw of the window
                    SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
                }
            }
        }

This works for some applications like notepad, vlc. But some other applications like the windows calculator does not respond to this even though no exceptions are being thrown.

I tried using the above code for some applications like notepad and vlc media player and it was working fine. While for some applications like chrome, calculator etc, it is not working. I am excpecting that this code would work for all appplications.

Upvotes: 1

Views: 164

Answers (1)

IInspectable
IInspectable

Reputation: 51506

You cannot do this and you should not be trying anyway.

Modifying window styles affects only windows that care about those styles. In essence, only bog-standard native windows without any customization whatsoever.

Everyone else will be unimpressed by a change they do not care to observe. That includes all native windows that perform custom rendering of their "non-client" area and everything based on windowing frameworks (such as WinUI) that do not have the system handle all aspects of the non-client area.

The "problem" you are trying to solve has no general solution.

Upvotes: 0

Related Questions