gpt4 book ai didi

excel - 禁用 VBA 用户窗体 'x' ,但仍允许卸载我

转载 作者:行者123 更新时间:2023-12-04 21:09:28 26 4
gpt4 key购买 nike

我有一个用户表单,在关闭时需要运行清理步骤。我想要 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 , GetSystemMenuFindWindow
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

截图 :

enter image description here

关于excel - 禁用 VBA 用户窗体 'x' ,但仍允许卸载我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15808997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com