禁用最小化按钮,但保留交叉和最大化按钮 - WPF、C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16381933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:43:46  来源:igfitidea点击:

Disable Minimize Button, but Keep Cross and Maximize Buttons - WPF, C#

c#wpf

提问by Bubbled86

I'd like to know how to disable the Minimize button, but keep the Maximize/Restore button and the Close button (the red "X").

我想知道如何禁用最小化按钮,但保留最大化/恢复按钮和关闭按钮(红色“X”)。

Here's an image of what I want my window's buttons on the top-right to look like:

这是我希望窗口右上角的按钮看起来像的图像:

enter image description here

在此处输入图片说明

回答by Blablablaster

You may need to use PInvoke here. Basically you're importing SetWindowLong and GetWindowLong functions and setting corresponding flags to Win API window using it's handle(hwnd)

您可能需要在此处使用 PInvoke。基本上,您正在导入 SetWindowLong 和 GetWindowLong 函数并使用它的句柄(hwnd)为 Win API 窗口设置相应的标志

private const int GWL_STYLE = -16;
private const int WS_MINIMIZE = -131073;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

private static void CanMinimize(Window w)
{
  var hwnd = new WindowInteropHelper(w).Handle;
  long value = GetWindowLong(hwnd, GWL_STYLE);
  SetWindowLong(hwnd, GWL_STYLE, (int)(value & ~WS_MINIMIZE));
} 

回答by Mal Ross

Blablablaster is basically right -- you need to P/Invoke a couple of Windows API calls -- but the following TechNet article also describes when/where you should make the call to the Windows API:

Blablablaster 基本上是正确的——你需要 P/Invoke 几个 Windows API 调用——但下面的 TechNet 文章还描述了你应该何时/何地调用 Windows API:

WPF: Disabling or Hiding the Minimize, Maximize or Close Button Of a Window

WPF:禁用或隐藏窗口的最小化、最大化或关闭按钮

Hope this helps.

希望这可以帮助。

回答by Rob

Here is how I do it

这是我如何做到的

Private Sub WindowAddChange_StateChanged(sender As Object, e As EventArgs) Handles Me.StateChanged

    If sender.windowstate = WindowState.Minimized Then
        Me.WindowState = WindowState.Normal
    End If

End Sub

回答by Tanuj Wadhwa

I don't think WPF provides a way to disable just the minimize button. What you can do is disable the complete title bar and create a custom title bar for yourself. Check thisout.

我不认为 WPF 提供了一种仅禁用最小化按钮的方法。您可以做的是禁用完整的标题栏并为自己创建自定义标题栏。看看这个