在我们日常编程中,进入VB编程开发界面后,默认的窗体运行后是下图的状态,右上角的关闭按钮是直接可用的状态。

vb.net 程序退出 进程还在 vb退出窗体代码_ci

但是在实际开发中,有时候根据实际功能需要,会出现不想让右上角的关闭按钮起作用时的情形,那么应该如何做呢?

vb.net 程序退出 进程还在 vb退出窗体代码_vb.net 程序退出 进程还在_02

代码实现:

标准模块代码:

Option ExplicitDeclare Function GetSystemMenu Lib "User32" (ByVal hwnd As Long, ByVal bRevert As Long) As LongDeclare Function DeleteMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As LongDeclare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As LongPublic Const MF_BYPOSITION = &H400&

窗体中代码:

Option ExplicitPrivate Sub Form_Load() '窗体载入时调用模块中的方法禁用关闭按钮    Dim hwndMenu As Long    Dim c As Long    hwndMenu = GetSystemMenu(Me.hwnd, 0)    c = GetMenuItemCount(hwndMenu)    DeleteMenu hwndMenu, c - 1, MF_BYPOSITION    c = GetMenuItemCount(hwndMenu)    DeleteMenu hwndMenu, c - 1, MF_BYPOSITIONEnd SubPrivate Sub Command1_Click() '退出    EndEnd Sub

赶快实际运行,体验一下吧~