EnumThreadWindows VB声明 Declare Function EnumThreadWindows Lib "user32" Alias "EnumThreadWindows" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long 说明 枚举与指定任务相关的窗口 返回值 Long,非零表示成功,零表示失败 参数表 参数 类型及说明 dwThreadId Long,某线程的标识符,它的窗口将被枚举 lpfn Long,指向一个函数的指针,要求为每个子窗口都调用这个函数。用AddressOf运算符获得函数在标准模式下的地址 lParam Long,在枚举期间,传递给dwcbkd32d.ocx定制控件之EnumWindows事件的值。这个值的含义是由程序员规定的 注解 子窗口下属的其他子窗口也可由这个函数枚举
'Enum Classnames 'in a form Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: [email protected] Dim ThreadID As Long, ProcessID As Long ' receive id to thread and process of Form1 ' Determine the thread which owns this window ThreadID = GetWindowThreadProcessId(Me.hWnd, ProcessID) ' Use the callback function to list all of the enumerated thrad windows EnumThreadWindows ThreadID, AddressOf EnumThreadWndProc, 0 'Show the results Me.AutoRedraw = True Me.Print sClasses End Sub 'In a module Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn 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 'variable used to list all the classnames Public sClasses As String Public Function EnumThreadWndProc(ByVal hWnd As Long, ByVal lParam As Long) As Long Dim Ret As Long, sText As String 'create a string-buffer sText = Space(255) 'get the classname of the window handle Ret = GetClassName(hWnd, sText, 255) 'cut off the unnecessary part of Chr$(0)'s sText = Left$(sText, Ret) 'add this classname to the list of classnames sClasses = sClasses + sText + vbCrLf 'continue the enumeration EnumThreadWndProc = 1 End Function
findwindow不可以,完全不了解的exe你是无法知道他的窗口名or类名的。to 楼主:
一个进程不见得有窗口,or 有可能有很多窗口,所以你问题问的不太明确!单纯传入PID而获得hwnd的API据我所知应该是没有,所以还是要用EnumWindows来列举所有的窗口,每得到一个hwnd就用GetWindowThreadProcessId来得到拥有它的进程的PID,和你的PID比较,是一样的话就是了!
Declare Function EnumThreadWindows Lib "user32" Alias "EnumThreadWindows" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
说明
枚举与指定任务相关的窗口
返回值
Long,非零表示成功,零表示失败
参数表
参数 类型及说明
dwThreadId Long,某线程的标识符,它的窗口将被枚举
lpfn Long,指向一个函数的指针,要求为每个子窗口都调用这个函数。用AddressOf运算符获得函数在标准模式下的地址
lParam Long,在枚举期间,传递给dwcbkd32d.ocx定制控件之EnumWindows事件的值。这个值的含义是由程序员规定的
注解
子窗口下属的其他子窗口也可由这个函数枚举
'Enum Classnames
'in a form
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim ThreadID As Long, ProcessID As Long ' receive id to thread and process of Form1
' Determine the thread which owns this window
ThreadID = GetWindowThreadProcessId(Me.hWnd, ProcessID)
' Use the callback function to list all of the enumerated thrad windows
EnumThreadWindows ThreadID, AddressOf EnumThreadWndProc, 0
'Show the results
Me.AutoRedraw = True
Me.Print sClasses
End Sub
'In a module
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn 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
'variable used to list all the classnames
Public sClasses As String
Public Function EnumThreadWndProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim Ret As Long, sText As String
'create a string-buffer
sText = Space(255)
'get the classname of the window handle
Ret = GetClassName(hWnd, sText, 255)
'cut off the unnecessary part of Chr$(0)'s
sText = Left$(sText, Ret)
'add this classname to the list of classnames
sClasses = sClasses + sText + vbCrLf
'continue the enumeration
EnumThreadWndProc = 1
End Function