[轉貼]如何列舉window handle

VB相關技術
回覆文章
頭像
tim
文章: 1379
註冊時間: 2008年 11月 26日, 00:49

[轉貼]如何列舉window handle

文章 tim »

http://www.xtremevbtalk.com/archive/ind ... 30418.html

在 vb 裡, 利用 EnumChildWindows 及 Call back function 進行列舉 window handle 的程式:

代碼: 選擇全部

Option Explicit
'
' Enumerating and finding child windows
' using FindWindowEx and EnumChildWindows
'
' [email protected]
'
'====================================================================
'
' note: alias API declarations FindWindowEx and SendmessageS
'
'====================================================================
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent _
As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Declare Function SendMessageS Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Declare Function FindWindowX Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As Long, ByVal lpsz2 As Long) As Long

Public Const WM_GETTEXT = &HD


call back:
Function EnumChildWindow(ByVal hChild As Long, ByVal lParam As Long) As Long
 Dim wClass As String, wText As String
 Dim j As Integer

 wClass = Space(64)
 j = GetClassName(hChild, wClass, 63)
 wClass = Left(wClass, j)

 wText = Space(256)
 j = SendMessageS(hChild, WM_GETTEXT, 255, wText)
 wText = Left(wText, j)

 Debug.Print "Enum " & hChild; ", "; wClass;
 If Len(wText) Then Debug.Print ", """; wText; """";
 Debug.Print

 EnumChildWindow = 1 ' Continue enumeration
End Function
多多留言, 整理文章, 把經驗累積下來.....
回覆文章