
Private Declare Function EnumWindows Lib "user32dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
首先枚举所有的窗口。
然后
Private Declare Function EnumWindows Lib "user32dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
判断窗口的所属进程。
建立一个集合。
最后通过索引显示就OK了。
根据你的反馈,回复如下:
假如进程名为12345678exe,则可以分三步解决:
一、定义数组a(),然后枚举所有进程,找出进程名为12345678exe的进程,并将其ID存入数组a()中
二、枚举所有窗口,取得对应的进程ID,然后与数组a()中的ID进行比较,若处于可见状态且ID相同,则将该数组元素置为空。
三、循环检验数组a(),如ID不为空,则终止此进程。
调用SystemDiagnostics;
Process[] process = ProcessGetProcesses();
foreach(Process p in process)
{
ConsoleWriteLine(pMainWindowTitleToString());
}
ConsoleReadLine();
我在这里告诉楼上的几位朋友,这是完全可能的,因为此前我研究过这个问题,经过几番周折已经找出了方法,今天又经过几番周折我把核心部分摘出来了。
注:不要怪楼上几位朋友,因为这的确是个很复杂的过程,网上也找不到实例(我之前找了很久,没找到才自己动手),几乎是用API堆出来的。
'一模块,一窗体,一文本框(用于输入进程名),一列表框(用于显示所有标题),一命令按钮(开始)
'模块部分
Public Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
'枚举窗口
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'获取窗口标题
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _
(ByVal hWnd As Long) As Long
'获取窗口标题长度
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function IsWindowEnabled Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function IsWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Public 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 1024
End Type
Public hwn As Long, SSS As String
Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long '回调函数
Dim sSave As String, Ret As Long, windowProcessId As Long
Ret = GetWindowTextLength(hWnd)
sSave = Space(Ret)
GetWindowText hWnd, sSave, Ret + 1
GetWindowThreadProcessId hWnd, windowProcessId
If windowProcessId = Form1pid Then
If IsWindowEnabled(hWnd) = 1 Then
hwn = hWnd
SSS = SSS & hWnd & "|" '存储所有相关句柄进字符串sss
End If
End If
EnumWindowsProc = 2
End Function
Public Sub draw()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
'窗体部分
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Public pid As Long
Dim pname As String
Dim a As String, hw As Long
'原创函数,返回字符串中字串个数
Function lon(st As String, sr As String) As Long
Dim f As Long, g As Long
For f = 1 To Len(st)
If Mid(st, f, Len(sr)) = sr Then g = g + 1
lon = g
Next f
End Function
'原创函数:返回字符串中第几段字符。例:quduan("23,43,5,23",",",2)=43
Function quduan(pli As String, pl As String, n As Long) As String '取字符串指定段
Dim j As Integer
For i = 1 To Len(pli)
If Mid(pli, i, Len(pl)) = pl Then j = j + 1
Next i
Dim a() As String
a() = Split(pli, pl)
If n > j + 1 Then quduan = "": Exit Function
quduan = a(n - 1)
End Function
Private Sub Command1_Click()
a = LCase(Text1) 'text1为程序名,如notepadexe
Dim my As PROCESSENTRY32
Dim l As Long, l1 As Long, flag As Boolean, mName As String, i As Integer
Dim st As Long, mt As Long, it As Long, jt As Long, nt As String '筛选窗口变量列表
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then mydwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, myszExeFile, Chr(0)) '返回chr(0)在各个进程中出现的位置
mName = LCase(Left(myszExeFile, i - 1)) '返回小写的(返回i-1的前n个字符,即正确的名称)
If mName = a Then pid = myth32ProcessID '得到启动程序PID
Loop Until (Process32Next(l, my) < 1)
End If
draw '模块过程用于枚举窗口句柄与PID对比
'筛选主程序有效窗口
If SSS <> "" Then 'sss为所得主程序句柄集合
Do While it <= lon(SSS, "|")
it = it + 1
st = Val(quduan(SSS, "|", it))
jt = GetWindowTextLength(st)
nt = Space(jt)
GetWindowText st, nt, jt + 1
List1AddItem nt '加载此程序下所有窗体的标题
If nt <> "" And IsWindowVisible(st) = 1 And IsWindow(st) = 1 And IsWindowEnabled(st) = 1 Then
ShowWindow st, 1 '显示可显示的窗口
End If
Loop
End If
End Sub
可获得所有标题,可调出主窗口,通常进程都有好多隐藏的各种各样的窗口,我拿QQ测试发现有17个隐藏窗口
以上就是关于VB根据进程名获取对应窗体标题全部的内容,包括:VB根据进程名获取对应窗体标题、VB怎么获得与程序标题对应的进程名、c# 得到所有打开的窗口程序名和标题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)