如何从 WPF 中可调整大小的窗口中删除最小化和最大化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/339620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove minimize and maximize from a resizable window in WPF?
提问by Nidonocu
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.
WPF 不提供允许调整大小但没有最大化或最小化按钮的窗口的功能。我希望能够制作这样一个窗口,以便我可以拥有可调整大小的对话框。
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.
我知道该解决方案将意味着使用 pinvoke,但我不确定该调用什么以及如何调用。搜索 pinvoke.net 并没有发现任何我需要的东西,主要是我确定因为 Windows 窗体确实在其窗口上提供了CanMinimize
和CanMaximize
属性。
Could someone point me towards or provide code (C# preferred) on how to do this?
有人可以指点我或提供有关如何执行此操作的代码(首选 C#)吗?
回答by Matt Hamilton
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!
希望这可以帮助!
回答by Musikero31
One way is to set your ResizeMode="NoResize"
. It will behave like this.
一种方法是设置您的ResizeMode="NoResize"
. 它会像这样。
I hope this helps!
我希望这有帮助!
回答by Gishu
回答by Eldar
If anyone use Devexpress window (DXWindow) accepted answer doesn't work. One ugly approach is
如果有人使用 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 Andrej
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;
}
回答by Chinmay Behera
You can set the ResizeMode="NoResize" of the window if you want to remove Minimize and Maximize button
如果要删除最小化和最大化按钮,可以设置窗口的 ResizeMode="NoResize"
回答by Olivier Jacot-Descombes
This variant of the solution proposedby @MattHamilton can (and must) be called in the constructor of the Window. The trick is to subscribe a delegate to the SourceInitialized
event within the extension method.
@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);
};
}