vb问题,如何获取窗口标题及对应的pid值
我只要可以放入模块中使用的代码!1.获取窗体标题名的函数名可以自定义,当我输入某个窗体名的时候显示它的pid值,不输人任何参数则获取所有窗体的标题名2.获取进程名的pid...
我只要可以放入模块中使用的代码!
1.获取窗体标题名的函数名可以自定义,当我输入某个窗体名的时候显示它的pid值,不输人任何参数则获取所有窗体的标题名
2.获取进程名的pid值,如有多个则分行显示出来.
3.解释一下代码的用法以及各个api函数的意思
问题多了点,希望热心高手能帮忙解决这个问题! 展开
1.获取窗体标题名的函数名可以自定义,当我输入某个窗体名的时候显示它的pid值,不输人任何参数则获取所有窗体的标题名
2.获取进程名的pid值,如有多个则分行显示出来.
3.解释一下代码的用法以及各个api函数的意思
问题多了点,希望热心高手能帮忙解决这个问题! 展开
展开全部
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
th32ModuleID 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_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
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_SNAPmodule, 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
'获取窗体标题名声明
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
th32ModuleID 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_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
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_SNAPmodule, 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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用
GetWindowThreadProcessId
和
GetWindowText
GetWindowThreadProcessId
和
GetWindowText
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看书吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大哥 API 太多 说了你也看不明白
还是看书吧
还是看书吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看书吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询