首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VBA在有Windows句柄时如何使用FindWindowEx

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

Stack Overflow用户
提问于 2019-10-31 00:01:49
回答 1查看 3.8K关注 0票数 1

当您提交问题时,我查看了大量的“相似问题”,但不幸的是,这些问题都不适合我的问题,而且它们都在c++或c#中。

找到了 this,它帮我拿到了把手

我的problem现在是如何使用这个句柄在这个窗口上单击"No“:

下面的代码正在无错误地检索句柄(我假设句柄输出是正确的),但是我不知道该去哪里,在哪里查找如何使用句柄单击"No“按钮的帮助。

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

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-10-31 02:15:41

感谢丁曼和雷米为我指明了正确的方向。谢谢你没有给我答案,让我研究。

下面的代码工作得完美无缺,将完全取代我以前的代码。

注意,下面的代码更整洁、更简洁。

代码语言:javascript
运行
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58635223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档