Results 1 to 24 of 24

Thread: disabling close button

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    disabling close button

    hi all!!

    can neone tell me how to disable the CLOSE button (X) on the top right of the form while some process is on the run??

    thnx

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: disabling close button

    Well I could show you how to remove the close command and therefor disable the close button but as I understand it you want to enable it again after awhile and that's another matter. The easiest thing you could do is to simply set some boolean flag while you're running some code and use the QueryUnload event of the Form to check if the Form is being unloaded because someone pressed the close button (or picked Close in the system menu) and then simply ignore it if the boolean flag is still set to True.

  3. #3
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: disabling close button

    PrivateSub Form_Load()
    EnableCloseButton me.hWnd, false
    End Sub

    And to re-enable it later:


    private Sub Command1_Click()
    EnableCloseButton me.hWnd, true
    End Sub

    oops sorry, I didnt finish reading the example I found on google lol, my bad.
    Ne ways, heres the module that was with that example.
    I thought it was simple than that


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

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: disabling close button

    There's also the Controlbox property of the form you can set to false, but that would eliminate the minimize and maximize buttons as well.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: disabling close button

    Quote Originally Posted by planethax
    PrivateSub Form_Load()
    EnableCloseButton me.hWnd, false
    End Sub

    And to re-enable it later:

    private Sub Command1_Click()
    EnableCloseButton me.hWnd, true
    End Sub
    That looks great! Do you also have the source code for the EnableCloseButton procedure or did you just make that up?

    Quote Originally Posted by Jacob Roman
    There's also the Controlbox property of the form you can set to false, but that would eliminate the minimize and maximize buttons as well.
    But the ControlBox property can't be changed during run-time.

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: disabling close button

    I know it can't be changed during runtime. Just another one of them limitations.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: disabling close button

    Quote Originally Posted by Jacob Roman
    I know it can't be changed during runtime. Just another one of them limitations.
    Limitation? Well that might be true but the reason for that is that when you create a window you need to determent how this window will look and act. If you want it to have a system menu, what kind of border the window should have, the owner of the window (should it be displayed modally or not)...

    All of this VB does for you and all you have to do is to simply set some properties during design time. But if you want to change any of these behaviours you need to destroy the window and recreate it again from scratch.

    That is why you can't simply change some of these properties during run-time since it means a new window has to be created, and in VB we design and create our windows during design time (even though they aren't really created before we choose to show them).

  8. #8
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: disabling close button

    I'd just do it this way

    VB Code:
    1. Option Explicit
    2.  
    3. Private m_bBlockUnload As Boolean
    4.  
    5. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    6.  
    7.     If m_bBlockUnload Then
    8.         If UnloadMode = 0 Then
    9.             Cancel = True
    10.         End If
    11.     End If
    12. End Sub

  9. #9

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: disabling close button

    thnx planethax....yours code's working nicely.

  10. #10

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: disabling close button

    thnx longwolf....even ur code is nice but i failed to enable it afterwards. can u explain how can i do that.

  11. #11
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: disabling close button

    When ever your program begins a process that you want to protect just set:
    m_bBlockUnload = True.

    Also the line:
    VB Code:
    1. If UnloadMode = 0 Then
    will block the 'X' from closing the app, you may want to check other 'modes' of closing the program and block them too.

    Use the VB help files to pick the modes to block.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: disabling close button

    @Joacim, its originally posted by Hack on VBF but originated over at CodeGuru. I knew I seen that code before.

    Disable x
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: disabling close button

    Thanks Rob, that's the same code posted by Planethax above. I was well aware of the code to remove the Close menu item (or any other system menu items). However I've never seen the code to get it back while it was gone before. Probably because I've never found myself in the need of doing so Thanks anyway!

  14. #14

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: disabling close button

    hey longwolf!! can u explain me one thing in ur code?!?

    in the queryunload procedure, a parameter cancel is declared as integer then how can we assign a boolean value to it:-

    Cancel = True

  15. #15
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: disabling close button

    Quote Originally Posted by Harsh Gupta
    hey longwolf!! can u explain me one thing in ur code?!?

    in the queryunload procedure, a parameter cancel is declared as integer then how can we assign a boolean value to it:-

    Cancel = True

    Here is a description:

    VB Code:
    1. Private m_bBlockUnload As Boolean   ' Global variable to determine if the form should b allowed to unload.
    2.  
    3. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    4.     If m_bBlockUnload Then     ' Are we allowed to unload this form?
    5.         If UnloadMode = 0 Then ' Check the forums unload mode.
    6.             Cancel = True           ' Cancel the close.
    7.         End If                          ' Close the IF statement.
    8.     End If                              ' Close the IF statement.
    9. End Sub

    And yes thats odd, I always use a True too, it says its fine in the MSDN documentation so...

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  16. #16
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: disabling close button

    A Boolean is stored as an Integer. The True and False values are Integer values corresponding to -1 and 0 respectively. Before version 4 VB didn't have an Integer data type but the True and False values did exist. We then used an Integer as a Boolean.

    When it comes to the QueryUnload or the Unload events of a Form it doesn't really matter what value you give to the Cancel property. As long as it is not equal to 0 it will stop the unloading of the Form.

  17. #17
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: disabling close button

    Quote Originally Posted by Joacim Andersson
    A Boolean is stored as an Integer. The True and False values are Integer values corresponding to -1 and 0 respectively. Before version 4 VB didn't have an Integer data type but the True and False values did exist. We then used an Integer as a Boolean.

    Right that makes sence too me

    Thanks for the info even though I did not aks for it Joacim Andersson!

    Cheers,


    RyanJ
    My Blog.

    Ryan Jones.

  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: disabling close button

    Quote Originally Posted by sciguyryan
    Thanks for the info even though I did not aks for it Joacim Andersson!
    No you didn't ask for it. I was replying to Harsh Gupta that did ask about it.

  19. #19

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: disabling close button

    oooookkkkk thnx Joacim Andersson

  20. #20
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: disabling close button

    Quote Originally Posted by Joacim Andersson
    No you didn't ask for it. I was replying to Harsh Gupta that did ask about it.
    Yes, I know Harsh Gupta did but it was still useful to know
    I meant it in the context that, though I did not ask for it I would still say thanks

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  21. #21
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: disabling close button

    Sorry for dragging this topic up again, but the above code doesn't work correctly anymore in at least windows 7 (it doesn't draw the Close button disable).
    the following is a version that does work correctly in windows 7. It's based on the code above which changes the ID of the SC_CLOSE due to VB resetting the state if you use the dropdown menu..

    It took a while to figure out what went wrong.. It seems Windows 7 relies on the ID SC_CLOSE and some other state to update the X button.
    I also tried replacing my usage of EnableMenuItem with the original SetMenuItem but for some strange reason that only seems to work when you won't use the dropdown menu, once you use that, the drawstate of the button isn't updated anymore..

    The following code does work in all the situations I tested.. So I hope this will help some other people who stumbled on the same problem as I have..

    Code:
    Public Declare Function EnableMenuItem Lib "user32.dll" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
    
    Public Sub EnableCloseButton(ByVal HwndForm As Long, ByVal bEnabled As Boolean)
      Dim hMenu     As Long
      Dim lID       As Long
      Dim lOldID    As Long
      Dim lReturn   As Long
      Dim tMII      As MENUITEMINFO
    
      hMenu = GetSystemMenu(HwndForm, 0)
      With tMII
        .cbSize = Len(tMII)
        .dwTypeData = String(80, 0)
        .cCH = Len(.dwTypeData)
        .fMask = MIIM_STATE
        .wID = SC_CLOSE
        '--------------------------------------------------------------
        'Try to get the SC_Close Menu
        lReturn = GetMenuItemInfo(hMenu, SC_CLOSE, False, tMII)
        '--------------------------------------------------------------
        'Didn't exist? then Try the xSC_Close Menu (When the menu is
        'disabled by us we set the ID to xSC_Close)
        If lReturn = 0 Then
          .wID = xSC_CLOSE
          lReturn = GetMenuItemInfo(hMenu, xSC_CLOSE, False, tMII)
        End If 'If lReturn=0
        '--------------------------------------------------------------
        'We got the MenuItemInfo?
        If lReturn <> 0 Then
          '--------------------------------------------------------------
          'Menu still original Close? then change state (before changing ID)
          If .wID = SC_CLOSE Then lReturn = EnableMenuItem(hMenu, .wID, IIf(bEnabled, MF_ENABLED, MF_GRAYED))
          '--------------------------------------------------------------
          'Because VB Changes the State depending on the ID on the fly
          'we have to Change the ID to xSC_Close if Disabled and to SC_Close when Enabled
          lOldID = .wID
          lID = IIf(bEnabled, SC_CLOSE, xSC_CLOSE)
          If lID <> lOldID Then
            .fMask = MIIM_ID
            .wID = lID
            lReturn = SetMenuItemInfo(hMenu, lOldID, False, tMII)
            If lReturn = 0 Then .wID = lOldID
          End If 'If lID<>lOldID
          '--------------------------------------------------------------
          'Only update the state again if the menuID is the original SC_CLOSE
          If .wID = SC_CLOSE Then lReturn = EnableMenuItem(hMenu, .wID, IIf(bEnabled, MF_ENABLED, MF_GRAYED))
        End If 'If lReturn<>0
      End With 'With tMII
    End Sub

  22. #22
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: disabling close button

    @SuperDre,

    There was no need to reply to this topic since it over 8 years old. Please start a new topic.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  23. #23
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: disabling close button

    Yes it was, as the solutions give don't work properly anymore in newer windows, at least not as the way I expected, and therefore with the new solution it will help other people, as this was also the topic I found when I was looking for a solution.. We're here to help each other aren't we? so any better solution to a problem is IMHO best put in a topic which already discusses the problem and not create another new topic for it.. but that's just MY opinion..

  24. #24
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: disabling close button

    Quote Originally Posted by SuperDre View Post
    Yes it was, as the solutions give don't work properly anymore in newer windows, at least not as the way I expected, and therefore with the new solution it will help other people, as this was also the topic I found when I was looking for a solution.. We're here to help each other aren't we? so any better solution to a problem is IMHO best put in a topic which already discusses the problem and not create another new topic for it.. but that's just MY opinion..
    Ah ok! I thought you also, had a problem you were facing! As long as that isn't the case it's fine. I thought you were hi-jacking the thread with your own problem which, is not acceptable.

    Edit:

    I read

    The following code does work in all the situations I tested.. So I hope this will help some other people who stumbled on the same problem as I have..
    as too mean you were still having a problem.
    Last edited by Nightwalker83; Jun 14th, 2013 at 06:08 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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