http://zhidao.baidu.com/question/90619250.html
http://www.itgrass.com/a/vb/VB-sl/200801/12-3390.html
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long,ByVal lpString As String,ByVal cch As Long) As Long Private Sub Form_Load() Dim WinTitleStr As String WinTitleStr = Space(255) GetWindowText hwnd,a,255 Text1 = WinTitleStr End Sub 这个例子是用来取得form中标题
(获取单个窗口的所有内容)添加一个List1
Option Explicit Private Const GW_CHILD = 5 Private Const GW_HWNDFIRST = 0 Private Const GW_HWNDNEXT = 2 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long,ByVal hWnd2 As Long,ByVal lpsz1 As String,ByVal lpsz2 As String) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long,ByVal cch As Long) As Long Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long,ByVal lpClassName As String,ByVal nMaxCount As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long,ByVal wCmd As Long) As Long Private Sub FillChild(hWndParent As Long) Dim hWndChild As Long Dim szCaption As String Dim buffer As String Dim i As Long hWndChild = GetWindow(hWndParent,GW_CHILD) If (hWndChild = 0) Then Exit Sub hWndChild = GetWindow(hWndChild,GW_HWNDFIRST) If hWndChild = 0 Then Exit Sub While (hWndChild <> 0) szCaption = String$(255,0) GetClassName hWndChild,szCaption,250 szCaption = Left$(szCaption,InStr(szCaption,String$(1,0)) - 1) buffer = CStr(hWndChild) & "--" & szCaption i = GetWindowTextLength(hWndChild) szCaption = String$(255,0) GetWindowText hWndChild,i) buffer = buffer & "--" & szCaption List1.AddItem buffer FillChild hWndChild hWndChild = GetWindow(hWndChild,GW_HWNDNEXT) Wend End Sub Private Sub GetChildWindow(hwnd As Long) Dim szCaption As String Dim buffer As String Dim i As Long List1.Clear szCaption = String$(255,0) GetClassName hwnd,0)) - 1) buffer = CStr(hwnd) buffer = buffer & "--" & szCaption i = GetWindowTextLength(hwnd) szCaption = String$(255,0) GetWindowText hwnd,i) buffer = buffer & "--" & szCaption List1.AddItem buffer FillChild hwnd End Sub Private Sub Command1_Click() GetChildWindow FindWindow("#32770","FastCopy ver1.99r4") End Sub
(获取全屏所有窗口信息)添加一个ListView1
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long,ByVal lpsz2 As String) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long,ByVal cch As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,lpRect As RECT) As Long Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type mType '自定义数据类型 fhwnd As Long '窗口句柄 fText As String * 255 '窗口标题 fRect As RECT '窗口矩形 pHwnd As Long '父窗句柄 pText As String * 255 '父窗标题 End Type Private Sub mGetAllWindow(m_Type() As mType) '获取控件信息,写成SUB了,其实用FUNCTION返回值也可以,只是函数里面定义就多了,总的来看需要2个mType数组,这样做只需要一个,占用空间小了 Dim Wndback As Long '上一个被查找的目标句柄 Dim i As Long '数组控制 Do ReDim Preserve m_Type(i) DoEvents m_Type(i).fhwnd = FindWindowEx(0,Wndback,vbNullString,vbNullString) '获取hwnd,第一个参数指定为0,查找桌面子窗口,第2个参数是开始查找的窗口,第34个参数使函数查找所有窗口 If m_Type(i).fhwnd = 0 Then '=0时已经查找一遍了,退出 Exit Sub Else '否则获取控件相关消息 GetWindowText m_Type(i).fhwnd,m_Type(i).fText,255 '获取标题 GetWindowRect m_Type(i).fhwnd,m_Type(i).fRect '获取RECT m_Type(i).pHwnd = GetParent(m_Type(i).fhwnd) '获取父HWND GetWindowText m_Type(i).pHwnd,m_Type(i).pText,255 '获取父标题 End If Wndback = m_Type(i).fhwnd '保存上一个查的句柄 i = i + 1 Loop End Sub Private Sub Command1_Click() Dim cType() As mType mGetAllWindow cType() Dim i As Long ListView1.ListItems.Clear For i = LBound(cType) To UBound(cType) ListView1.ListItems.Add,"a" & i,cType(i).fhwnd ListView1.ListItems("a" & i).SubItems(1) = cType(i).fText ListView1.ListItems("a" & i).SubItems(2) = cType(i).fRect.Left ListView1.ListItems("a" & i).SubItems(3) = cType(i).fRect.Bottom ListView1.ListItems("a" & i).SubItems(4) = cType(i).fRect.Top ListView1.ListItems("a" & i).SubItems(5) = cType(i).fRect.Right ListView1.ListItems("a" & i).SubItems(6) = cType(i).pHwnd ListView1.ListItems("a" & i).SubItems(7) = cType(i).pText Next End Sub Private Sub Form_Load() ListView1.ColumnHeaders.Add,"句柄",1200 ListView1.ColumnHeaders.Add,"标题",2800 ListView1.ColumnHeaders.Add,"Rect.Left",800 ListView1.ColumnHeaders.Add,"Rect.Bottom","Rect.Top","Rect.Right","父窗句柄","父窗标题",2800 ListView1.View = lvwReport ListView1.FullRowSelect = True Command1.Caption = "刷新" End Sub