只需输入API VBA代码即可按标题/标题捕获特定窗口的所有句柄 [英] Nee a code of API VBA to capture all handles of particular window by caption/Title

查看:801
本文介绍了只需输入API VBA代码即可按标题/标题捕获特定窗口的所有句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上搜索很多,但没有找到解决方案.我需要通过该窗口的标题/标题来获取特定窗口的所有API句柄.

I have search lot on the internet but I didn't find the solution. I need to get all API handles of particular window by caption/title of that window.

我有一个代码,但是它捕获了所有打开的窗口的所有句柄.

I have a code but it captures all handle of all open windows.

    Public Sub GetWindows()
    x = 0
    winOutputType.winHandle = 0
    winOutputType.winClass = 1
    winOutputType.winTitle = 2
    winOutputType.winHandleClass = 3
    winOutputType.winHandleTitle = 4
    winOutputType.winHandleClassTitle = 5

    GetWinInfo 0&, 0, winOutputType.winHandleClassTitle
End Sub

我需要代码来询问窗口名称,然后捕获该特定窗口的句柄.

I need code that will ask me window name and then capture handles of that particular window.

推荐答案

我正在寻找,但找不到链接.我到这里了.对于希望寻找类似查询的未来用户,这应该对您有帮助.

I was looking for a duplicate for Close all VBE windows (VB for Aplications) but couldn't find a link. I ended up here. For future users looking for a similar query, this should help you/them.

Option Explicit

Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr

Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As LongPtr

Public Sub GetWindows()
    '~~> Pass Full Name or Partial Name. This is not case sensitive
    Debug.Print GetAllWindowHandles("Microsoft Visual Basic")
End Sub

Private Function GetAllWindowHandles(partialName As String)
    Dim hWnd As Long, lngRet As Long
    Dim strText As String

    hWnd = FindWindowEx(0&, 0&, vbNullString, vbNullString)

    While hWnd <> 0
        strText = String$(100, Chr$(0))
        lngRet = GetWindowText(hWnd, strText, 100)

        If InStr(1, strText, partialName, vbTextCompare) > 0 Then
            Debug.Print "The Handle of the window is " & hWnd & " and " & vbNewLine & _
                        "The title of the window is " & Left$(strText, lngRet) & vbNewLine & _
                        "----------------------"
        End If

        '~~> Find next window  
        hWnd = FindWindowEx(0&, hWnd, vbNullString, vbNullString)
    Wend
End Function

这篇关于只需输入API VBA代码即可按标题/标题捕获特定窗口的所有句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆