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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值