如何从 WPF 中可调整大小的窗口中删除最小化和最大化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/339620/
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-08 19:50:37  来源:igfitidea点击:

How do I remove minimize and maximize from a resizable window in WPF?

wpfuser-interfaceresizepinvoke

提问by Nidonocu

WPF doesn't provide the ability to have a window that allows resize but doesn't have maximize or minimize buttons. I'd like to able to make such a window so I can have resizable dialog boxes.

WPF 不提供允许调整大小但没有最大化或最小化按钮的窗口的功能。我希望能够制作这样一个窗口,以便我可以拥有可调整大小的对话框。

I'm aware the solution will mean using pinvoke but I'm not sure what to call and how. A search of pinvoke.net didn't turn up any thing that jumped out at me as what I needed, mainly I'm sure because Windows Forms does provide the CanMinimizeand CanMaximizeproperties on its windows.

我知道该解决方案将意味着使用 pinvoke,但我不确定该调用什么以及如何调用。搜索 pinvoke.net 并没有发现任何我需要的东西,主要是我确定因为 Windows 窗体确实在其窗口上提供了CanMinimizeCanMaximize属性。

Could someone point me towards or provide code (C# preferred) on how to do this?

有人可以指点我或提供有关如何执行此操作的代码(首选 C#)吗?

回答by Matt Hamilton

I've stolen some code I found on the MSDN forums and made an extension method on the Window class, like this:

我偷了一些在 MSDN 论坛上找到的代码,并在 Window 类上创建了一个扩展方法,如下所示:

internal static class WindowExtensions
{
    // from winuser.h
    private const int GWL_STYLE      = -16,
                      WS_MAXIMIZEBOX = 0x10000,
                      WS_MINIMIZEBOX = 0x20000;

    [DllImport("user32.dll")]
    extern private static int GetWindowLong(IntPtr hwnd, int index);

    [DllImport("user32.dll")]
    extern private static int SetWindowLong(IntPtr hwnd, int index, int value);

    internal static void HideMinimizeAndMaximizeButtons(this Window window)
    {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
        var currentStyle = GetWindowLong(hwnd, GWL_STYLE);

        SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX));
    }
}

The only other thing to remember is that for some reason this doesn't work from a window's constructor. I got around that by chucking this into the constructor:

唯一要记住的另一件事是,由于某种原因,这在窗口的构造函数中不起作用。我通过将它放入构造函数来解决这个问题:

this.SourceInitialized += (x, y) =>
{
    this.HideMinimizeAndMaximizeButtons();
};

Hope this helps!

希望这可以帮助!

回答by Musikero31

One way is to set your ResizeMode="NoResize". It will behave like this. enter image description here

一种方法是设置您的ResizeMode="NoResize". 它会像这样。 在此处输入图片说明

I hope this helps!

我希望这有帮助!

回答by Gishu

Don't know if this works for your req. visually.. This is

不知道这是否适合您的要求。视觉上..这是

<Window x:Class="DataBinding.MyWindow" ...Title="MyWindow" Height="300" Width="300" 
    WindowStyle="ToolWindow" ResizeMode="CanResizeWithGrip">

回答by Eldar

If anyone use Devexpress window (DXWindow) accepted answer doesn't work. One ugly approach is

如果有人使用 Devexpress 窗口 (DXWindow),则接受的答案不起作用。一种丑陋的方法是

public partial class MyAwesomeWindow : DXWindow
{
    public MyAwesomeWIndow()
    {
       Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        // hides maximize button            
        Button button = (Button)DevExpress.Xpf.Core.Native.LayoutHelper.FindElementByName(this, DXWindow.ButtonParts.PART_Maximize.ToString());
        button.IsHitTestVisible = false;
        button.Opacity = 0;

        // hides minimize button
        button = (Button)DevExpress.Xpf.Core.Native.LayoutHelper.FindElementByName(this, DXWindow.ButtonParts.PART_Minimize.ToString());
        button.IsHitTestVisible = false;
        button.Opacity = 0;

        // hides close button
        button = (Button)DevExpress.Xpf.Core.Native.LayoutHelper.FindElementByName(this, DXWindow.ButtonParts.PART_CloseButton.ToString());
        button.IsHitTestVisible = false;
        button.Opacity = 0;
    } 
}

回答by Andrej

Here's a solution I'm using. Note that maximize button is still displayed.

这是我正在使用的解决方案。请注意,最大化按钮仍会显示。

Markup:

标记:

<Window x:Class="Example"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Example"
        StateChanged="Window_StateChanged">

Code behind:

后面的代码:

// Disable maximizing this window
private void Window_StateChanged(object sender, EventArgs e)
{
    if (this.WindowState == WindowState.Maximized)
        this.WindowState = WindowState.Normal;
}

回答by Chinmay Behera

You can set the ResizeMode="NoResize" of the window if you want to remove Minimize and Maximize button

如果要删除最小化和最大化按钮,可以设置窗口的 ResizeMode="NoResize"

回答by Olivier Jacot-Descombes

This variant of the solution proposedby @MattHamilton can (and must) be called in the constructor of the Window. The trick is to subscribe a delegate to the SourceInitializedevent within the extension method.

@MattHamilton提出解决方案的这个变体可以(并且必须)在 Window 的构造函数中调用。诀窍是SourceInitialized在扩展方法中为事件订阅一个委托。

private const int GWL_STYLE = -16, WS_MAXIMIZEBOX = 0x10000, WS_MINIMIZEBOX = 0x20000;

[DllImport("user32.dll")]
extern private static int GetWindowLong(IntPtr hwnd, int index);

[DllImport("user32.dll")]
extern private static int SetWindowLong(IntPtr hwnd, int index, int value);

/// <summary>
/// Hides the Minimize and Maximize buttons in a Window. Must be called in the constructor.
/// </summary>
/// <param name="window">The Window whose Minimize/Maximize buttons will be hidden.</param>
public static void HideMinimizeAndMaximizeButtons(this Window window)
{
    window.SourceInitialized += (s, e) => {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
        int currentStyle = GetWindowLong(hwnd, GWL_STYLE);

        SetWindowLong(hwnd, GWL_STYLE, currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
    };
}