wpf中窗口禁止最大化可以通过属性ResizeMode来设置,但是ResizeMode有一个问题就是如果ResizeMode设置为NoResize的话,是可以禁止最大化的,但是这样同时也就不能拖动调整大小了,通过如下方法可以达到这个效果,通过win32 API来设置:
需要用到的API函数:
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hMenu, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
以下函数是具体的设置函数:
/// <summary>
/// 是否禁止最大化, 允许拖动
/// </summary>
/// <param name="isDisable">True:禁止 False:允许</param>
private void DisableMaxmizebox(bool isDisable)
{
int GWL_STYLE = -16;
int WS_MAXIMIZEBOX = 0x00010000;
int SWP_NOSIZE = 0x0001;
int SWP_NOMOVE = 0x0002;
int SWP_FRAMECHANGED = 0x0020;
IntPtr handle = new WindowInteropHelper(this).Handle;
int nStyle = GetWindowLong(handle, GWL_STYLE);
if (isDisable)
{
nStyle &= ~(WS_MAXIMIZEBOX);
}
else
{
nStyle |= WS_MAXIMIZEBOX;
}
SetWindowLong(handle, GWL_STYLE, nStyle);
SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
}
说明:文章原文出自:http://www.cnblogs.com/dougua/archive/2007/07/03/804035.html