gpt4 book ai didi

vba - 使用 VBA 和 VB.NET API 处理多个窗口

转载 作者:行者123 更新时间:2023-12-02 21:27:31 28 4
gpt4 key购买 nike

我尝试通过以下 VBA 代码使用 API,但有多个窗口句柄具有相同的标题和类名,例如“#32770”和“Button”。那么,我怎样才能继续进行下一个处理呢?我已附上 spy 注册表值的屏幕截图,我想访问带有多个按钮标题的第二个窗口句柄,但它们也具有相同的窗口标题和类名称。

screenshot

请参阅随附的多个窗口句柄的屏幕截图。

Sub sbRunCalcUsingAPI()

hwnd = FindWindow(vbNullString, "Calculator")

start_doc = ShellExecute(hwnd, "open", "C:\Windows\system32\calc.exe", 0, 0, SW_NORMAL)

If start_doc = 2 Then Exit Sub
If start_doc = 3 Then Exit Sub

Do
DoEvents
hWnd2 = FindWindow(vbNullString, "Calculator")
Loop Until hWnd2 > 0

main_view = FindWindowEx(hWnd2, 0&, "CalcFrame", vbNullString)
sub_window2 = FindWindowEx(main_view, X&, "#32770", vbNullString)
sub_window2One = FindWindowEx(main_view, 0&, "Button", vbNullString)

End Sub

最佳答案

您需要在此处使用“GetNextWindow()”而不是“FindWindowEx()”。现在您有以下三个选项之一:第一个子窗口、下一个子窗口、或上一个子窗口。每个案例都是独一无二的,您的方法可能需要根据情况进行一些调整关于你的元素的属性。就我个人而言,当类或窗口标题不明确时,我会使用它。我在不明确的窗口标签上方或下方寻找独特的窗口标签然后使用选项之一查找 hwnd。

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long _
) As Long

Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As Long

Public Declare Function GetNextWindow Lib "user32.dll" Alias "GetWindow" ( _
ByVal hwnd As Long, _
ByVal wFlag As Long _
) As Long

Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDPREV = 1
Public Const GW_HWNDNEXT = 2

Public Const SW_NORMAL As Long = 1


Sub sbRunCalcUsingAPI()

Dim x As Long

'You do not need 'Let hwnd = FindWindow(vbNullString, "Calculator") '
'if you are opening it for the first time.
'It is used in cases where you want to open a 'tabbed window', like Internet Explorer.
'You would then find the parent window and open a new tab in it. For your purpose,
'you can leave it blank."

' Let hwnd = FindWindow(vbNullString, "Calculator")

start_doc = ShellExecute( _
0&, _
"open", _
"C:\Windows\system32\calc.exe", _
0, _
0, _
SW_NORMAL _
)

If start_doc = 2 Then Exit Sub
If start_doc = 3 Then Exit Sub

Do
DoEvents
Let hWnd2 = FindWindow( _
vbNullString, _
"Calculator" _
)
Loop Until hWnd2 > 0

Let main_view = FindWindowEx( _
hWnd2, _
0&, _
"CalcFrame", _
vbNullString _
)

Let sub_window2 = FindWindowEx( _
main_view, _
x&, _
"#32770", _
vbNullString _
)


'You will want to use 'GetNextWindow() instead of 'FindWindowEx() here.
'Now you have one of three options: First Child Window, Next Child Window,
'or Previous Child Window
'Every case is unique, and your approach may need some tweaking depending
'on your element's attributes.
'Personally, I use this when the class or window caption is ambiguous.
'I look for a unique window label above or below the ambiguous one
'and then find the hwnd using one of the options.

' Let sub_window2One = FindWindowEx(main_view, 0&, "Button", vbNullString)

'First Child Window
Let sub_window2One = GetNextWindow( _
sub_window2, _
GW_HWNDFIRST _
)
'Previous Child Window
Let VIP_6_Digit_hwnd = GetNextWindow( _
sub_window2, _
GW_HWNDPREV _
)
'Next Child Window
Let VIP_6_Digit_hwnd = GetNextWindow( _
sub_window2, _
GW_HWNDNEXT _
)

End Sub

关于vba - 使用 VBA 和 VB.NET API 处理多个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40056794/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com