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

VB获取所有运行程序的句柄,第1张

'-----------Begin of Module1bas-----------------------------

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 Form1List1AddItem t

EnumWindowsProc = True

End Function

'-----------End of Module1bas-----------------------------

'-----------Begin of form1frm-----------------------------

Private Sub Command1_Click()

List1Clear

Call EnumWindows(AddressOf EnumWindowsProc, 0)

Call EnumWindowsProc(GetForegroundWindow, 0)

End Sub

Private Sub Form_Load()

Command1Caption = "开始"

End Sub

'-----------End of form1frm-----------------------------

测试过的,没问题,要源码请发邮件到284304241@qqcom索要,或者到>

根据补充信息,修改如下:

窗体上加入控件command1,然后复制下面代码,运行,启动画图程序,单击command1,即可找到你要的画纸的句柄

Option Explicit

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

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Sub Command1_Click()

Dim huatu, huazi

Dim i As Integer

huatu = FindWindowEx(0, 0, vbNullString, "未命名 - 画图")

Do Until huatu = 0

huazi = FindWindowEx(huatu, 0, "AfxFrameOrView42u", vbNullString)

huazi = FindWindowEx(huazi, 0, "Afx:1000000:8", vbNullString)

i = i + 1

MsgBox "找到第" & i & "个画纸,它的句柄是:" & huazi

huatu = FindWindowEx(0, huatu, vbNullString, "未命名 - 画图")

Loop

End Sub

OptionExplicitPrivateDeclareFunctionFindWindowLibuser32AliasFindWindowA(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsLongPrivateDeclareFunctionGetWindowTextLibuser32AliasGetWindowTextA(ByValhwndAsLong,ByVallpStringAsString,ByValcchAsLong)AsLongPrivateDeclareFunctionGetWindowLibuser32(ByValhwndAsLong,ByValwCmdAsLong)AsLongPrivateDeclareFunctionGetDesktopWindowLibuser32()AsLongPrivateDeclareFunctionGetClassNameLibuser32AliasGetClassNameA(ByValhwndAsLong,ByVallpClassNameAsString,ByValnMaxCountAsLong)AsLongPrivateConstGW_HWNDFIRST=0PrivateConstGW_HWNDNEXT=2PrivateConstGW_CHILD=5PrivateSubCommand1_Click()CallList1_ClickEndSubPrivateSubCommand2_Click()DimhwndAsLongDimsAsString,tAsStringList1Clearhwnd=GetDesktopWindow()s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem桌面:&hwnd&类名:&s&标题:&t&vbCrLfhwnd=GetWindow(hwnd,GW_CHILDOrGW_HWNDFIRST)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWhilehwnd<0hwnd=GetWindow(hwnd,GW_HWNDNEXT)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWendEndSubPrivateSubForm_Load()Command1Caption=获取所有控件Command2Caption=遍历所有窗体EndSubPrivateSubEnumAllHandles(ByValhwndAsLong)DimhnAsLongDimfirsthdAsLongDimsAsString,tAsStringfirsthd=GetWindow(hwnd,GW_CHILD)firsthd=GetWindow(firsthd,GW_HWNDFIRST)hn=firsthdDoWhilehn<0s=String(256,Chr(0))GetClassNamehn,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthn,t,255t=Replace(t,Chr(0),)Text1Text=Text1Text&句柄:&hn&父句柄:&hwnd&类名:&s&标题:&t&vbCrLfTreeView1NodesAddk&hwnd,tvwChild,k&hn,句柄:&hn&类名:&s&标题:&tEnumAllHandleshnhn=GetWindow(hn,GW_HWNDNEXT)Ifhn=firsthdThenExitDoLoopEndSubPrivateSubList1_Click()IfList1ListIndex=-1ThenExitSubTreeView1NodesClearTreeView1NodesAdd,,k&Trim(Str(Val(Mid(List1Text,4)))),List1TextText1Text=EnumAllHandlesVal(Mid(List1Text,4))TreeView1Nodes(k&Trim(Str(Val(Mid(List1Text,4)))))Expanded=TrueEndSub'添加两个按钮一个文本框一个列表框和一个树形图

用delphi实现:

窗体句柄:FindWindow(nil,'窗体名称');

控件句柄:

uses ActiveX;

ActiveXOleInitialize(nil);

GetInnerClassHandle(窗体句柄,'控件类名控件名');

方案一

开始——控制面板——计划任务——新建任务——每一分钟——TASKKILL /F /IM QQexe /T

方案二

使用编程软件没分钟检测QQexe的PID,如果不为0,则说明正在运行,则运行TASKKILL /F /IM cmdexe /T结束QQ进程

'创建一个列表框和一个按钮

'API函数定义

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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Const GW_HWNDFIRST = 0

Const GW_HWNDNEXT = 2

Private Sub Command1_Click()

Dim s As Long

Dim a As String

a = Space(255)

s = GetWindow(Mehwnd, GW_HWNDFIRST) '获得句柄

GetWindowText s, a, 255 '获得标题

Dim ab As String 255

GetClassName s, ab, 255 '获得类名

Dim ClassName As String

ClassName = StripTerFlag(ab) 'StripTerFlag是一个自定义函数,后面有代码,用于去处结束符

If UCase(ClassName) = "NOTEPAD" Then

List1AddItem a

List1ItemData(List1ListCount - 1) = s

End If

Do While s

DoEvents

s = GetWindow(s, GW_HWNDNEXT) '获得句柄

GetWindowText s, a, 255 '获得标题

GetClassName s, ab, 255 '获得类名

ClassName = StripTerFlag(ab)

If UCase(ClassName) = "NOTEPAD" Then

List1AddItem a

List1ItemData(List1ListCount - 1) = s

End If

Loop

End Sub

Private Sub List1_Click()

MsgBox List1ItemData(List1ListIndex) '单击列表框获得句柄

End Sub

以上就是关于VB获取所有运行程序的句柄全部的内容,包括:VB获取所有运行程序的句柄、VB怎么获取其它窗体句柄、VB怎么寻找子窗口等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10164125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存