WoundEdGoat
Programmer
Can someone tell me how to disable the close button on a form and still have the use of the minimize button?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private Declare Function GetSystemMenu Lib "user32" ( _
ByVal hwnd As Long, _
ByVal bRevert As Long _
) As Long
Private Declare Function GetMenuItemCount Lib "user32" ( _
ByVal hMenu As Long _
) As Long
Private Declare Function RemoveMenu Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long _
) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Declare Function InsertMenuItem Lib "user32" _
Alias "InsertMenuItemA" ( _
ByVal hMenu As Long, _
ByVal un As Long, _
ByVal bool As Boolean, _
ByRef lpcMenuItemInfo As MENUITEMINFO _
) As Long
Private Const MF_DISABLED = &H2&
Private Const MF_BYPOSITION = &H400&
Private Const SC_CLOSE = &HF060&
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type
Private Sub Command1_Click()
DisableX Me
End Sub
Private Sub Command2_Click()
EnableX Me
End Sub
Private Function EnableX(frm As Form)
Dim hMenu As Long, TempMenu As MENUITEMINFO
'get handle again
hMenu = GetSystemMenu(frm.hwnd, 1)
'replace close button
InsertMenuItem hMenu, SC_CLOSE, 0, TempMenu
'redraw menu
DrawMenuBar frm.hwnd
End Function
Public Function DisableX(frm As Form)
Dim hMenu As Long, nCount As Long
'Get handle to system menu
hMenu = GetSystemMenu(frm.hwnd, 0)
'Get number of items in menu
nCount = GetMenuItemCount(hMenu)
'Remove last item from system menu (last item is 'Close')
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)
'Redraw menu
DrawMenuBar frm.hwnd
End Function