使用默认的样式设置,只能设置禁用最大化,无法设置禁用最小化。最终还是发现,需要用到api函数。只是不知道在win7下面工作情况如何。
I've stolen some code I found on the MSDN forums and made an extension method on the Window class, like this:
internalstaticclassWindowExtensions{[DllImport("user32.dll")]internalexternstaticintSetWindowLong(IntPtrhwnd,intindex,intvalue);[DllImport("user32.dll")]internalexternstaticintGetWindowLong(IntPtrhwnd,intindex);internalstaticvoidHideMinimizeAndMaximizeButtons(thisWindowwindow){constintGWL_STYLE=-16;IntPtrhwnd=newSystem.Windows.Interop.WindowInteropHelper(window).Handle;longvalue=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
privateconstintMF_BYPOSITION=0x400
;
[
DllImport("User32"
)]
privatestaticexternintRemoveMenu(IntPtrhMenu,intnPosition,intwFlags
);
[
DllImport("User32"
)]
privatestaticexternIntPtrGetSystemMenu(IntPtrhWnd,boolbRevert
);
[
DllImport("User32"
)]
privatestaticexternintGetMenuItemCount(IntPtrhWnd
);
internalstaticvoidRemoveCloseButton(Windowwindow
)
{
IntPtrhMenu=GetSystemMenu(newWindowInteropHelper(window).Handle,false
);
intmenuItemCount=GetMenuItemCount(hMenu
);
RemoveMenu(hMenu,menuItemCount-1,MF_BYPOSITION
);
禁用最小化或最大化按钮
// Disable minimize and maximize buttons
[
DllImport("user32.dll"
)]
internalexternstaticintSetWindowLong(IntPtrhwnd,intindex,intvalue
);
[
DllImport("user32.dll"
)]
internalexternstaticintGetWindowLong(IntPtrhwnd,intindex
);
internalstaticvoidDisableMinimizeButton(thisWindowwindow
)
{
constintGWL_STYLE= -16
;
IntPtrhwnd=newSystem.Windows.Interop.WindowInteropHelper(window).Handle
;
longvalue=GetWindowLong(hwnd,GWL_STYLE
);
SetWindowLong(hwnd,GWL_STYLE, (int)(value& -131073
));
}
internalstaticvoidDisableMaximizeButton(thisWindowwindow
)
{
constintGWL_STYLE= -16
;
IntPtrhwnd=newSystem.Windows.Interop.WindowInteropHelper(window).Handle
;
longvalue=GetWindowLong(hwnd,GWL_STYLE
);
SetWindowLong(hwnd,GWL_STYLE, (int)(value& -65537
));
internalstaticvoidDisableMinimizeAndMaximizeButtons(thisWindowwindow)
{
constintGWL_STYLE= -16;
IntPtrhwnd=newSystem.Windows.Interop.WindowInteropHelper(window).Handle;
longvalue=GetWindowLong(hwnd,GWL_STYLE);
SetWindowLong(hwnd,GWL_STYLE, (int)(value& -65537& -131073));
}