当宏仍在导入工作表时,我创建了一个userform来显示进度条。
问题是用户可以按下红色的X按钮,该按钮将关闭并中断所完成的处理。
有没有办法隐藏这个红色的厄运按钮,以便潜在用户在运行时不会有任何令人困惑的按钮可点击。
编辑:
我试过这个
'Find the userform's Window
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
'Get the current window style
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
'Set the new window style
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Const GWL_STYLE = -16
Const WS_SYSMENU = &H80000
我在userform_initialize上用了这个
Dim hWnd As Long, lStyle As Long
'Which type of userform
If Val(Application.Version) >= 9 Then
hWnd = FindWindow("ThunderDFrame", Me.Caption)
Else
hWnd = FindWindow("ThunderXFrame", Me.Caption)
End If
'Get the current window style and turn off the Close button
lStyle = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, (lStyle And Not WS_SYSMENU)
我收到了这条错误消息
这段代码取自这里。我不知道我做错了什么,我已经删除了评论。这是我找到的最简单的代码,所以我想将它集成到我的userform中。任何帮助都是非常感谢的。
发布于 2013-03-01 08:38:06
下面是您可以这样调用的例程:
subRemoveCloseButton MyForm
或者从你的身体里:
subRemoveCloseButton Me
下面是您需要的代码:
Private Const mcGWL_STYLE = (-16)
Private Const mcWS_SYSMENU = &H80000
'Windows API calls to handle windows
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If
#If VBA7 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
#Else
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
#End If
#If VBA7 Then
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#Else
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If
Public Sub subRemoveCloseButton(frm As Object)
Dim lngStyle As Long
Dim lngHWnd As Long
lngHWnd = FindWindow(vbNullString, frm.Caption)
lngStyle = GetWindowLong(lngHWnd, mcGWL_STYLE)
If lngStyle And mcWS_SYSMENU > 0 Then
SetWindowLong lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU)
End If
End Sub
发布于 2013-03-01 08:35:29
您可以从以下代码片段中解决这个问题:
选择菜单栏上的cmdClose
按钮,选择光标正在闪烁的View | Code
,输入以下代码:
Private Sub cmdClose_Click()
Unload Me
End Sub
在菜单栏上,选择View | Object
,返回到UserForm。
,允许用户按Esc键关闭表单:
在“属性”窗口中选择cmdClose按钮,将Cancel
属性更改为True
,以防止用户通过单击X按钮关闭表单
打开UserForm
时,右上角有一个X
。除了使用“关闭表单”按钮之外,人们还可以使用X关闭表单。如果您想防止这种情况发生,请按照以下步骤操作。
右键单击UserForm的空部分,从过程下拉列表中选择View | Code
,在右上角选择QueryClose。
在光标闪烁的地方,粘贴下面示例中突出显示的代码
Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the Close Form button!"
End If
End Sub
在菜单栏上,选择View | Object
,返回到UserForm。现在,如果有人在X
中单击UserForm,他们将看到您的消息。
发布于 2016-10-27 13:43:01
这是对“彼得?阿尔贝”的上述回答的改进。
FindWindow
调用进行了改进,使其仅能找到UserForms。原始答案中的函数搜索每个窗口类(例如资源管理器窗口和其他程序的窗口)。因此,当其他程序或资源管理器窗口的名称与UserForm名称相同时,可能会删除它们的x按钮。Private Const mcGWL_STYLE = (-16)
Private Const mcWS_SYSMENU = &H80000
'Windows API calls to handle windows
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#If Win64 Then
Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" ( _
ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#End If
Public Sub RemoveCloseButton(objForm As Object)
Dim lngStyle As LongPtr
Dim lngHWnd As LongPtr
Dim lpClassName As String
lpClassName = vbNullString
If Val(Application.Version) >= 9 Then
lpClassName = "ThunderDFrame"
Else
lpClassName = "ThunderXFrame"
End If
lngHWnd = FindWindow(lpClassName, objForm.Caption)
lngStyle = GetWindowLongPtr(lngHWnd, mcGWL_STYLE)
If lngStyle And mcWS_SYSMENU > 0 Then
SetWindowLongPtr lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU)
End If
End Sub
ThunderDFrame? Excel中的UserForms实际上属于
ThunderDFrame
类,这是2002年以后Microsoft应用程序中所有UserFroms的类。在那之前,是ThunderXFrame
。
https://stackoverflow.com/questions/15153491
复制相似问题