禁用/隐藏其他应用程序窗口上的关闭、最大化、最小化按钮(SetWindowLong 不起作用)

问题描述 投票:0回答:1

我能够使用此 C# 代码删除包含 Windows 应用程序的关闭、最小化、最大化的整个部分

        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);
                }
            }
        }

这适用于记事本、vlc 等某些应用程序。但是其他一些应用程序(例如 Windows 计算器)即使没有抛出异常,也不会对此做出响应。

我尝试将上面的代码用于某些应用程序,例如记事本和 vlc 媒体播放器,并且运行良好。但对于某些应用程序(例如 Chrome、计算器等),它不起作用。我希望这段代码适用于所有应用程序。

c# windows winapi window win32-process
1个回答
0
投票

你不能这样做,也不应该尝试。

修改窗口样式仅影响关心这些样式的窗口。本质上,只有沼泽标准的本机窗口,没有任何任何自定义。

其他人对于他们不关心观察的变化不会有任何印象。这包括所有对其“非客户端”区域执行自定义渲染的本机窗口,以及基于窗口框架(例如 WinUI)的所有内容,这些框架不让系统处理非客户端区域的所有方面。

您试图解决的“问题”没有通用的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.