
楼主您好 VB技术部 为您解答在窗体上放置两个按钮 Command1 名字为开始获取 Command2 名字为停止获取 一个list 一个timer然后把timer 的属性 Enabled 设置为False Interval填写为1000 加入以下代码Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) 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 SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SWP_NOMOVE = &H2
Dim s As String
Private Type POINTAPI
X As Long
Y As Long
End Type Private Sub Command1_Click()
List1Clear
Timer1Enabled = True
End SubPrivate Sub Command2_Click()
Timer1Enabled = False
End SubPrivate Sub Timer1_Timer()
Dim lpPoint As POINTAPI
Dim QQhwnd As Long
Dim sBuffer As String
sBuffer = Space(255)
GetCursorPos lpPoint
QQhwnd = WindowFromPoint(lpPointX, lpPointY)
GetWindowText QQhwnd, sBuffer, 255
List1AddItem sBuffer
End Sub 运行后点下按钮 然后鼠标按下你要获取的窗口 1秒自动获得进程名称 效果图
已知进程名称或PID,是无法准确定位窗体句柄的。
这是因为:一旦程序启动后,系统只分配给它一个PID,而一个程序往往有多个窗口。所以,要想从PID反向查找对应的窗口不具有唯一性,并已经过实践证明,往这方面研究只能走入死胡同。
解决此问题办法是:先枚举所有窗口句柄,然后取得对应的进程名称或PID,如果该进程或PID满足你的条件,则可以进行相应处理。
正着做有点难,那么反着做。枚举窗口,获得pid,得到进程名,判断,ok
下面的代码是获得所有窗口名称的,因为一个程序不止有一个顶层窗口,所以你可能还需要根据窗口属性判断以下是不是需要的。对于一些程序不允许打开进程或者获取路径的,就没有办法了,在这里我直接忽略。在我这wps就获取不到路径。另外,GetProcessImageFileName得到的是系统内部的路径,不是像c:\atxt这样的,是\Device\HarddiskVolume1\atxt。不过不影响获得文件名
Option ExplicitPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetProcessImageFileName Lib "psapi" Alias "GetProcessImageFileNameA" (ByVal hProcess As Long, ByVal lpImageFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Sub Form_Load()
Dim hWindow As Long
hWindow = FindWindow(vbNullString, vbNullString)
Dim AllWindowCaption As String
Do Until hWindow = 0
Dim pid As Long
GetWindowThreadProcessId hWindow, pid
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION, 0, pid)
If hProcess Then
Dim FileName As String
FileName = String(255, 0)
FileName = LeftB(FileName, GetProcessImageFileName(hProcess, FileName, 255) + 1)
Dim SA() As String
SA() = Split(FileName, "\")
If UBound(SA()) > -1 Then
If StrComp(SA(UBound(SA())), "工程1exe", vbTextCompare) = 0 Then
Dim WindowCaption As String
WindowCaption = String(255, 0)
WindowCaption = Left(WindowCaption, GetWindowText(hWindow, WindowCaption, 255))
AllWindowCaption = AllWindowCaption & WindowCaption & vbCrLf
End If
End If
End If
CloseHandle hProcess
hWindow = GetWindow(hWindow, GW_HWNDNEXT)
Loop
MsgBox AllWindowCaption
End
End Sub
以记事本为例,系统中有多个记事本在运行。
模块代码:
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPublic Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Const GW_OWNER = 4
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public WndCaption As String
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
Dim a As Long
Dim v As Long
S = String(255, 0)
Call GetWindowText(hwnd, S, 255)
S = Left(S, InStr(S, Chr(0)) - 1)
v = GetWindow(hwnd, GW_OWNER)
a = IsWindowVisible(hwnd)
If Len(S) > 0 And a <> 0 And v = 0 And Right(S, 3) = "记事本" Then
WndCaption = WndCaption & S & vbCrLf
End If
EnumWindowsProc = True
End Function
窗体代码:
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0&
MsgBox WndCaption
End Sub
只能获取最前的那个窗口句柄
版本 2
子程序 进程_名取句柄0, 整数型, , 找不到将返回0
参数 进程名, 文本型
局部变量 sHandle, 整数型
局部变量 Pn, PROCESSENTRY32
局部变量 lpTe, THREADENTRY32
局部变量 Found, 整数型
局部变量 ret, 整数型
局部变量 sThread, 整数型
局部变量 tFound, 整数型
sHandle = CreateToolhelp32Snapshot (15, 0)
Pndwsize = 296
Found = Process32First (sHandle, Pn)
判断循环首 (Found ≠ 0)
如果真 (到小写 (到文本 (PnszExefile)) = 到小写 (进程名))
sThread = CreateToolhelp32Snapshot (4, 0)
lpTedwSize = 28
tFound = Thread32First (sThread, lpTe)
判断循环首 (tFound ≠ 0)
如果真 (lpTeth32OwnerProcessID = Pnth32ProcessID)
EnumThreadWindows (lpTeth32ThreadID, &EnumThreadWndProc, ret)
跳出循环 ()
如果真结束
tFound = Thread32Next (sThread, lpTe)
判断循环尾 ()
CloseHandle (sThread)
跳出循环 ()
如果真结束
Found = Process32Next (sHandle, Pn)
判断循环尾 ()
CloseHandle (sHandle)
返回 (ret)
子程序 EnumThreadWndProc, 整数型
参数 hwnd, 整数型
参数 lParam, 整数型, 参考
局部变量 lHwnd1, 整数型
局部变量 lHwnd2, 整数型
lHwnd1 = GetParent (hwnd)
判断循环首 (lHwnd1 ≠ 0) ' 用取父窗口的方法,循环找到他祖宗 :)
lHwnd2 = lHwnd1
lHwnd1 = GetParent (lHwnd1)
判断循环尾 ()
如果真 (lHwnd2 ≠ 0)
lParam = lHwnd2
返回 (0)
如果真结束
返回 (1)
版本 2
DLL命令 CreateToolhelp32Snapshot, 整数型
参数 dwFlags, 整数型
参数 th32ProcessID, 整数型
DLL命令 Process32First, 整数型
参数 hSnapShot, 整数型
参数 uProcess, PROCESSENTRY32
DLL命令 Thread32First, 整数型, "KERNEL32DLL", "Thread32First"
参数 hSnapshot, 整数型
参数 lpte, THREADENTRY32
DLL命令 EnumThreadWindows, 整数型, "user32", "EnumThreadWindows"
参数 dwThreadId, 整数型
参数 lpfn, 子程序指针
参数 lParam, 整数型, 传址
DLL命令 Thread32Next, 整数型, "KERNEL32DLL", "Thread32Next"
参数 hSnapshot, 整数型
参数 lpte, THREADENTRY32
DLL命令 CloseHandle, 整数型, "kernel32"
参数 hObject, 整数型
DLL命令 Process32Next, 整数型
参数 hSnapShot, 整数型
参数 uProcess, PROCESSENTRY32
DLL命令 GetParent, 整数型, "user32", "GetParent"
参数 hwnd, 整数型
版本 2
数据类型 PROCESSENTRY32
成员 dwsize, 整数型, , , 296
成员 cntusage, 整数型, , , 引用数
成员 th32ProcessID, 整数型, , , 进程标识符
成员 th32defaultheapid, 整数型, , , 默认堆ID
成员 th32moduleid, 整数型, , , 进程模块标识符
成员 cntthreads, 整数型, , , 线程数
成员 th32ParentProcessID, 整数型, , , 父进程标识符
成员 pcpriclassbase, 整数型, , , 线程优先权
成员 dwflags, 整数型
成员 szExefile, 字节型, , "260", 进程名称
数据类型 THREADENTRY32
成员 dwSize, 整数型
成员 cntUsage, 整数型
成员 th32ThreadID, 整数型
成员 th32OwnerProcessID, 整数型
成员 tpBasePri, 整数型
成员 tpDeltaPri, 整数型
成员 dwFlags, 整数型
以上就是关于VB6中如何获得指定进程名称全部的内容,包括:VB6中如何获得指定进程名称、vB 通过进程名称获取窗体句柄、vb 如何根据进程名获取窗体标题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)