[ DllImport (
"
USER32.DLL
"
) ]
public static extern int GetSystemMenu( int hwnd, int bRevert);
[ DllImport ( " USER32.DLL " ) ]
public static extern int RemoveMenu( int hMenu, int nPosition, int wFlags);
/// <summary>
/// 返回值,非零表示成功,零表示失败。
/// </summary>
/// <param name="iHWND"> 窗口的句柄 </param>
/// <returns> 是否成功 </returns>
public int RemoveXButton( int iHWND)
{
int iSysMenu;
const int MF_BYPOSITION = 0x400 ; // 0x400-基于位置
iSysMenu = GetSystemMenu( this .Handle.ToInt32(), 0 );
return RemoveMenu(iSysMenu, 6 , MF_BYPOSITION); // 是6而不是其他
}
public static extern int GetSystemMenu( int hwnd, int bRevert);
[ DllImport ( " USER32.DLL " ) ]
public static extern int RemoveMenu( int hMenu, int nPosition, int wFlags);
/// <summary>
/// 返回值,非零表示成功,零表示失败。
/// </summary>
/// <param name="iHWND"> 窗口的句柄 </param>
/// <returns> 是否成功 </returns>
public int RemoveXButton( int iHWND)
{
int iSysMenu;
const int MF_BYPOSITION = 0x400 ; // 0x400-基于位置
iSysMenu = GetSystemMenu( this .Handle.ToInt32(), 0 );
return RemoveMenu(iSysMenu, 6 , MF_BYPOSITION); // 是6而不是其他
}
通过函数返回值就可以判断目的是否达到,在构造函数里面可以这样判断:
<div class="chcontent">int<span style="color: rgb(0, 0, 0);"> iReturn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> RemoveXButton( </span>this<span style="color: rgb(0, 0, 0);">.Handle.ToInt32()); </span>if<span style="color: rgb(0, 0, 0);">(iReturn </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span>0<span style="color: rgb(0, 0, 0);">) </span>//返回值非零表示成功,零表示失败。 <span style="color: rgb(0, 0, 0);"> { MessageBox.Show(</span>"Error!"<span style="color: rgb(0, 0, 0);">); } </span>else<span style="color: rgb(0, 0, 0);"> { MessageBox.Show(</span>"Success!"<span style="color: rgb(0, 0, 0);">); }</span></div>
这样,关闭按钮处于不可用状态,不过不要忘记添加一个控件可以让窗体正常关闭,不然只能结束进程来退出程序了。
最小化 最大化按钮不显示可以使用两个属性:
MaximumBox=false;
MinimumBox=false;