VB怎样获得系统打开的其它程序窗口[只知道EXE文件名]

学VB时间不长,想用VB做一个联众军旗的自动走步器。第1步:当军旗[junqi.exe]启动后,桌面就弹出了程序的桌面窗口,但是窗口标题是不固定的[因为换了用户名,标题也... 学VB时间不长,想用VB做一个联众军旗的自动走步器。
第1步:当 军旗[junqi.exe]启动后,桌面就弹出了程序的桌面窗口,但是窗口标题是不固定的[因为换了用户名,标题也会变化},必须先使用VPI函数通过程序内存中启动的EXE文件名 来得到分配到的PID号。
第2步:接着用枚举窗口的方式,使用API 函数EnumWindows和GetWindowThreadProcessId 得到系统里运行顶级窗口的所有句柄和PID号。
如果第1步中的PID和第2步中PID 有一样的,那就是自己要找的那个窗口,那个窗口句柄是。。。?

弄了好几天,代码总是写不成,好象根本无法比较,高分求高手指教,最好给个可运行的代码。
就是说建2个文本框,1个命令按纽,点下命令按纽就会在第1第2个文本里分别显示要找程序文件的 PID 与句柄号
现在只得到了第1个文本中的内容
我做的有错误的代码如下:
‘建立的模块中
ublic Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParamAs As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Const TH32CS_SNAPPROCESS = &H2&
Public Type PROCESSENTRY32
dwSize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
On Error Resume Next
Dim pid As Long
Call GetWindowThreadProcessId(hwnd, pid)
‘应该在这里比较才对
EnumWindowsProc = True
End Function

’窗体中
Private Sub Command1_Click()
Dim lSnapShot As Long
Dim lNextProcess As Long
Dim tPE As PROCESSENTRY32
lNextProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
tPE.dwSize = Len(tPE)
If Process32First(lNextProcess, tPE) <> 0 Then
Do
If Left(LCase(tPE.szExeFile), InStr(tPE.szExeFile, ".") + 3) = "junqi.exe" Then
'遍历进程,查找notepad.exe,找到后执行操作.......
qq = tPE.th32ProcessID
Text1.Text = qq
End If
Loop While Process32Next(lNextProcess, tPE) <> 0
End If
CloseHandle lNextProcess
EnumWindows AddressOf EnumWindowsProc, 0&
'这里不知道还该写什么
End Sub

我想建个动态数组储存下枚举到的窗口句柄和PID,再进行比较,可惜也失败了。。。
求VB高手帮忙指点,我的QQ52521977
展开
 我来答
moonxun1002
推荐于2016-10-13
知道答主
回答量:29
采纳率:0%
帮助的人:0
展开全部
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParamAs As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Const TH32CS_SNAPPROCESS = &H2&
Public Type PROCESSENTRY32
dwSize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
On Error Resume Next
Dim pid As Long
Call GetWindowThreadProcessId(hwnd, pid)

EnumWindowsProc = True
End Function

’窗体中
Private Sub Command1_Click()
Dim lSnapShot As Long
Dim lNextProcess As Long
Dim tPE As PROCESSENTRY32
lNextProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
tPE.dwSize = Len(tPE)
If Process32First(lNextProcess, tPE) <> 0 Then
Do
If Left(LCase(tPE.szExeFile), InStr(tPE.szExeFile, ".") + 3) = "junqi.exe" Then

qq = tPE.th32ProcessID
Text1.Text = qq
End If
Loop While Process32Next(lNextProcess, tPE) <> 0
End If
CloseHandle lNextProcess
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式