如何使用 FindWindow 在 VBA 中查找具有部分名称的可见或不可见窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25098263/
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
How to use FindWindow to find a visible or invisible window with a partial name in VBA
提问by Arun Kumar C M
I am using the Windows API with Excel VBA to work with a particular window, using the FindWindow()function, but FindWindow()requires the full title/caption of the window to find.
我正在使用 Windows API 和 Excel VBA 来处理特定窗口,使用该FindWindow()函数,但FindWindow()需要窗口的完整标题/标题才能找到。
Question 1
问题 1
P_Win = FindWindow(vbNullString, "PlusApi_Excel Sample_17_39_12 Api Generated Orders")in my case the window will change the name (dynamic) (some part of the window name will be fixed and some part will be dynamic) 
P_Win = FindWindow(vbNullString, "PlusApi_Excel Sample_17_39_12 Api Generated Orders")在我的情况下,窗口将更改名称(动态)(窗口名称的某些部分将是固定的,而某些部分将是动态的) 
Ex. The window name is first time "PlusApi_Excel Sample_17_39_12 Api Generated Orders"and second time it will be "PlusApi_Excel Sample_17_45_13 Api Generated Orders"I think I need to call window with part name but I don't know how to do with kindly help me 
前任。窗口名称是第一次"PlusApi_Excel Sample_17_39_12 Api Generated Orders",第二次是"PlusApi_Excel Sample_17_45_13 Api Generated Orders"我想我需要用部件名称调用窗口,但我不知道该怎么做,请帮助我
Question 2
问题2
Above challenge I have one more problem the PlusApi will be hidden but my code shows still a positive value.
以上挑战我还有一个问题,PlusApi 将被隐藏,但我的代码仍显示正值。
I think I need to call "visible"window only.
我想我"visible"只需要打电话给窗口。
回答by djikay
I found the following code in this vbforums.com answerand enhanced it to look for visible or invisiblewindowsas well, therefore hopefully answering both your questions:
我在此 vbforums.com 答案中找到了以下代码,并对其进行了增强以查找可见或不可见的窗口,因此希望能回答您的两个问题:
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
The code searches for a window with a partial title of "Excel" and tells you if it found it and if it's a visible window or not. You should be able to adapt it for your own purposes.
该代码搜索部分标题为“Excel”的窗口,并告诉您是否找到它以及它是否是可见窗口。您应该能够根据自己的目的对其进行调整。

