VB VBA遍歷指定窗體所有控件

2017-09-20 08:00:00
硬軟兼得的博客
轉貼
1997

用VB 遍歷 指定程序窗口下 所有控件的句柄及控件中的文字

就是讀取另一箇程序裡標籤顯示的文字代碼...
當前前提是知道窗口句柄以及控件句柄纔能讀取到

1. 標籤有句柄,是由 SetWindowText 實現的(其實從底層一點看,還是髮送瞭 WM_SETTEXT 消息),
例如VC、Masm 32 的程序。這種情況好解決,GetWindowText 或 髮送WM_GETTEXT消息就OK瞭;

2. TextOut 畵上去的(例如 VB 就是這樣的)。這種情況要 hook TextOut;


GetWindowText()或SendMessage()都無法取得vb程序的label的文字,因爲vb的label沒有handle, 但大傢髮現 KingSoft CIBA 可以取得vb的label值,這是因爲 KingSoft CIBA 攔下瞭Win32API中的textOut函數


用VB調用API函數 遍歷窗口下所有控件及取牠的文本內容


Private Const GW_CHILD = 5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, 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, szCaption, 250
szCaption = Left$(szCaption, 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, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)

buffer = CStr(hwnd)
buffer = buffer & "--" & szCaption
i = GetWindowTextLength(hwnd)
szCaption = String$(255, 0)
GetWindowText hwnd, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer & "--" & szCaption
List1.AddItem buffer
FillChild hwnd
End Sub

調用方法
GetChildWindow hwnd'hwnd是指定的窗口句柄

結果以

窗體句柄--窗體類名稱--窗體Text

形式列在列錶框List1中,沒時間放到TreeView裡瞭

隻能穫取有句柄項的內容,VB的Label是穫取不到的,這箇隻能做攔截
VC的STATIC上內容可以被穫取
Text的內容也無法穫取,這箇要靠 WM_GETTEXT來穫取
Button的內容可穫取

 

 

 

===

 

添加一箇按鈕,一箇listview,代碼如下,保證好用

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 GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, 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", 800
   ListView1.ColumnHeaders.Add , , "Rect.Top", 800
   ListView1.ColumnHeaders.Add , , "Rect.Right", 800
   ListView1.ColumnHeaders.Add , , "父窗句柄", 1200
   ListView1.ColumnHeaders.Add , , "父窗標題", 2800
   ListView1.View = lvwReport
   ListView1.FullRowSelect = True
   Command1.Caption = "刷新"
End Sub
 

EnumWindows
getwindowtext
分享