【问题标题】:How to use FindWindow to find a visible or invisible window with a partial name in VBA如何使用 FindWindow 在 VBA 中查找具有部分名称的可见或不可见窗口
【发布时间】:2014-09-25 17:19:09
【问题描述】:

我正在使用带有 Excel VBA 的 Windows API 来处理特定窗口,使用 FindWindow() 函数,但 FindWindow() 需要窗口的完整标题/标题才能找到。

问题 1

P_Win = FindWindow(vbNullString, "PlusApi_Excel Sample_17_39_12 Api Generated Orders") 在我的情况下,窗口将更改名称(动态)(窗口名称的某些部分将是固定的,而某些部分将是动态的)

例如。窗口名称是第一次"PlusApi_Excel Sample_17_39_12 Api Generated Orders" 第二次是"PlusApi_Excel Sample_17_45_13 Api Generated Orders" 我想我需要用部件名称调用窗口,但我不知道该怎么做,请帮助我

问题 2

除了挑战我还有一个问题,PlusApi 将被隐藏,但我的代码仍然显示为正值。

我想我只需要打电话给"visible" window。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我在this vbforums.com answer 中找到了以下代码,并增强了它以查找可见或不可见窗口,因此希望能回答您的两个问题:

    Option Explicit
    
    Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean
    
    Private Const GW_HWNDNEXT = 2
    
    Private Sub Test()
    
        Dim lhWndP As Long
        If GetHandleFromPartialCaption(lhWndP, "Excel") = True Then
            If IsWindowVisible(lhWndP) = True Then
              MsgBox "Found VISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
            Else
              MsgBox "Found INVISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
            End If
        Else
            MsgBox "Window 'Excel' not found!", vbOKOnly + vbExclamation
        End If
    
    End Sub
    
    Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
    
        Dim lhWndP As Long
        Dim sStr As String
        GetHandleFromPartialCaption = False
        lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
        Do While lhWndP <> 0
            sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
            GetWindowText lhWndP, sStr, Len(sStr)
            sStr = Left$(sStr, Len(sStr) - 1)
            If InStr(1, sStr, sCaption) > 0 Then
                GetHandleFromPartialCaption = True
                lWnd = lhWndP
                Exit Do
            End If
            lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
        Loop
    
    End Function
    

    代码搜索部分标题为“Excel”的窗口,并告诉您是否找到它以及它是否是可见窗口。您应该能够根据自己的目的对其进行调整。

    【讨论】:

    • 区分大小写,但这很容易通过将GetHandle... 函数中的InStr 行更改为:If InStr(1, sStr, sCaption, vbTextCompare) &gt; 0 Then
    • 是否需要在我的 Excel 文件中添加对某些内容的引用才能使用它? (即工具>参考)
    • @TekiusFanatikus:无需添加任何特殊参考,只需一个标准的空白工作簿即可使用。
    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多