Results 1 to 9 of 9

Thread: Disabling close button on MDIchild form

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2001
    Posts
    36

    Disabling close button on MDIchild form

    Is it possible to disable the close button (ie grey it out and remove it from the control menu) on an MDIchild form without disabling anything else.

    Regards

    Jon

  2. #2
    somnath
    Guest
    Hi friend

    try setting the MDIChild's ControlBox propery to False
    from your parent form.

    it is not possible to disable the X button

    but by overriding the mousemove method of MDIChild form
    you might succeed in disabling it manually,
    but appearance cudn't be greyed out


    thanks

    - somnath

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

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2001
    Posts
    36
    Wow Thank you much appreciated

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I've never tried that with an MDI Child Finchy. May I assume, by your response, that it did, in fact, work?

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2001
    Posts
    36
    Ahh

    Thought it did but actually only greys out Close on the control menu. It doesn't disable the x in the top right

    Thanks anyways

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2001
    Posts
    36
    Update

    It does actually work but not when the mdichild form is maximised


    interesting...

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    When the child form is maximized, the X and the Close option on the menu are both available?????

    That is interesting...

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2001
    Posts
    36
    sorry I didn't make it very clear

    when the mdichild form is maximised:
    the 'close' on the control menu is greyed out and is disabled
    the 'x' is not greyed out and is enabled

    when the mdichild form is in any other state:
    both 'close' and 'x' are greyed out and are disabled

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