双击标题禁用最大化 WPF 窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16074229/
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
Disable maximizing WPF window on double click on the caption
提问by Alex Klaus
How to disable maximizing WPF window on double click on the caption and leave resizing available?
如何在双击标题时禁用最大化 WPF 窗口并保持调整大小可用?
I know that ResizeModedisables maximizing, but it also prevents resizing the form
我知道ResizeMode禁用最大化,但它也阻止调整表单大小
ResizeMode="CanMinimize"
I know how to remove maximize and minimize buttons, but it's still possible to maximize by double click on the caption.
我知道如何删除最大化和最小化按钮,但仍然可以通过双击标题来最大化。
In WinForms it can be achieved easily. Just set FormBorderStylefrom Noneto FixedSingleor Fixed3D. But it's not an option in WPF any more.
在 WinForms 中,它可以轻松实现。只需将FormBorderStyle从None设置为FixedSingle或Fixed3D。但它不再是 WPF 中的一个选项。
P.S. I'm trying some tricks with handling WM_GETMINMAXINFO, WM_SYSCOMMAND, etc. But seems it's not working...
PS 我正在尝试一些处理 WM_GETMINMAXINFO、WM_SYSCOMMAND 等的技巧。但似乎它不起作用......
采纳答案by Alex Klaus
WPFdoes not have a native way to disable maximizing windows (unlike WinForms). Hence consider the following key points:
WPF没有禁用最大化窗口的本机方法(与WinForms不同)。因此,请考虑以下关键点:
1. Hide the Maximize button
1.隐藏最大化按钮
Using WinAPIis a way to go, but only for hiding the Maximize button. Use the following:
使用WinAPI是一种方法,但仅用于隐藏最大化按钮。使用以下内容:
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private const int GWL_STYLE = -16;
private const int WS_MAXIMIZEBOX = 0x10000;
private void Window_SourceInitialized(object sender, EventArgs e)
{
var hwnd = new WindowInteropHelper((Window)sender).Handle;
var value = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, (int)(value & ~WS_MAXIMIZEBOX));
}
2. Handling maximizing manually
2. 手动最大化
That code above still allows maximizing (e.g. by double click on the window's title).
上面的代码仍然允许最大化(例如通过双击窗口的标题)。
WPFhas no control on the title bar behavior. if you want to change the double click behavior you will need to remove the title bar and create you own. Take a look how it's been done in MahApps.Metro- link to sample. After that handle double click event.
WPF无法控制标题栏行为。如果您想更改双击行为,您将需要删除标题栏并创建您自己的。看看它是如何在MahApps.Metro 中完成的-链接到示例。之后处理双击事件。
回答by Marek Dzikiewicz
I had a similar issue. My window does not have any form border or title bar, but it can be moved around (using the mouse). The problem is that if a user moves the window to the top edge of the screen, then Windows automatically maximizes the window.
我有一个类似的问题。我的窗口没有任何窗体边框或标题栏,但它可以四处移动(使用鼠标)。问题在于,如果用户将窗口移动到屏幕的顶部边缘,则 Windows 会自动最大化窗口。
I managed to work around this issue by attaching the following handler to the window's StateChangedevent.
我设法通过将以下处理程序附加到窗口的StateChanged事件来解决此问题。
private void OnWindowStateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
}
回答by edtheprogrammerguy
Good solution I put together with a little help from MSDN on detecting non-client mouse activity in WPF windows.
很好的解决方案,我在 MSDN 的帮助下整理了一些关于在 WPF 窗口中检测非客户端鼠标活动的方法。
Calling handled = truein the WndProcif msg == WM_NCLBUTTONDBLCLKwill prevent the window from maximizing when the user double clicks on the non-client area.
调用handled = true中WndProc,如果msg == WM_NCLBUTTONDBLCLK将阻止窗口最大化时,在非客户区用户双击。
myClass() //c'tor
{
InitializeComponent();
SourceInitialized += new EventHandler(myClass_SourceInitialized);
}
void myClass_SourceInitialized(object sender, EventArgs e)
{
System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));
}
int WM_NCLBUTTONDBLCLK { get { return 0x00A3; } }
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_NCLBUTTONDBLCLK)
{
handled = true; //prevent double click from maximizing the window.
}
return IntPtr.Zero;
}
Helpful MSDN ref: https://social.msdn.microsoft.com/Forums/vstudio/en-US/f54dde25-b748-4724-a7fe-a355b086cfd4/mouse-event-in-the-nonclient-window-area
有用的 MSDN 参考:https: //social.msdn.microsoft.com/Forums/vstudio/en-US/f54dde25-b748-4724-a7fe-a355b086cfd4/mouse-event-in-the-nonclient-window-area
回答by avantprime
After encountering this problem and researching this SO question I decided that the answers were not sufficient. After removing the Title Bar the window still maximizes when double clicking close to the top of the window.
在遇到这个问题并研究了这个 SO 问题后,我认为答案是不够的。删除标题栏后,在靠近窗口顶部双击时窗口仍然最大化。
I chose the approach of removing the title bar and disabling double clicking on the window.
我选择了删除标题栏和禁用双击窗口的方法。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MouseDoubleClick += (sender, args) =>
{
args.Handled = true;
};
}
}
In my application I was using MahApps.Metro which would inherit from MetroWindowinstead of Windowhowever the above example should work in both cases.
在我的应用程序中,我使用的是 MahApps.Metro,它会从MetroWindow而不是Window继承,但是上面的例子应该适用于两种情况。
回答by Walt Ritscher
Does this work for you?
这对你有用吗?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.SizeChanged += MainWindow_SizeChanged;
}
void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = System.Windows.WindowState.Normal;
}
}
}
回答by JanDotNet
Another simple (but ugly) solution:
另一个简单(但丑陋)的解决方案:
// inside a Window class
protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnPreviewMouseDoubleClick(e);
const int titleHeight = 30;
var position = e.GetPosition(this);
if (position.Y <= titleHeight)
{
e.Handled = true;
}
}
Note: The user can still maximizing the window using the contextmenu on the title bar / moving the window to the upper edge of the screen.
注意:用户仍然可以使用标题栏上的上下文菜单最大化窗口/将窗口移动到屏幕的上边缘。

