简体   繁体   English

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

[英]How to remove minimize and maximize buttons from a resizable window?

WPF doesn't provide the ability to have a window that allows resize but doesn't have maximize or minimize buttons. WPF 不提供具有允许调整大小但没有最大化或最小化按钮的窗口的能力。 I'd like to able to make such a window so I can have resizable dialog boxes.我希望能够制作这样一个窗口,这样我就可以拥有可调整大小的对话框。

I'm aware the solution will mean using pinvoke but I'm not sure what to call and how.我知道解决方案将意味着使用 pinvoke,但我不确定该调用什么以及如何调用。 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 CanMinimize and CanMaximize properties on its windows.搜索 pinvoke.net 并没有发现任何我需要的东西,主要是因为 Windows 窗体确实在其窗口上提供了CanMinimizeCanMaximize属性。

Could someone point me towards or provide code (C# preferred) on how to do this?有人可以指点我或提供有关如何执行此操作的代码(首选 C#)吗?

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!希望这可以帮助!

One way is to set your ResizeMode="NoResize" .一种方法是设置您的ResizeMode="NoResize" It will behave like this.它会表现得像这样。在此处输入图像描述

I hope this helps!我希望这有帮助!

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

If anyone use Devexpress window (DXWindow) accepted answer doesn't work.如果有人使用 Devexpress 窗口 (DXWindow) 接受的答案不起作用。 One ugly approach is一种丑陋的方法是

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

This variant of the solution proposed by @MattHamilton can (and must) be called in the constructor of the Window. @MattHamilton提出的解决方案的这种变体可以(并且必须)在 Window 的构造函数中调用。 The trick is to subscribe a delegate to the SourceInitialized event within the extension method.诀窍是在扩展方法中订阅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);
    };
}

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

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

Just use只需使用

WindowStyle="ToolWindow"

It hides the maximize and minimize buttons, but the window can still be resized by dragging the window borders and minimize using the hide button in the bottom right corner of the taskbar.它隐藏了最大化和最小化按钮,但仍然可以通过拖动窗口边框来调整窗口大小,并使用任务栏右下角的隐藏按钮进行最小化。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM