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