excel - VBA如何在有Windows句柄时使用FindWindowEx

标签 excel vba winapi hwnd

我查看了您提交问题时弹出的众多“类似问题”,但不幸的是,这些问题都不适合我的问题,而且它们都使用 c++ 或 c#。

找到 this它帮助我掌握了句柄:

enter image description here

我的 问题 现在是我如何使用此句柄在此窗口上单击“否”:

enter image description here

我的 代码 下面正在努力无错误地检索句柄(我假设句柄输出是正确的),但是我不确定去哪里,在哪里寻找有关如何使用句柄单击“否”按钮的帮助。

非常感谢任何帮助我指出正确方向的帮助。

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

最佳答案

感谢@TinMan 和@Remy 为我指明了正确的方向。感谢您没有给我答案并允许我研究..

以下代码完美运行,将完全替换我以前的代码。

请注意,下面的代码更加简洁明了。

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

关于excel - VBA如何在有Windows句柄时使用FindWindowEx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635223/

相关文章:

python - Pandas read_excel 干扰 na_values 和转换器错误?

vba - 在字典的 excel 中引用字典的内部 - VBA 对象需要错误

excel - VBA Excel 在字符串的第二个字符后插入符号

vba - 使用 Range 时应用程序定义或对象定义错误

c++ - 在 C++ 中使用 Windows Composition 引擎的透明窗口

excel - VBA - 通过循环定义范围变量

java - 将 jTable 选择导出到 Excel 并打开 Excel

excel - 后期绑定(bind)编译错误: User-defined type not defined referencing Outlook mailitem in Excel VBA

c++ - 帮助异步 I/O

c++ - WinApi:如何为屏幕阅读器添加替代文本到所有者绘制的按钮?