vb问题,如何获取窗口标题及对应的pid值
发布网友
发布时间:2022-05-10 06:51
我来回答
共4个回答
热心网友
时间:2023-11-17 22:13
Option Explicit
'获取窗体标题名声明
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Declare Function GetDesktopWindow Lib "user32" () 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 GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Type PROCESSENTRY32
dwsize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32MoleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 260
End Type
'获取进程名pid声明
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmole = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmole
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'函数功能:获取窗体标题
'参数说明:WndTitle 窗体标题
'返回值:输入某个窗体名的时候显示它的pid值,不输人任何参数则获取所有窗体的标题名
Function FindWindow_EX(Optional ByVal WndTitle As String)
Dim level, iFound, Resu
Dim hWnd As Long, K As Long
Dim sWindowText As String
Dim sClassname As String
Dim sID
hWnd = GetWindow(GetDesktopWindow, GW_CHILD)
Do Until hWnd = 0
DoEvents
'sWindowText = GetWinText(hWnd)
sWindowText = Space$(254)
K = GetWindowText(hWnd, sWindowText, 254)
sWindowText = StrConv(LeftB(StrConv(sWindowText, vbFromUnicode), K), vbUnicode)
If sWindowText = WndTitle Or WndTitle = vbNullString Then
If WndTitle <> "" Then '返回窗口句柄
Resu = hWnd
Exit Do
Else '获取所有窗体的标题名
If sWindowText <> "" Then Resu = Resu & sWindowText & vbNewLine
End If
End If
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop
FindWindow_EX = Resu
End Function
'函数功能:获取进程名pid
'参数说明:ProcName 进程名称
'返回值:进程名的pid值,如有多个则分行显示出来.
Function GetPIDFromProcName(ByVal ProcName As String) As String '根据路径获取被监控进程的进程句柄
Dim lPid As Long, S As String, i As Integer
Dim Proc As PROCESSENTRY32, hSnapshot As Long
Dim mSnapshot As Long
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '创建一个snapshot对象
Proc.dwsize = Len(Proc)
lPid = ProcessFirst(hSnapshot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据
Do While lPid <> 0
mSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPmole, Proc.th32ProcessID)
S = Proc.szExeFile
i = InStr(S, Chr(0))
If i Then S = Left(S, i - 1)
If LCase(ProcName) = LCase(S) Then '如果找到了,则…
GetPIDFromProcName = GetPIDFromProcName & Proc.th32ProcessID & vbNewLine
End If
CloseHandle (mSnapshot)
lPid = ProcessNext(hSnapshot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据
Loop
CloseHandle hSnapshot
End Function
热心网友
时间:2023-11-17 22:14
用
GetWindowThreadProcessId
和
GetWindowText
热心网友
时间:2023-11-17 22:14
看书吧
热心网友
时间:2023-11-17 22:15
大哥 API 太多 说了你也看不明白
还是看书吧