如何从WPF中可调整大小的窗口中删除最小化和最大化? [英] How do I remove minimize and maximize from a resizable window in WPF?
问题描述
WPF不提供具有允许调整大小但没有最大化或最小化按钮的窗口的功能.我希望能够创建一个这样的窗口,以便可以设置可调整大小的对话框.
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.
我知道该解决方案将意味着使用pinvoke,但我不确定该调用什么以及如何调用.搜索pinvoke.net并没有发现我需要的任何东西,主要是因为Windows窗体确实在其窗口上提供了CanMinimize
和CanMaximize
属性.
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 CanMinimize
and CanMaximize
properties on its windows.
有人可以指向我或提供有关如何执行此操作的代码(首选C#)吗?
Could someone point me towards or provide code (C# preferred) on how to do this?
推荐答案
我偷了一些在MSDN论坛上找到的代码,并在Window类上做了一个扩展方法,像这样:
I've stolen some code I found on the MSDN forums and made an extension method on the Window class, like this:
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();
};
希望这会有所帮助!
这篇关于如何从WPF中可调整大小的窗口中删除最小化和最大化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!