如何使用 FindWindow 在 VBA 中查找具有部分名称的可见或不可见窗口 [英] How to use FindWindow to find a visible or invisible window with a partial name in VBA

查看:42
本文介绍了如何使用 FindWindow 在 VBA 中查找具有部分名称的可见或不可见窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有 Excel VBA 的 Windows API 来处理特定窗口,使用 FindWindow() 函数,但 FindWindow() 需要完整的标题/说明要查找的窗口.

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.

问题 1

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

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)

例如.窗口名称为第一次"PlusApi_Excel Sample_17_39_12 Api Generated Orders"第二次是 "PlusApi_Excel Sample_17_45_13 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

问题 2

以上挑战我还有一个问题,PlusApi 将被隐藏,但我的代码仍显示正值.

Above challenge I have one more problem the PlusApi will be hidden but my code shows still a positive value.

我想我只需要调用 "visible" 窗口.

I think I need to call "visible" window only.

推荐答案

我在 这个 vbforums.com 答案增强它以寻找可见或不可见窗口,因此希望回答您的两个问题:

I found the following code in this vbforums.com answer and enhanced it to look for visible or invisible windows as well, therefore hopefully answering both your questions:

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"的窗口,并告诉您是否找到了它以及它是否为可见窗口.您应该能够根据自己的目的对其进行调整.

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.

这篇关于如何使用 FindWindow 在 VBA 中查找具有部分名称的可见或不可见窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆