我有一个用户表单,在关闭时需要运行清理步骤。我想要 X
按钮被禁用和/或不可见,但我仍然需要能够卸载表单。我使用了如下代码,但它也阻止了 Unload Me
.
'Disables closing via x button
Sub UserForm_QueryClose(Cancel As Integer, ClsoeMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox ("BLOCKED")
Cancel = True
End If
End Sub
最佳答案
不要使用 UserForm_QueryClose
在这种情况下。使用 API RemoveMenu
, GetSystemMenu
和 FindWindow
This是我最喜欢的 API 网站
删除菜单 : http://allapi.mentalis.org/apilist/RemoveMenu.shtml
获取系统菜单 : http://allapi.mentalis.org/apilist/GetSystemMenu.shtml
查找窗口 : http://allapi.mentalis.org/apilist/FindWindow.shtml
看这个例子
Option Explicit
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub UserForm_Initialize()
Dim Ret As Long
'~~> Change UserForm1 to match your userform's caption
Ret = FindWindow("ThunderDFrame", "UserForm1")
Do While Ret = 0
'~~> Change UserForm1 to match your userform's caption
Ret = FindWindow("ThunderDFrame", "UserForm1")
DoEvents
Loop
RemoveMenu GetSystemMenu(Ret, 0), 6, MF_BYPOSITION
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
截图 :
关于excel - 禁用 VBA 用户窗体 'x' ,但仍允许卸载我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15808997/