Results 1 to 6 of 6

Thread: how to supress the close button on the form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2004
    Posts
    169

    how to supress the close button on the form

    Since I have my own close button on the form, how to make the vb style close button at the right-upper corner "x" disappear so that it only shows minimize, and maximize.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to supress the close button on the form

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const SC_CLOSE As Long = &HF060&
    4. Private Const MIIM_STATE As Long = &H1&
    5. Private Const MIIM_ID As Long = &H2&
    6. Private Const MFS_GRAYED As Long = &H3&
    7. Private Const WM_NCACTIVATE As Long = &H86
    8.  
    9. Private Type MENUITEMINFO
    10.     cbSize As Long
    11.     fMask As Long
    12.     fType As Long
    13.     fState As Long
    14.     wID As Long
    15.     hSubMenu As Long
    16.     hbmpChecked As Long
    17.     hbmpUnchecked As Long
    18.     dwItemData As Long
    19.     dwTypeData As String
    20.     cch As Long
    21. End Type
    22.  
    23. Private Declare Function GetSystemMenu Lib "user32" ( _
    24.     ByVal hWnd As Long, ByVal bRevert As Long) As Long
    25. Private Declare Function GetMenuItemInfo Lib "user32" Alias _
    26.     "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, _
    27.     ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long
    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. Private Declare Function SendMessage Lib "user32" Alias _
    32.     "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
    33.     ByVal wParam As Long, lParam As Any) As Long
    34. Private Declare Function IsWindow Lib "user32" _
    35.     (ByVal hWnd As Long) As Long
    36.  
    37. Public Function EnableCloseButton(ByVal hWnd As Long, Enable As Boolean) As Integer
    38.     Const xSC_CLOSE As Long = -10
    39.     ' Check that the window handle passed is valid
    40.     EnableCloseButton = -1
    41.     If IsWindow(hWnd) = 0 Then Exit Function
    42.     ' Retrieve a handle to the window's system menu
    43.     Dim hMenu As Long
    44.     hMenu = GetSystemMenu(hWnd, 0)
    45.     ' Retrieve the menu item information for the close menu item/button
    46.     Dim MII As MENUITEMINFO
    47.     MII.cbSize = Len(MII)
    48.     MII.dwTypeData = String(80, 0)
    49.     MII.cch = Len(MII.dwTypeData)
    50.     MII.fMask = MIIM_STATE
    51.    
    52.     If Enable Then
    53.         MII.wID = xSC_CLOSE
    54.     Else
    55.         MII.wID = SC_CLOSE
    56.     End If
    57.    
    58.     EnableCloseButton = -0
    59.     If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
    60.     ' Switch the ID of the menu item so that VB can not undo the action itself
    61.     Dim lngMenuID As Long
    62.     lngMenuID = MII.wID
    63.    
    64.     If Enable Then
    65.         MII.wID = SC_CLOSE
    66.     Else
    67.         MII.wID = xSC_CLOSE
    68.     End If
    69.    
    70.     MII.fMask = MIIM_ID
    71.     EnableCloseButton = -2
    72.     If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then Exit Function
    73.     ' Set the enabled / disabled state of the menu item
    74.     If Enable Then
    75.         MII.fState = (MII.fState Or MFS_GRAYED)
    76.         MII.fState = MII.fState - MFS_GRAYED
    77.     Else
    78.         MII.fState = (MII.fState Or MFS_GRAYED)
    79.     End If
    80.    
    81.     MII.fMask = MIIM_STATE
    82.     EnableCloseButton = -3
    83.     If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
    84.     SendMessage hWnd, WM_NCACTIVATE, True, 0
    85.     EnableCloseButton = 0
    86. End Function
    87.  
    88. Private Sub Form_Load()
    89. EnableCloseButton Me.hWnd, False
    90. End Sub
    Not only will this disable the close button, but it will also disable the Close menu option of the System menu.

    If, at some point, you want to enable those two features, all you have to do is say: EnableCloseButton Me.hWnd, True

  3. #3
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: how to supress the close button on the form

    Here's a simpler method:

    On the form queryunload event
    ///////////////////////////////////////////////////////////////
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode <> 1 Then
    Cancel = vbTrue
    End If
    End Sub
    //////////////////////////////////////////////////////////////

  4. #4
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: how to supress the close button on the form

    I guess I need to fully read first. My code just disables the button.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: how to supress the close button on the form

    redshirtme, while it's OK to disable or bypass the X in control box, most users expect to find it there, so why not just call your close button code from the QueryUnload Event?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to supress the close button on the form

    Quote Originally Posted by MartinLiss
    redshirtme, while it's OK to disable or bypass the X in control box, most users expect to find it there, so why not just call your close button code from the QueryUnload Event?
    Martin makes an excellent point. I venture to say if you decide to go the disable the button route, you will get questions about why they can't do that. If you put whatever you need done in, as Martin suggests, the QueryUnload event, this code will execute regardless of how the user closes your application.

    Although I keep the code I posted in my Code Library, I don't actually use it anymore. I did once, and got some 'unpleasant' feedback from my customers. From that point on, I started making use of QueryUnload.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width