如何枚举所有桌面上的程序窗口

sssvip123 2009-10-23 01:36:22
如何枚举所有桌面上的程序窗口(看不见的窗口就不需要了),请给个实例。
...全文
298 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
futurepi 2010-09-01
  • 打赏
  • 举报
回复
学习了
Flyingdragon168 2009-10-23
  • 打赏
  • 举报
回复
API 函数,FindWindow与EnumWindows结合。
贝隆 2009-10-23
  • 打赏
  • 举报
回复
'Example Name:EnumWindows and EnumChildWindows Callbacks  

'------------------------------------------------------------------------------
'
' BAS Moduel Code
'
'------------------------------------------------------------------------------
Option Explicit

Private Const LVIF_INDENT As Long = &H10
Private Const LVIF_TEXT As Long = &H1
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETITEM As Long = (LVM_FIRST + 6)

Private Type LVITEM
mask As Long
iItem As Long
iSubItem As Long
state As Long
stateMask As Long
pszText As String
cchTextMax As Long
iImage As Long
lParam As Long
iIndent As Long
End Type

Public Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long

Public Declare Function EnumChildWindows Lib "user32" _
(ByVal hWndParent As Long, _
ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" _
(ByVal hwnd 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 GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

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

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

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Public Function EnumWindowProc(ByVal hwnd As Long, _
ByVal lParam As Long) As Long

'working vars
Dim nSize As Long
Dim sTitle As String
Dim sClass As String

Dim sIDType As String
Dim itmX As ListItem
Dim nodX As Node

'eliminate windows that are not top-level.
If GetParent(hwnd) = 0& And _
IsWindowVisible(hwnd) Then

'get the window title / class name
sTitle = GetWindowIdentification(hwnd, sIDType, sClass)

'add to the listview
Set itmX = Form1.ListView1.ListItems.Add(Text:=sTitle, Key:=CStr(hwnd) & "h")
itmX.SmallIcon = Form1.ImageList1.ListImages("parent").Key
itmX.SubItems(1) = CStr(hwnd)
itmX.SubItems(2) = sIDType
itmX.SubItems(3) = sClass

End If

'To continue enumeration, return True
'To stop enumeration return False (0).
'When 1 is returned, enumeration continues
'until there are no more windows left.
EnumWindowProc = 1

End Function


Private Function GetWindowIdentification(ByVal hwnd As Long, _
sIDType As String, _
sClass As String) As String

Dim nSize As Long
Dim sTitle As String

'get the size of the string required
'to hold the window title
nSize = GetWindowTextLength(hwnd)

'if the return is 0, there is no title
If nSize > 0 Then

sTitle = Space$(nSize + 1)
Call GetWindowText(hwnd, sTitle, nSize + 1)
sIDType = "title"

sClass = Space$(64)
Call GetClassName(hwnd, sClass, 64)

Else

'no title, so get the class name instead
sTitle = Space$(64)
Call GetClassName(hwnd, sTitle, 64)
sClass = sTitle
sIDType = "class"

End If

GetWindowIdentification = TrimNull(sTitle)

End Function


Public Function EnumChildProc(ByVal hwnd As Long, _
ByVal lParam As Long) As Long

'working vars
Dim sTitle As String
Dim sClass As String
Dim sIDType As String
Dim itmX As ListItem

'get the window title / class name
sTitle = GetWindowIdentification(hwnd, sIDType, sClass)

'add to the listview
Set itmX = Form2.ListView1.ListItems.Add(Text:=sTitle)
itmX.SmallIcon = Form2.ImageList1.ListImages("child").Key
itmX.SubItems(1) = CStr(hwnd)
itmX.SubItems(2) = sIDType
itmX.SubItems(3) = sClass

Listview_IndentItem Form2.ListView1.hwnd, CLng(itmX.Index), 1

EnumChildProc = 1

End Function


Private Function TrimNull(startstr As String) As String

Dim pos As Integer

pos = InStr(startstr, Chr$(0))

If pos Then
TrimNull = Left$(startstr, pos - 1)
Exit Function
End If

'if this far, there was
'no Chr$(0), so return the string
TrimNull = startstr

End Function


Private Sub Listview_IndentItem(hwnd As Long, _
nItem As Long, _
nIndent As Long)

Dim LV As LVITEM

'if nIndent indicates that indentation
'is requested nItem is the item to indent
If nIndent > 0 Then

With LV
.mask = LVIF_INDENT
.iItem = nItem - 1 'have to subtract 1
.iIndent = nIndent
End With

Call SendMessage(hwnd, LVM_SETITEM, 0&, LV)

End If

End Sub
'--end block--'
'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option Explicit

Private Sub Command1_Click()

ListView1.ListItems.Clear
Call EnumWindows(AddressOf EnumWindowProc, &H0)

End Sub


Private Sub Form_Load()

Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2

End Sub


Private Sub ListView1_DblClick()

Dim hwndSelected As Long

hwndSelected = Val(ListView1.SelectedItem.Key)

Load Form2
Call Form2.EnumSelectedWindow(ListView1.SelectedItem.Text, hwndSelected)

End Sub
'--end block--'


Form2 Code

Add the following code to Form2:

--------------------------------------------------------------------------------

Option Explicit

Public Sub EnumSelectedWindow(sItem As String, hwnd As Long)

ListView1.ListItems.Clear
ListView1.ListItems.Add Text:=sItem, SmallIcon:="parent"

Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0)

Me.Show vbModal

End Sub


Private Sub Form_Load()

Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2

End Sub
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chenjl1031 的回复:]
标准模块:

VB codeOptionExplicitPublic DeclareFunction GetDesktopWindow Lib"user32.dll" ()AsLongPublic DeclareFunction EnumChildWindows Lib"user32.dll" (ByVal hWndParentAsLong, ByVal lpEnumFuncAsLong, ByVal lParamAsLong)AsLong'Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As BooleanPrivate DeclareFunction GetWindowText Lib"user32" Alias"GetWindowTextA" (ByVal hwndAsLong, ByVal lpStringAsString, ByVal cchAsLong)AsLongPrivate DeclareFunction GetWindowTextLength Lib"user32" Alias"GetWindowTextLengthA" (ByVal hwndAsLong)AsLongPublicFunction EnumWindowsProc(ByVal hwndAsLong, ByVal lParamAsLong)AsBooleanDim windowCaptionAsString, LengthCaptionAsLongOnErrorResumeNext
LengthCaption= GetWindowTextLength(hwnd)
windowCaption=Space$(LengthCaption)Call GetWindowText(hwnd, windowCaption, LengthCaption+1)
Form1.List1.AddItem Str$(hwnd)&""& windowCaption
EnumWindowsProc=TrueEnd Function


VB code'窗体Form1代码OptionExplicit'窗体上添加一个命令按钮Command1,一个列表框List1PrivateSub Command1_Click()Dim DeskTopHwndAsLong
DeskTopHwnd= GetDesktopWindow
EnumChildWindows DeskTopHwnd, AddressOf EnumWindowsProc, ByVal0&End Sub
[/Quote]
东方之珠 2009-10-23
  • 打赏
  • 举报
回复
标准模块:

Option Explicit

Public Declare Function GetDesktopWindow Lib "user32.dll" () As Long
Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
'Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim windowCaption As String, LengthCaption As Long

On Error Resume Next
LengthCaption = GetWindowTextLength(hwnd)
windowCaption = Space$(LengthCaption)
Call GetWindowText(hwnd, windowCaption, LengthCaption + 1)
Form1.List1.AddItem Str$(hwnd) & " " & windowCaption
EnumWindowsProc = True
End Function




'窗体Form1代码
Option Explicit
'窗体上添加一个命令按钮Command1,一个列表框List1
Private Sub Command1_Click()
Dim DeskTopHwnd As Long
DeskTopHwnd = GetDesktopWindow
EnumChildWindows DeskTopHwnd, AddressOf EnumWindowsProc, ByVal 0&
End Sub
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 supermanking 的回复:]
看看这几个API
EnumWindows
[/Quote]
猩猩到底是不一样。
one banana is lot banana.
luofenghen 2009-10-23
  • 打赏
  • 举报
回复
感觉API就是查找存放桌面图标的文件夹
不知道对不对?
现在还是人类 2009-10-23
  • 打赏
  • 举报
回复
IsWindowVisible
现在还是人类 2009-10-23
  • 打赏
  • 举报
回复
看看这几个API
EnumWindows
现在还是人类 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 patrickkong 的回复:]

C:\Documents and Settings\你的\Desktop
[/Quote]
你太有才了,呵呵
  • 打赏
  • 举报
回复

C:\Documents and Settings\你的\Desktop
sssvip123 2009-10-23
  • 打赏
  • 举报
回复
任何在桌面上看得见的窗口
  • 打赏
  • 举报
回复
程序的定义是什么? txt文件是不?

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧