问题描述
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 Forms 确实提供了 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();
};
希望这会有所帮助!
这篇关于如何从可调整大小的窗口中删除最小化和最大化按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!