原文:http://www.cnblogs.com/appleseeker/archive/2009/02/02/1382583.html
这篇文章中将介绍如何隐藏起Form右上角的OK/X按钮,有时候用户觉得不需要这个按钮,那去掉它吧~~~~
再次介绍下该按钮的功能,X表示最小化,OK表示确认并关闭.通常新建一个Form后,默认窗体的最小化是True,所以如果需要显示OK的话,就需要将该属性设为False.
隐藏该按钮需要调用3个API
Code
[DllImport("aygshell.dll")]
private static extern bool SHDoneButton(IntPtr hWnd, UInt32 dwState); // 窗体为OK时,调用此方法即可.
[DllImport("coredll.dll")]
public static extern UInt32 SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong); // 获得指定窗口的信息
[DllImport("coredll.dll")]
public static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex); // 设置指定窗口的信息,这2个方法结合起来用,才能隐藏X按钮
具体使用
Code
public const UInt32 SHDB_SHOW = 0x0001;
public const UInt32 SHDB_HIDE = 0x0002;
public const int GWL_STYLE = -16;
public const UInt32 WS_NONAVDONEBUTTON = 0x00010000;
/**//// <summary>
/// 隐藏OK按钮
/// </summary>
/// <param name="hWnd"></param>
public static void HideDoneButton(IntPtr hWnd)
{
SHDoneButton(hWnd, SHDB_HIDE);
}
/**//// <summary>
/// 隐藏X按钮
/// </summary>
/// <param name="hWnd"></param>
public static void HideXButton(IntPtr hWnd)
{
UInt32 dwStyle = GetWindowLong(hWnd, GWL_STYLE);
if ((dwStyle & WS_NONAVDONEBUTTON) == 0)
SetWindowLong(hWnd, GWL_STYLE, dwStyle | WS_NONAVDONEBUTTON);
}
在实际调用时,只要将窗体的Handle作为参数传入即可.
隐藏按钮后,如何打开?
只需要设置窗体的MinimizeBox属性即可.想显示OK,设为False.想显示X,设为True即可.
代码下载:DeviceTechDemo.rar
运行环境:VS2008 + WM6.0 + .net cf3.5