VB获得指定xxx.exe进程的窗口标题(包括子窗体)

指定xxx.exe进程可能为多个。请写出详细代码。... 指定xxx.exe进程可能为多个。
请写出详细代码。
展开
 我来答
吹花PM
2012-08-04 · TA获得超过946个赞
知道小有建树答主
回答量:635
采纳率:0%
帮助的人:606万
展开全部
我在这里告诉楼上的几位朋友,这是完全可能的,因为此前我研究过这个问题,经过几番周折已经找出了方法,今天又经过几番周折我把核心部分摘出来了。
注:不要怪楼上几位朋友,因为这的确是个很复杂的过程,网上也找不到实例(我之前找了很久,没找到才自己动手),几乎是用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 = Form1.pid 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为程序名,如notepad.exe
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 my.dwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, my.szExeFile, Chr(0)) '返回chr(0)在各个进程中出现的位置
mName = LCase(Left(my.szExeFile, i - 1)) '返回小写的(返回i-1的前n个字符,即正确的名称)
If mName = a Then pid = my.th32ProcessID '得到启动程序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
List1.AddItem 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个隐藏窗口
追问

感谢 caizifengliua的回答,我也相信一定可以的。

还有个小问题就是现在如果有多个xxx.exe进程的情况下怎么办?

它只会获取1个进行的信息。

追答
把回调函数部分改一下就OK了
匿名用户
2012-08-04
展开全部
enumwindows
enumchildwindows
getwindowtext
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
信心安J
2012-08-03 · TA获得超过402个赞
知道小有建树答主
回答量:787
采纳率:0%
帮助的人:722万
展开全部
API无所不能
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2012-08-03
展开全部
不可以的了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
冬晨夏语
2012-08-04
知道答主
回答量:12
采纳率:0%
帮助的人:4.4万
展开全部
不可以
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
头发头发头发啊
2012-08-04
知道答主
回答量:25
采纳率:0%
帮助的人:9.1万
展开全部
不可以
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 4条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式