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

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

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

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

CanMinimize
CanMaximize
属性。

有人可以向我指出或提供代码(首选 C#)来说明如何执行此操作吗?

wpf user-interface resize pinvoke
9个回答
126
投票

我偷了一些在 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));
    }
}

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

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

114
投票

一种方法是设置您的

ResizeMode="NoResize"
。它会表现得像这样。 enter image description here


27
投票

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

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

6
投票

如果有人使用 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;
    } 
}

3
投票

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

标记:

<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">

背后代码:

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

3
投票

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

2
投票

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


2
投票

只需使用

WindowStyle="ToolWindow"

它隐藏了最大化和最小化按钮,但仍然可以通过拖动窗口边框来调整窗口大小,并使用任务栏右下角的隐藏按钮最小化。

https://learn.microsoft.com/en-us/dotnet/api/system.windows.window.windowstyle?view=windowsdesktop-6.0


0
投票

您可以将

ResizeMode="CanMinimize"
添加到

<Window x:Class="ModernDesign.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

它并没有完全删除最大化按钮(它只是将其变灰),但最小化和关闭按钮的功能仍然保留。

(如果这个答案令人困惑,我很抱歉,我对 .xaml 还很陌生)

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