你會粘貼到你的窗體的代碼來使用它。但是,這會永久禁用關閉按鈕。動態地執行它需要非常不同的代碼,您必須修改系統菜單。將此代碼粘貼到您的表單中,並在您的邏輯中使用CloseEnabled屬性:
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