Function DisableCloseButton(UF As MSForms.UserForm) As Boolean
Dim UFHWnd As Long
Dim hMenu As Long
Dim ItemCount As Long
Dim Res As Long
UFHWnd = HWndOfUserForm(UF)
If UFHWnd = 0 Then
Exit Function
End If
hMenu = GetSystemMenu(UFHWnd, 0&)
If hMenu = 0 Then
Exit Function
End If
ItemCount = GetMenuItemCount(hMenu)
Res = EnableMenuItem(hMenu, ItemCount - 1, MF_DISABLED Or MF_BYPOSITION)
If Res = -1 Then
Exit Function
End If
DrawMenuBar UFHWnd
DisableCloseButton = True
End Function
Function HideCloseButton(UF As MSForms.UserForm) As Boolean
Dim UFHWnd As Long
Dim WinInfo As Long
Dim R As Long
UFHWnd = HWndOfUserForm(UF)
If UFHWnd = 0 Then
Exit Function
End If
WinInfo = GetWindowLong(UFHWnd, GWL_STYLE)
WinInfo = WinInfo And (Not WS_SYSMENU)
R = SetWindowLong(UFHWnd, GWL_STYLE, WinInfo)
DrawMenuBar UFHWnd
HideCloseButton = (R <> 0)
End Function
Function HWndOfUserForm(UF As UserForm) As Long
Dim AppHWnd As Long
Dim DeskHWnd As Long
Dim WinHWnd As Long
Dim UFHWnd As Long
Dim Cap As String
Dim WindowCap As String
Cap = UF.Caption
UFHWnd = FindWindow(C_USERFORM_CLASSNAME, Cap)
If UFHWnd <> 0 Then
HWndOfUserForm = UFHWnd
Exit Function
End If
AppHWnd = Application.hwnd
UFHWnd = FindWindowEx(AppHWnd, 0&, C_USERFORM_CLASSNAME, Cap)
If UFHWnd <> 0 Then
HWndOfUserForm = UFHWnd
Exit Function
End If
If Application.ActiveWindow Is Nothing Then
HWndOfUserForm = 0
Exit Function
End If
WinHWnd = WindowHWnd(Application.ActiveWindow)
UFHWnd = FindWindowEx(WinHWnd, 0&, C_USERFORM_CLASSNAME, Cap)
HWndOfUserForm = UFHWnd
End Function
Function WindowHWnd(W As Excel.Window) As Long
Dim AppHWnd As Long
Dim DeskHWnd As Long
Dim WHWnd As Long
Dim Cap As String
AppHWnd = Application.hwnd
DeskHWnd = FindWindowEx(AppHWnd, 0&, C_EXCEL_DESK_CLASSNAME, vbNullString)
If DeskHWnd > 0 Then
Cap = WindowCaption(W)
WHWnd = FindWindowEx(DeskHWnd, 0&, C_EXCEL_WINDOW_CLASSNAME, Cap)
End If
WindowHWnd = WHWnd
End Function
Private Sub UserForm_Initialize()
End Sub