加载中…
个人资料
VB乐园
VB乐园
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,844
  • 关注人气:18
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VB通过遍历枚举所有窗口获取指定窗口句柄

(2012-02-11 20:47:52)
标签:

it

    Private Declare Function GetDesktopWindow Lib "user32" () As Integer
    Private Declare Function GetForegroundWindow Lib "user32" () As Integer
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
    Private Const GW_CHILD = 5
    Private Const GW_HWNDNEXT = 2

 

    Public Function GetHandle() As Long
        Dim tmp As String
        Dim hwnd As Long
        Dim strTitle As New String(Chr(0), 255)     '用来存储窗口的标题
        '取得桌面窗口
        hwnd = GetDesktopWindow()
        '取得桌面窗口的第一个子窗口
        hwnd = GetWindow(hwnd, GW_CHILD)
        '通过循环来枚举所有的窗口
        Do While hwnd <> 0
            '取得下一个窗口的标题,并写入到列表框中
            GetWindowText(hwnd, strTitle, Len(strTitle))
            If Strings.Left(strTitle, 1) <> vbNullChar Then
                '取得有空字符的窗口标题
                tmp = Strings.Left(strTitle, InStr(1, strTitle, vbNullChar))
                If Strings.Left(tmp, 12) = "神鬼传奇新篇全球公测版 " Then
                    GetHandle = hwnd
                    Exit Do
                End If
            End If
            '调用GetWindow函数,来取得下一个窗口
            hwnd = GetWindow(hwnd, GW_HWNDNEXT)
        Loop
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Hex(GetHandle)
    End Sub
End Class
 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有