Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hWnd As HandleRef, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hWnd As HandleRef) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function EnumWindows(ByVal callback As EnumThreadWindowsCallback, ByVal extraData As IntPtr) As Boolean
End Function
<DllImport("user32.dll", ExactSpelling:=True)> _
Private Shared Function EnumChildWindows(ByVal hwndParent As HandleRef, ByVal lpEnumFunc As EnumChildrenCallback, ByVal lParam As HandleRef) As Boolean
End Function
Private Delegate Function EnumThreadWindowsCallback(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Private Delegate Function EnumChildrenCallback(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Private Function EnumWindowsCallback(ByVal handle As IntPtr, ByVal extraParameter As IntPtr) As Boolean
Dim num1 As Integer = GetWindowTextLength(New HandleRef(Me, handle)) * 2
Dim builder1 As New StringBuilder(num1)
GetWindowText(New HandleRef(Me, handle), builder1, builder1.Capacity)
System.Console.WriteLine(String.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()))
Application.DoEvents()
listBox1.Items.Add(String.Format("Wnd:{0} Title: {1}", handle, builder1.ToString()))
EnumChildWindows(New HandleRef(Me, handle), New EnumChildrenCallback(AddressOf EnumChildWindowsCallback), New HandleRef(Nothing, IntPtr.Zero))
Return True
End Function
Private Function EnumChildWindowsCallback(ByVal handle As IntPtr, ByVal lparam As IntPtr) As Boolean
Dim num1 As Integer = GetWindowTextLength(New HandleRef(Me, handle)) * 2
Dim builder1 As New StringBuilder(num1)
GetWindowText(New HandleRef(Me, handle), builder1, builder1.Capacity)
System.Console.WriteLine(String.Format("/tSubWnd:{0} Title: {1}", handle, builder1.ToString()))
listBox1.Items.Add(String.Format("SubWnd:{0} Title: {1}", handle, builder1.ToString()))
Return True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim callback1 As New EnumThreadWindowsCallback(AddressOf EnumWindowsCallback)
EnumWindows(callback1, IntPtr.Zero)
End Sub
End Class
VB.net 2010
【VB.net遍历窗口及子窗口的控件.rar】点击下载此文件