6

我想隐藏任务栏中的 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

单步执行代码,当我进入倒数第二行 AppActivate 时,任务栏中会出现一个单独的按钮,最后一行隐藏了任务栏中 Excel 工作簿的原始按钮。然后我只剩下一个用户表单,可以像任何普通应用程序一样最大化或最小化到任务栏。问题是如果我通过代码加载用户表单,则用户表单的单独按钮不会出现在任务栏中,因此任务栏中不会显示 Excel 按钮。

4

2 回答 2

1

回答我自己的问题:问题不在于我上面发布的代码,而在于加载用户表单的方式。它应该被加载为无模式。

于 2014-01-16T09:43:29.490 回答
0

只需将以下代码放入您的主 UserForm_Initialize 子程序中,它将最小化 Excel 应用程序窗口,同时让您的表单在桌面上保持打开状态。

Private Sub minimizeWindow()
  With Application
    .WindowState = xlMinimized
  End With
End sub
于 2017-01-11T00:41:24.640 回答