开发者

Disable close button in windows form

I found this code over the internet but am unsure about how to use it. Also I need to enable it back after work is complete. Help please.

Private Const CP_NOCLOSE_BUTTON As Integer = &H200

Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim myCp As CreateParams = MyBase.CreateParams
        myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLO开发者_如何学运维SE_BUTTON
        Return myCp
    End Get
End Property


You would paste this into your form's code to use it. This however permanently disables the close button. Doing it dynamically requires very different code, you have to modify the system menu. Paste this code into your form and use the CloseEnabled property in your logic:

Public Property CloseEnabled() As Boolean
    Get
        Return mCloseEnabled
    End Get
    Set(ByVal value As Boolean)
        If value <> mCloseEnabled Then
            mCloseEnabled = value
            setSystemMenu()
        End If
    End Set
End Property
Private mCloseEnabled As Boolean = True

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    MyBase.OnHandleCreated(e)
    setSystemMenu()
End Sub

Private Sub setSystemMenu()
    Dim menu As IntPtr = GetSystemMenu(Me.Handle, False)
    Dim enable As Integer
    If Not mCloseEnabled Then enable = 1
    EnableMenuItem(menu, SC_CLOSE, enable)
End Sub

'' P/Invoke declarations
Private const SC_CLOSE As Integer = &hf060
Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
Private Declare Function EnableMenuItem Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal IDEnableItem As Integer, ByVal wEnable As Integer) As Integer
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜