VB获取所有运行程序的句柄

我想用VB编一个程序,要获得所有正在运行的窗口句柄,我再用GetWindowText函数得到它的标题,可我不知道怎么得到所有窗口的句柄,我想做个程序就是让用户可以选择指定... 我想用VB编一个程序,要获得所有正在运行的窗口句柄,我再用GetWindowText函数得到它的标题,可我不知道怎么得到所有窗口的句柄,
我想做个程序就是让用户可以选择指定的窗口,整天在线,希望VB高手来帮帮忙!!!多谢了
答的好我再加分,我分多的是。
你这是不是把控件名称都获取了啊?我是要运行的程序标题
展开
 我来答
Eli2021
推荐于2016-07-16 · TA获得超过664个赞
知道小有建树答主
回答量:1266
采纳率:72%
帮助的人:342万
展开全部
'-----------Begin of Module1.bas-----------------------------
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 GetForegroundWindow Lib "user32" () As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean

Dim s As String
Dim t As String
s = String(1024, Chr(0))
GetWindowText hwnd, s, 1024
t = Trim(Left(s, InStr(1, s, Chr(0)) - 1))
If t <> "" Then Form1.List1.AddItem t
EnumWindowsProc = True

End Function
'-----------End of Module1.bas-----------------------------

'-----------Begin of form1.frm-----------------------------
Private Sub Command1_Click()

List1.Clear
Call EnumWindows(AddressOf EnumWindowsProc, 0)
Call EnumWindowsProc(GetForegroundWindow, 0)
End Sub

Private Sub Form_Load()
Command1.Caption = "开始"
End Sub

'-----------End of form1.frm-----------------------------

测试过的,没问题,要源码请发邮件到284304241@qq.com索要,或者到http://hi.baidu.com/eli261留言!

Good Luck!
百度网友0b2cad097
2007-08-03 · 超过22用户采纳过TA的回答
知道答主
回答量:99
采纳率:0%
帮助的人:0
展开全部
Option Explicit

Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

'Constants that are used by the API
Const WM_CLOSE = &H10
Const INFINITE = &HFFFFFFFF
Const SYNCHRONIZE = &H100000

Private Sub Command_Close_Click()
'Closes Windows Calculator
Dim hWindow As Long
Dim hThread As Long
Dim hProcess As Long
Dim lProcessId As Long
Dim lngResult As Long
Dim lngReturnValue As Long

hWindow = FindWindow(vbNullString, "计算器")
hThread = GetWindowThreadProcessId(hWindow, lProcessId)
hProcess = OpenProcess(SYNCHRONIZE, 0&, lProcessId)
lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
lngResult = WaitForSingleObject(hProcess, INFINITE)

'Does the handle still exist?
DoEvents
hWindow = FindWindow(vbNullString, "计算器")
If IsWindow(hWindow) = 1 Then
'The handle still exists. Use the TerminateProcess function
'to close all related processes to this handle.
MsgBox "Handle still exists."
Else
'Handle does not exist.
MsgBox "All Program Instances Closed."
End If
End Sub

Private Sub Command_Start_Click()
'Starts Windows Calculator
Shell "calc.exe", vbNormalNoFocus
End Sub

这个也许用得上
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式