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

Wpf 如何从可调整大小的窗口中删除最小化和最大化按钮?,wpf,user-interface,resize,pinvoke,Wpf,User Interface,Resize,Pinvoke,WPF不提供允许调整大小但没有最大化或最小化按钮的窗口。我想能够使这样一个窗口,这样我就可以有大小可调的对话框 我知道解决方案将意味着使用pinvoke,但我不确定该调用什么以及如何调用。搜索pinvoke.net并没有发现任何我所需要的东西,主要是因为Windows窗体确实在其Windows上提供了CanMinimize和canmaximized属性 有人能告诉我怎么做或提供代码(C#首选)吗?我偷了一些在MSDN论坛上找到的代码,并在Window类上做了一个扩展方法,如下所示: intern

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

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


有人能告诉我怎么做或提供代码(C#首选)吗?

我偷了一些在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();
};
希望这有帮助

不知道这是否适用于您的请求。视觉上。。这是

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


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

我正在使用一个解决方案。请注意,“最大化”按钮仍然显示

标记:

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

一种方法是设置您的
ResizeMode=“NoResize”
。它的行为将是这样的。


我希望这有帮助

如果有人使用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@matt的这个变量。诀窍是在扩展方法中订阅
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);
    };
}
private const int GWL_STYLE=-16,WS_max=0x10000,WS_max=0x20000;
[DllImport(“user32.dll”)]
外部私有静态int GetWindowLong(IntPtr hwnd,int index);
[DllImport(“user32.dll”)]
外部私有静态int SetWindowLong(IntPtr hwnd、int索引、int值);
/// 
///隐藏窗口中的最小化和最大化按钮。必须在构造函数中调用。
/// 
///将隐藏其最小化/最大化按钮的窗口。
公共静态无效隐藏最小化和最大化按钮(此窗口)
{
window.SourceInitialized+=(s,e)=>{
IntPtr hwnd=new System.Windows.Interop.WindowInteropHelper(window.Handle);
int currentStyle=GetWindowLong(hwnd,GWL_样式);
SetWindowLong(hwnd、GWL_样式、currentStyle&~WS_MAXIMIZEBOX&~WS_MINIMIZEBOX);
};
}


这几乎可以实现,但如果双击标题栏,右键单击并使用控制菜单或任务栏按钮(如果可用),仍然可以最大化或最小化窗口。当然,它看起来像一个工具窗口,而不是一个普通的窗口。对。。。但是,这个约束看起来很奇怪,用户不允许最大化,但可以通过调整大小手动拖动并放大窗口。但这是你的窗户。。您的规则:)在设置了窗口的
MaxWidth
和/或
MaxHeight
的情况下,这可能很方便。窗口中有一个更漂亮的代码:protected override void OnSourceInitialized(EventArgs e){base.OnSourceInitialized(e);this.HideMinimizeAndMaximizeButtons()}为什么不让它更简单,并在hideMinizeAndMaximizeButtons()方法中订阅SourceInitialized事件?然后你可以从构造函数中调用这个方法,而不必做任何其他事情。这个解决方案不包括双击标题栏。@Lonli Lokli实际上,它包括。当使用Win32 API隐藏按钮时,它也会禁用窗口上下文菜单选项并双击标题。在知道windows窗体中存在此功能之后,我不明白microsoft从一开始就没有将此类功能放在WPF中。坦率地说,带有最小化框和最大化书本的对话框看起来不专业,双击标题栏也是同样的问题。很抱歉让我对WPF有点失望,它很好,有字符串资产,但偶尔你会发现一些简单的东西,但最终不是。呃,但是你不能调整它的大小,应该可以。这会使窗口无法调整大小,这与问题正好相反。一般来说,谷歌搜索者在寻找一种禁用最小化和最大化按钮的方法时,并没有对窗口大小进行调整的具体要求。目前这是“wpf窗口禁用最小化”查询的最高结果,此答案正确回答了问题。不管怎样,微软并没有将标题栏或整个窗口的chrome作为“仅仅是另一个控件或属性”而感到羞耻。90年代遗留下来的东西太多了……在这种情况下,具有动态宽度的元素(例如DockPanel)仍然能够更改窗口大小。但不再是用户。所以,这个选项实际上满足了我的要求。没有回答问题,但对我很有帮助。谢谢。@ TrimoRoRoiii,同时在这一页上可能已经是谷歌的最高结果,绝对不意味着这个答案是最好的,当你认为它没有解决“如何删除最小化和最大化从<代码>可调整窗口< /代码>在WPF?”时,它从成品中删除“可调整”的方面。这就像帮助一个想把车漆成红色的人,在这个过程中你拆下了引擎。他问如何隐藏按钮,但保留功能。尽管如此,车窗还是在闪烁。不太好。在我注意到你的答案之前,我可能读了五篇不同的帖子。这确实管用,但很恶心。你有没有找到更好的方法?
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);
    };
}