如何使用FindWindow在VBA中查找具有部分名称的可见或不可见的窗口 [英] How to use FindWindow to find a visible or invisible window with a partial name in 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)
Ex。窗口名称是第一次PlusApi_Excel Sample_17_39_12 Api生成的订单
,第二次将为PlusApi_Excel Sample_17_45_13 Api生成的订单 / code>
我想我需要调用窗口的部件名称,但我不知道如何善待帮助我
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屋!