2010-06-19 62 views
2

我通過互聯網發現了這個代碼,但我不確定如何使用它。另外我需要在工作完成後啓用它。請幫助。禁用windows窗體中的關閉按鈕

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_NOCLOSE_BUTTON 
     Return myCp 
    End Get 
End Property 

回答

3

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