使用VBA和VB.NET API处理多个窗口
我试图使用带有以下VBA代码的API,但是有使用VBA和VB.NET API处理多个窗口,vba,excel,vb.net-2010,Vba,Excel,Vb.net 2010,我试图使用带有以下VBA代码的API,但是有多个具有相同标题和类名的窗口句柄,如“#32770”和“Button”。那么,我怎样才能转到下一个句柄呢。我附上了spy注册表值的屏幕截图,我想用多个按钮标题访问第二个窗口句柄,但它们也有相同的窗口标题和类名 请参阅附加的多窗口句柄屏幕截图 Sub sbRunCalcUsingAPI() hwnd = FindWindow(vbNullString, "Calculator") start_doc = ShellExecute(hwnd
多个具有相同标题和类名的窗口句柄
,如“#32770”和“Button”。那么,我怎样才能转到下一个句柄呢。我附上了spy注册表值的屏幕截图,我想用多个按钮标题访问第二个窗口句柄,但它们也有相同的窗口标题和类名
请参阅附加的多窗口句柄屏幕截图
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
Guyz,请尽快回答以上问题。。。。