怎样通过按钮读取程序列表中所选程序的句柄?
Option Explicit
'得到窗口的标题条文本
Declare Function GetWindowText Lib "user32 " Alias "GetWindowTextA " (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'枚举所有屏幕上的顶层窗口
Declare Function EnumWindows Lib "user32 " (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
If Len(S) > 0 Then Form1.List1.AddItem S
EnumWindowsProc = True
End Function
Private Sub Form_Load()
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
已经成功读取出程序列表。
接下来怎样做到在列表里选定一个程序,然后点击按钮读取出该程序的句柄?
[解决办法]
模块中:
Option Explicit
'得到窗口的标题条文本
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 EnumWindows Lib "user32 " (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32 " (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
If Len(S) > 0 Then Form1.List1.AddItem S
EnumWindowsProc = True
End Function
Public Sub getWnd()
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
Public Function getPID(strT As String, h As Long) As Long
h = FindWindow(vbNullString, strT)
GetWindowThreadProcessId h, getPID
End Function
窗口中:
Private Sub Command1_Click()
Dim h As Long, pid As Long
If List1.ListIndex > = 0 Then
pid = getPID(List1.List(List1.ListIndex), h)
MsgBox "窗口句柄: " & h & vbCrLf & "所在进程ID: " & pid, vbInformation
End If
End Sub
Private Sub Form_Load()
getWnd
End Sub