WPF如何禁用最小化按钮

使用默认的样式设置,只能设置禁用最大化,无法设置禁用最小化。最终还是发现,需要用到api函数。只是不知道在win7下面工作情况如何。

http://stackoverflow.com/questions/339620/how-do-i-remove-minimize-and-maximize-from-a-resizable-window-in-wpf

I've stolen some code I found on the MSDN forums and made an extension method on the Window class, like this:

internal static class WindowExtensions { [DllImport("user32.dll")] internal extern static int SetWindowLong(IntPtr hwnd, int index, int value); [DllImport("user32.dll")] internal extern static int GetWindowLong(IntPtr hwnd, int index); internal static void HideMinimizeAndMaximizeButtons(this Window window) { const int GWL_STYLE = -16; IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle; long value = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, (int)(value & -131073 & -65537)); } } 

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!

移除关闭按钮

// Disable close button

private const int MF_BYPOSITION = 0x400

;

[

DllImport("User32"

)]

private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags

);

[

DllImport("User32"

)]

private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert

);

[

DllImport("User32"

)]

private static extern int GetMenuItemCount(IntPtr hWnd

);

internal static void RemoveCloseButton(Window window

)

{

IntPtr hMenu = GetSystemMenu(new WindowInteropHelper(window).Handle, false

);

int menuItemCount = GetMenuItemCount(hMenu

);

RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION

);

禁用最小化或最大化按钮

// Disable minimize and maximize buttons

[

DllImport ( "user32.dll"

)]

internal extern static int SetWindowLong ( IntPtr hwnd , int index , int value

);

[

DllImport ( "user32.dll"

)]

internal extern static int GetWindowLong ( IntPtr hwnd , int index

);

internal static void DisableMinimizeButton ( this Window window

)

{

const int GWL_STYLE = - 16

;

IntPtr hwnd = new System . Windows . Interop . WindowInteropHelper ( window ). Handle

;

long value = GetWindowLong ( hwnd , GWL_STYLE

);

SetWindowLong ( hwnd , GWL_STYLE , ( int )( value & - 131073

));

}

internal static void DisableMaximizeButton ( this Window window

)

{

const int GWL_STYLE = - 16

;

IntPtr hwnd = new System . Windows . Interop . WindowInteropHelper ( window ). Handle

;

long value = GetWindowLong ( hwnd , GWL_STYLE

);

SetWindowLong ( hwnd , GWL_STYLE , ( int )( value & - 65537

));

internal static void DisableMinimizeAndMaximizeButtons(this Window window)

{

const int GWL_STYLE = -16;

IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;

long value = GetWindowLong(hwnd, GWL_STYLE);

SetWindowLong(hwnd, GWL_STYLE, (int)(value & -65537 & -131073));

}

你可能感兴趣的:(windows,工作,WPF)