【发布时间】:2019-10-31 00:01:49
【问题描述】:
我查看了您提交问题时弹出的众多“类似问题”,但不幸的是,这些问题都不适合我的问题,而且它们都是用 c++ 或 c# 编写的。
我找到this,它帮助我获得了句柄:
我的问题现在是我如何使用这个句柄在这个窗口上点击“否”:
我的 代码 下面正在努力检索 句柄 而没有错误(我假设句柄输出是正确的),但是我不确定去哪里,去哪里寻找帮助如何使用手柄点击“否”按钮。
非常感谢任何能帮助我指明正确方向的人。
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 nMaxCount 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 uCmd As Long) As Long
Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean
Private Const GW_HWNDNEXT = 2
Private Sub GetWindowHandle()
Dim lhWndP As Long
If GetHandleFromPartialCaption(lhWndP, "SAP GUI for Windows 740") = True Then
If IsWindowVisible(lhWndP) = True Then
MsgBox "Found VISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
Else
MsgBox "Found INVISIBLE Window Handle: " & lhWndP, vbOKOnly + vbInformation
Debug.Print lhWndP
End If
Else
MsgBox "Window 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, vbTextCompare) > 0 Then
GetHandleFromPartialCaption = True
lWnd = lhWndP
Exit Do
End If
lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop
End Function
【问题讨论】:
-
我会先尝试使用 winapi 调用
SetFocus然后使用 VBASendKeys "%Y"来单击按钮。如果这不起作用,您将需要使用 winapiEnumChildWindows查找按钮句柄并使用 winapiSendMessage或PostMessage来单击按钮。 -
@TinMan thxs 伙计。感谢指导帮助。我会做更多的研究。干杯
-
@alowflyingpig 见Closing a Yes/No message box via code