VB Code:
  1. '
  2. ' From Pete Cozens Example on the CodeGuru Site
  3. '
  4.  
  5. Private Const SC_CLOSE As Long = &HF060&
  6. Private Const MIIM_STATE As Long = &H1&
  7. Private Const MIIM_ID As Long = &H2&
  8. Private Const MFS_GRAYED As Long = &H3&
  9. Private Const WM_NCACTIVATE As Long = &H86
  10.  
  11. Private Type MENUITEMINFO
  12.     cbSize As Long
  13.     fMask As Long
  14.     fType As Long
  15.     fState As Long
  16.     wID As Long
  17.     hSubMenu As Long
  18.     hbmpChecked As Long
  19.     hbmpUnchecked As Long
  20.     dwItemData As Long
  21.     dwTypeData As String
  22.     cch As Long
  23. End Type
  24.  
  25. Private Declare Function GetSystemMenu Lib "user32" ( _
  26.     ByVal hWnd As Long, ByVal bRevert As Long) As Long
  27.  
  28. Private Declare Function GetMenuItemInfo Lib "user32" Alias _
  29.     "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, _
  30.     ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long
  31.  
  32. Private Declare Function SetMenuItemInfo Lib "user32" Alias _
  33.     "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, _
  34.     ByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long
  35.  
  36. Private Declare Function SendMessage Lib "user32" Alias _
  37.     "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
  38.     ByVal wParam As Long, lParam As Any) As Long
  39.  
  40. Private Declare Function IsWindow Lib "user32" _
  41.     (ByVal hWnd As Long) As Long
  42.  
  43. '*******************************************************************************
  44. ' Enables / Disables the close button on the titlebar and in the system menu
  45. ' of the form window passed.
  46. '-------------------------------------------------------------------------------
  47. ' Return Values:
  48. '
  49. '    0  Close button state changed succesfully / nothing to do.
  50. '   -1  Invalid Window Handle (hWnd argument) Passed to the function
  51. '   -2  Failed to switch command ID of Close menu item in system menu
  52. '   -3  Failed to switch enabled state of Close menu item in system menu
  53. '
  54. '-------------------------------------------------------------------------------
  55. ' Parameters:
  56. '
  57. '   hWnd    The window handle of the form whose close button is to be enabled/
  58. '           disabled / greyed out.
  59. '
  60. '   Enable  True if the close button is to be enabled, or False if it is to
  61. '           be disabled / greyed out.
  62. '
  63. '-------------------------------------------------------------------------------
  64. ' Example:
  65. '
  66. ' Add a form window to your project, and place a button on the form. Add the
  67. ' following in the form's code window:
  68. '
  69. '    Option Explicit
  70. '
  71. '    Private m_blnCloseEnabled As Boolean
  72. '
  73. '    Private Sub Form_Load()
  74. '        m_blnCloseEnabled = True
  75. '        Command1.Caption = "Disable"
  76. '    End Sub
  77. '
  78. '    Private Sub Command1_Click()
  79. '        m_blnCloseEnabled = Not m_blnCloseEnabled
  80. '        EnableCloseButton Me.hwnd, m_blnCloseEnabled
  81. '
  82. '        If m_blnCloseEnabled Then
  83. '            Command1.Caption = "Disable"
  84. '        Else
  85. '            Command1.Caption = "Enable"
  86. '        End If
  87. '    End Sub
  88. '
  89. '-------------------------------------------------------------------------------
  90.  
  91. Public Function EnableCloseButton(ByVal hWnd As Long, Enable As Boolean) _
  92.                                                                 As Integer
  93.     Const xSC_CLOSE As Long = -10
  94.  
  95.     ' Check that the window handle passed is valid
  96.    
  97.     EnableCloseButton = -1
  98.     If IsWindow(hWnd) = 0 Then Exit Function
  99.    
  100.     ' Retrieve a handle to the window's system menu
  101.    
  102.     Dim hMenu As Long
  103.     hMenu = GetSystemMenu(hWnd, 0)
  104.    
  105.     ' Retrieve the menu item information for the close menu item/button
  106.    
  107.     Dim MII As MENUITEMINFO
  108.     MII.cbSize = Len(MII)
  109.     MII.dwTypeData = String(80, 0)
  110.     MII.cch = Len(MII.dwTypeData)
  111.     MII.fMask = MIIM_STATE
  112.    
  113.     If Enable Then
  114.         MII.wID = xSC_CLOSE
  115.     Else
  116.         MII.wID = SC_CLOSE
  117.     End If
  118.    
  119.     EnableCloseButton = -0
  120.     If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
  121.    
  122.     ' Switch the ID of the menu item so that VB can not undo the action itself
  123.    
  124.     Dim lngMenuID As Long
  125.     lngMenuID = MII.wID
  126.    
  127.     If Enable Then
  128.         MII.wID = SC_CLOSE
  129.     Else
  130.         MII.wID = xSC_CLOSE
  131.     End If
  132.    
  133.     MII.fMask = MIIM_ID
  134.     EnableCloseButton = -2
  135.     If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then Exit Function
  136.    
  137.     ' Set the enabled / disabled state of the menu item
  138.    
  139.     If Enable Then
  140.         MII.fState = (MII.fState Or MFS_GRAYED)
  141.         MII.fState = MII.fState - MFS_GRAYED
  142.     Else
  143.         MII.fState = (MII.fState Or MFS_GRAYED)
  144.     End If
  145.    
  146.     MII.fMask = MIIM_STATE
  147.     EnableCloseButton = -3
  148.     If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
  149.    
  150.     ' Activate the non-client area of the window to update the titlebar, and
  151.     ' draw the close button in its new state.
  152.    
  153.     SendMessage hWnd, WM_NCACTIVATE, True, 0
  154.    
  155.     EnableCloseButton = 0
  156.    
  157. End Function
Say: EnableCloseButton = False