VBA 如何在有 Windows 句柄时使用 FindWindowEx [英] VBA How to use FindWindowEx when have Windows Handle
问题描述
我查看了当您提交问题时弹出的众多类似问题",但不幸的是,这些问题都不适合我的问题,而且它们都是用 c++ 或 c# 编写的.
我发现
我的问题现在是我如何使用这个句柄在这个窗口点击否":
我的代码正在努力检索句柄而不会出错(我假设句柄输出是正确的),但是我不确定去哪里,去哪里寻找有关如何使用句柄单击否"按钮的帮助.
非常感谢为我指明正确方向的任何帮助.
选项显式私有声明函数 FindWindow Lib "User32" 别名 "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long私有声明函数 GetWindowText Lib "User32" 别名 "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long私有声明函数 GetWindowTextLength Lib "User32" 别名 "GetWindowTextLengthA" (ByVal hWnd As Long) As Long私有声明函数 GetWindow Lib "User32" (ByVal hWnd As Long, ByVal uCmd As Long) As Long私有声明函数 IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean私有常量 GW_HWNDNEXT = 2私有子 GetWindowHandle()Dim lhWndP 一样长如果 GetHandleFromPartialCaption(lhWndP, "SAP GUI for Windows 740") = True 那么如果 IsWindowVisible(lhWndP) = True 那么MsgBox "找到可见的窗口句柄:" &lhWndP, vbOKOnly + vbInformation别的MsgBox "找到不可见的窗口句柄:" &lhWndP, vbOKOnly + vbInformationDebug.Print lhWndP万一别的MsgBox "未找到窗口!", vbOKOnly + vbExclamation万一结束子私有函数 GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As BooleanDim lhWndP 一样长将 sStr 调暗为字符串GetHandleFromPartialCaption = FalselhWndP = FindWindow(vbNullString, vbNullString) '父窗口执行同时 lhWndP <>0sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))GetWindowText lhWndP, sStr, Len(sStr)sStr = Left$(sStr, Len(sStr) - 1)如果 InStr(1, sStr, sCaption, vbTextCompare) >0 那么GetHandleFromPartialCaption = TruelWnd = lhWndP退出做万一lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)环形结束函数
感谢 @TinMan 和 @Remy 为我指明了正确的方向.感谢您没有给我答案并允许我进行研究..
以下代码完美无缺,将完全替换我之前的代码.
请注意,下面的代码更简洁、更简洁.
选项显式私有声明函数 FindWindow Lib "user32" 别名 "FindWindowA" _(ByVal lpClassName As String, ByVal lpWindowName As String) As Long私有常量 WM_COMMAND = &H111私有常量 IDNO = 7公共声明 PtrSafe 函数 SendMessage Lib "user32.dll" 别名 "SendMessageW" ( _ByVal hwnd As LongPtr, _ByVal wMsg As Long, _ByVal wParam As LongPtr, _ByVal lParam As LongPtr) As LongPtrSub PressNo_SAPTimeout_Wnd()Dim hwnd As Longhwnd = FindWindow(vbNullString, "SAP GUI for Windows 740")如果 (hwnd <> 0) 那么SendMessage hwnd、WM_COMMAND、IDNO、ByVal 0&别的MsgBox "查找消息框时出错!", vbOKOnly万一结束子
I have looked over the numerous "similiar questions" that pop up when you submit a question but unfortunately none of these fit my problem, plus they are all in c++ or c#.
I found this and it has helped me get the handle:
My problem now is that how am I to use this handle to click "No" on this window:
My code below is working to retrieve the handle without error (I assume the handle output is correct), however I am unsure where to go, where to look for help on how to use the handle to click the "No" button.
Any help to point me in the right direction is much appreciated.
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
Thanks to @TinMan and @Remy for pointing me in the right direction. Thank you for not giving me the answer and allowing me to research..
The following code works flawlessly and will replace my previous code in it entirety.
Note this code below is much neater and more succinct.
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_COMMAND = &H111
Private Const IDNO = 7
Public Declare PtrSafe Function SendMessage Lib "user32.dll" Alias "SendMessageW" ( _
ByVal hwnd As LongPtr, _
ByVal wMsg As Long, _
ByVal wParam As LongPtr, _
ByVal lParam As LongPtr) As LongPtr
Sub PressNo_SAPTimeout_Wnd()
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "SAP GUI for Windows 740")
If (hwnd <> 0) Then
SendMessage hwnd, WM_COMMAND, IDNO, ByVal 0&
Else
MsgBox "Error finding message box!", vbOKOnly
End If
End Sub
这篇关于VBA 如何在有 Windows 句柄时使用 FindWindowEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!