vba 将 Excel 用户窗体显示为任务栏中的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15949023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying an Excel userform as a button in the taskbar
提问by Excel Developers
I want to hide the Excel button in the taskbar and display a separate button for my userform so that it feels like an application on its own. I know this has been covered a lot but I am having trouble with a specific issue: my code works fine when I step through it, but not if I let it run normally. Here is the code, which I have placed in the class module of Userform1:
我想隐藏任务栏中的 Excel 按钮并为我的用户表单显示一个单独的按钮,这样它就感觉像是一个独立的应用程序。我知道这已经涵盖了很多,但我遇到了一个特定问题:我的代码在我单步执行时可以正常工作,但如果我让它正常运行则不行。这是我放在 Userform1 的类模块中的代码:
Option Explicit
Private Declare Function GetWindowLong _
Lib "user32" _
Alias "GetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long) _
As Long
Private Declare Function SetWindowLong _
Lib "user32" _
Alias "SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) _
As Long
Private Declare Function DrawMenuBar _
Lib "user32" ( _
ByVal hWnd As Long) _
As Long
Private Declare Function FindWindowA _
Lib "user32" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As Long
Private Const GWL_EXSTYLE = (-20)
Private Const GWL_STYLE As Long = (-16)
Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_SYSMENU As Long = &H80000
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Sub UserForm_Activate()
Dim lFrmWndHdl As Long
Dim lStyle As Long
lFrmWndHdl = FindWindowA(vbNullString, Me.Caption)
lStyle = GetWindowLong(lFrmWndHdl, GWL_STYLE)
lStyle = lStyle Or WS_SYSMENU
lStyle = lStyle Or WS_MINIMIZEBOX
lStyle = lStyle Or WS_MAXIMIZEBOX
SetWindowLong lFrmWndHdl, GWL_STYLE, (lStyle)
lStyle = GetWindowLong(lFrmWndHdl, GWL_EXSTYLE)
lStyle = lStyle Or WS_EX_APPWINDOW
SetWindowLong lFrmWndHdl, GWL_EXSTYLE, lStyle
DrawMenuBar lFrmWndHdl
AppActivate ("Microsoft Excel")
ThisWorkbook.Application.Visible = False
End Sub
Stepping through the code, when I step into the 2nd to last line AppActivate a separate button appears in the taskbar, and the last line hides the original button for the Excel workbook in the taskbar. I am then left with just a userform that can be maximised or minimised to the taskbar like any normal application. The problem is if I load the userform via code the separate button for the userform does not appear in the taskbar, so there are no Excel buttons left showing in the taskbar.
单步执行代码,当我进入 AppActivate 的倒数第二行时,任务栏中会出现一个单独的按钮,最后一行隐藏了任务栏中 Excel 工作簿的原始按钮。然后我只剩下一个用户窗体,它可以像任何普通应用程序一样最大化或最小化到任务栏。问题是,如果我通过代码加载用户表单,则用户表单的单独按钮不会出现在任务栏中,因此任务栏中不会显示 Excel 按钮。
采纳答案by Excel Developers
To answer my own question: the problem was not in the code I posted above, but in the way the userform was loaded. It should be loaded as modeless.
回答我自己的问题:问题不在于我上面发布的代码,而在于加载用户表单的方式。它应该作为无模式加载。
回答by BigRenegade
Simply put the following code in your main UserForm_Initialize sub and it will minimize the Excel application window while leaving your form open on the desktop.
只需将以下代码放在您的主 UserForm_Initialize 子中,它将最小化 Excel 应用程序窗口,同时让您的表单在桌面上保持打开状态。
Private Sub minimizeWindow()
With Application
.WindowState = xlMinimized
End With
End sub