WPF不提供具有允许调整大小但没有最大化或最小化按钮的窗口的功能。我希望能够制作一个这样的窗口,以便可以调整对话框的大小。
我知道该解决方案将意味着使用pinvoke,但我不确定该如何呼叫。搜索pinvoke.net并没有发现我需要的任何东西,主要是我确定,因为Windows窗体确实在其窗口中提供CanMinimize
和CanMaximize
属性。
有人可以指向我或提供有关如何执行此操作的代码(首选C#)吗?
Answers:
我偷了一些在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();
};
希望这可以帮助!
一种方法是设置您的帐户ResizeMode="NoResize"
。它将像这样。
我希望这有帮助!
resizable window
WPF中删除最小化和最大化?”时,此答案是最佳的。当删除了“调整大小”从最终产品方面。这就像在帮助想要将其汽车涂成红色的人,并在此过程中卸下引擎。
如果有人使用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;
}
}
这是我正在使用的解决方案。请注意,最大化按钮仍然显示。
标记:
<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">
后面的代码:
// Disable maximizing this window
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Maximized)
this.WindowState = WindowState.Normal;
}
@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);
};
}