Results 1 to 11 of 11

Thread: Why can't I disable the close (X) button on the right-top side ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    111

    Why can't I disable the close (X) button on the right-top side ?

    I want to disable the right-top side of menu. Why can't the following code work ?

    Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long


    Private Sub Form_Load()
    hMenu = GetSystemMenu(Me.hwnd, 0)
    DeleteMenu hMenu, 6, MF_BYPOSITION
    End Sub

  2. #2
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Why can't I disable the close (X) button on the right-top side ?

    Have you declared these anywhere in your code?
    VB Code:
    1. Private hMenu As Long
    2. Private Const MF_BYPOSITION As Long = &H400&
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

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

    Re: Why can't I disable the close (X) button on the right-top side ?

    Try 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. ' Enables / Disables the close button on the titlebar and in the system menu
    40. ' of the form window passed.
    41. ' Return Values:
    42. '
    43. '    0  Close button state changed succesfully / nothing to do.
    44. '   -1  Invalid Window Handle (hWnd argument) Passed to the function
    45. '   -2  Failed to switch command ID of Close menu item in system menu
    46. '   -3  Failed to switch enabled state of Close menu item in system menu
    47.  
    48. Public Function EnableCloseButton(ByVal hWnd As Long, Enable As Boolean)
    49. As Integer
    50.     Const xSC_CLOSE As Long = -10
    51.     ' Check that the window handle passed is valid    
    52.     EnableCloseButton = -1
    53.     If IsWindow(hWnd) = 0 Then Exit Function    
    54.     ' Retrieve a handle to the window's system menu    
    55.     Dim hMenu As Long
    56.     hMenu = GetSystemMenu(hWnd, 0)    
    57.     ' Retrieve the menu item information for the close menu item/button    
    58.     Dim MII As MENUITEMINFO
    59.     MII.cbSize = Len(MII)
    60.     MII.dwTypeData = String(80, 0)
    61.     MII.cch = Len(MII.dwTypeData)
    62.     MII.fMask = MIIM_STATE
    63.    
    64.     If Enable Then
    65.         MII.wID = xSC_CLOSE
    66.     Else
    67.         MII.wID = SC_CLOSE
    68.     End If
    69.    
    70.     EnableCloseButton = -0
    71.     If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function    
    72.     ' Switch the ID of the menu item so that VB can not undo the action itself    
    73.     Dim lngMenuID As Long
    74.     lngMenuID = MII.wID
    75.    
    76.     If Enable Then
    77.         MII.wID = SC_CLOSE
    78.     Else
    79.         MII.wID = xSC_CLOSE
    80.     End If
    81.    
    82.     MII.fMask = MIIM_ID
    83.     EnableCloseButton = -2
    84.     If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then Exit Function    
    85.     ' Set the enabled / disabled state of the menu item    
    86.     If Enable Then
    87.         MII.fState = (MII.fState Or MFS_GRAYED)
    88.         MII.fState = MII.fState - MFS_GRAYED
    89.     Else
    90.         MII.fState = (MII.fState Or MFS_GRAYED)
    91.     End If
    92.    
    93.     MII.fMask = MIIM_STATE
    94.     EnableCloseButton = -3
    95.     If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function    
    96.     SendMessage hWnd, WM_NCACTIVATE, True, 0    
    97.     EnableCloseButton = 0    
    98. End Function
    99.  
    100. 'Usage
    101. Private Sub Form_Load()
    102.         'enabled
    103.         EnableCloseButton Me.hWnd, True      
    104.  End Sub
    105.  
    106. Private Sub Command1_Click()
    107.       'disabled
    108.        EnableCloseButton Me.hWnd, False
    109. End Sub

  4. #4
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Why can't I disable the close (X) button on the right-top side ?

    Why don't you just intercept the Form Closing Event instead of Sub-Clasing the form:

    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     If UnloadMode = vbFormControlMenu Or UnloadMode = 1 Then
    3.         'the X has been clicked or the user has pressed Alt+F4
    4.         Cancel = True
    5.         Me.Hide
    6.     End If
    7. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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

    Re: Why can't I disable the close (X) button on the right-top side ?

    Quote Originally Posted by Mark Gambo
    Why don't you just intercept the Form Closing Event instead of Sub-Clasing the form:

    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     If UnloadMode = vbFormControlMenu Or UnloadMode = 1 Then
    3.         'the X has been clicked or the user has pressed Alt+F4
    4.         Cancel = True
    5.         Me.Hide
    6.     End If
    7. End Sub
    This certainly would be a lot easier than running through all of the code required to disable the close button.

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Why can't I disable the close (X) button on the right-top side ?

    Quote Originally Posted by Hack
    This certainly would be a lot easier than running through all of the code required to disable the close button.
    Although the code posted by chu2654 does work if the variable & constant noted in Post #2 are declared.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Why can't I disable the close (X) button on the right-top side ?

    Quote Originally Posted by Hack
    This certainly would be a lot easier than running through all of the code required to disable the close button.

    But Hack's point is a good one, sometimes subclassing a form causes other problems especially during development.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Why can't I disable the close (X) button on the right-top side ?

    Quote Originally Posted by Mark Gambo
    But Hack's point is a good one, sometimes subclassing a form causes other problems especially during development.
    Yeah, I agree. Subclassing can be a nightmare if you're not ultra careful, but chu2654's original code didn't involve any subclassing, neither did Hack's.

    Personally I think your solution is a nice one. Can't get into any trouble there.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  9. #9
    Addicted Member
    Join Date
    Nov 2005
    Posts
    153

    Re: Why can't I disable the close (X) button on the right-top side ?

    Quote Originally Posted by Mark Gambo
    Why don't you just intercept the Form Closing Event instead of Sub-Clasing the form:

    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.     If UnloadMode = vbFormControlMenu Or UnloadMode = 1 Then
    3.         'the X has been clicked or the user has pressed Alt+F4
    4.         Cancel = True
    5.         Me.Hide
    6.     End If
    7. End Sub
    Note From Newbie in the 2nd Line:

    VB Code:
    1. If UnloadMode = vbFormControlMenu Then
    2. Cancel = True
    3. WindowState = 1 ' I minimized it becasue it will be in the SystemTray
    4. End If

    is enough because if there is Code to Unload the form it will not be Unloaded
    Last edited by _Conan_; Dec 31st, 2005 at 11:41 PM.

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

    Re: Why can't I disable the close (X) button on the right-top side ?

    If you remove the system menu's Close menu item it will disable and gray out the 'x'.

    http://www.vbforums.com/showpost.php...71&postcount=2
    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

  11. #11
    Addicted Member
    Join Date
    Nov 2005
    Posts
    153

    Re: Why can't I disable the close (X) button on the right-top side ?

    Quote Originally Posted by RobDog888
    If you remove the system menu's Close menu item it will disable and gray out the 'x'.

    http://www.vbforums.com/showpost.php...71&postcount=2
    it's cool and it will be handy someday

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