禁用“关闭”按钮而不隐藏控制框

时间:2015-02-04 13:45:19

标签: vb.net controlbox

我有一个应用程序,我不希望用户能够关闭表单。好的,够容易的。只需为表单设置ControlBox = false即可。

但是,我想将应用程序图标保留在表单的左上角。我知道,但细节对我来说意味着什么。

设置Controlbox = false也会使应用程序的图标在表单的左上角消失。有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:2)

Here是我使用的代码。

我的VB.Net版本。

Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overrides ReadOnly Property CreateParams() As Windows.Forms.CreateParams
    Get
        Dim mdiCp As Windows.Forms.CreateParams = MyBase.CreateParams
        mdiCp.ClassStyle = mdiCp.ClassStyle Or CP_NOCLOSE_BUTTON
        Return mdiCp
    End Get
End Property

答案 1 :(得分:1)

我需要一个可以有条件地禁用关闭的选项(就像标准MessageBoxYesNo问题所做的那样)所以接受的答案对我不起作用或者我可能看不到我怎么能让它发挥作用。我最终得到了这个

Private Const MF_BYPOSITION As Int32 = &H400
Private Const MF_REMOVE As Int32 = &H1000
Private Declare Auto Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As IntPtr
Private Declare Auto Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32
Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Auto Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32

Public Sub DisableCloseButton(ByVal hwnd As IntPtr)
    Dim hMenu As IntPtr, n As Int32
    hMenu = GetSystemMenu(hwnd, 0)
    If Not hMenu.Equals(IntPtr.Zero) Then
        n = GetMenuItemCount(hMenu)
        If n > 0 Then
            RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)
            RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)
            DrawMenuBar(hwnd)
        End If
    End If
End Sub

通过

进行调用

DisableCloseButton(MyForm.Handle)

当我将它用于自定义消息框时,我没有测试如何重新启用。