禁用WPF窗口的最大化按钮,保持缩放功能完好 [英] Disable maximize button of WPF window, keeping resizing feature intact

查看:1112
本文介绍了禁用WPF窗口的最大化按钮,保持缩放功能完好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,WPF窗口只有四张ResizeMode选择:NoResize,CanMinimize,CanResize和CanResizeWithGrip。不幸的是,还启用调整的选项,最大化的窗口,那些不都是对我没用。 是否有一个选项来禁用最大化按钮,同时保持调整大小功能?

So WPF windows only have four ResizeMode options: NoResize, CanMinimize, CanResize and CanResizeWithGrip. Unfortunately, the options that enable resizing also enable maximizing the window, and those that don't are useless to me. Is there an option to disable the maximize button while keeping the resize feature?

我preFER的解决方案,不涉及WinAPI的东西。

I'd prefer solutions that don't involve WinAPI stuff.

推荐答案

WPF不必单独禁用最大化按钮,你可以做的WinForms本机的能力。你需要求助于WinAPI的电话。这不是可怕的:

WPF does not have the native capability to disable the Maximize button alone, as you can do with WinForms. You will need to resort to a WinAPI call. It's not scary:

[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));
}

这篇关于禁用WPF窗口的最大化按钮,保持缩放功能完好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆