Click to See Complete Forum and Search --> : How can I disable the close button of a form??


kevin shen
August 31st, 2001, 01:50 PM
How can I disable the close button of a form? And How can I disable the close item in system menu?

Thank you for your help!

Best Regards,

Kevin Shen

urs
August 31st, 2001, 03:14 PM
'Cancel.Txt
'Disables the Cancel icon from you VB window

'Declarations Section
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Const MF_BYPOSITION = &H400&
Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&

Private Sub Form_Load()
' Disable the Close button on load
Dim hwndParent As Long
hwndParent = Me.hWnd
DisableItem hwndParent, "&Close", 6
'Disable button before Showing form so that form displays with button already disabled
Me.Show
End Sub

Sub DisableItem(hWnd As Long, sMenuCaption As String, iMenuPos As Integer)
'User-defined function to disable the Close button on the
'MDI Child Form toolbar.
Dim hMenu As Long
Dim hItem As Long

hMenu = GetSystemMenu(hWnd, 0)
hItem = GetMenuItemID(hMenu, iMenuPos)
ModifyMenu hMenu, hItem, MF_BYCOMMAND Or MF_GRAYED, -9, sMenuCaption
End Sub