Results 1 to 7 of 7

Thread: "X" box control

Hybrid View

  1. #1
    CoachG
    Guest

    "X" box control

    Is there anyway that I can write code for the "X" box on my forms? I like having that border there but I really would like the "X" to be inactive or not there at all. If it has to be there I would at least like to tell it to shut down the whole program and not leave other things running. Can I do this somehow? Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Whether you can disable the X button depends on the OS. You can trap for it however, in the form unload event.
    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2. 'there are 5 unloadmode levels
    3.         Select Case UnloadMode
    4.             Case vbFormControlMenu 'UnloadMode 0
    5.                  'form is being unloaded via the Close
    6.                  'command from the System menu
    7.                  'or from the X button
    8.             Case vbFormCode        'UnloadMode 1
    9.                  'Unload Me has been issued from code
    10.             Case vbAppWindows      'UnloadMode 2
    11.                  'Windows itself is closing
    12.             Case vbAppTaskManager  'UnloadMode 3
    13.                  'the Task Manager is closing the app
    14.             Case vbFormMDIForm     'UnloadMod 4
    15.                  'an MDI child form is closing because
    16.                  'its parent form is closing
    17.         End Select
    18. End Sub

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    easiest way would be to set controlbox property to false

    you could also use the query unload event to make it useless..

    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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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)

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    ya... i don't have that handy dandy code library

    by the way there isn't anything in it that would pertain to my post on taskbar height/width is there???

    I think the easiest way to get rid of the X button is to can the controlbox.. if you don't need it's other features and the icon.. like an MDI child for example

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    If you just don't want it, I would agree. Some folks, however, like to be able to keep it, but manipulate it.
    by the way there isn't anything in it that would pertain to my post on taskbar height/width is there???
    Which one is that?

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

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