枚举所有窗口句柄问题,求教

尊尚科技 2009-03-23 09:22:53
枚举所有窗口句柄问题,求教
悬赏分:50 - 离问题结束还有 14 天 23 小时
我需要实现:枚举所有存在的窗口,发现窗口标题[含有]“123”,就把该窗口的句柄输出到Text1中,但是不知道代码某处出了错误,一直取不出,请高手帮我修改一下 O(∩_∩)O谢谢 小生有礼~ 新手求教,希望能给出修改后的源码 O(∩_∩)O谢谢
Private Sub Command2_Click()

Dim l As Long

l = EnumWindows(AddressOf EnumWindowsProc, 0)

Text1.Text = xx

End Sub


以下代码我存在独立的Module里的:
Module:

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim RetVal As Long
Dim dd As String * 255
Dim a As String

a = "123"

RetVal = GetWindowText(hwnd, dd, 255)
If c = InStr(dd, a) Then
xx = dd
Else
End If
End Function
...全文
374 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
东方之珠 2009-03-24
  • 打赏
  • 举报
回复
Form1窗体模块:

Option Explicit

Private Sub Command2_Click()

Dim l As Boolean

Text1.Text = ""

l = EnumWindows(AddressOf EnumWindowsProc, ByVal 0&)

'Text1.Text = xx

End Sub



标准模块:

Option Explicit

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 Long
Dim RetVal As Long
Dim CaptionLength As Long
Dim WindowCaption As String
Dim a As String

a = "123"

CaptionLength = GetWindowTextLength(hwnd) '取得顶级窗口标题长度
WindowCaption = Space(CaptionLength) '设置窗口标题长度

RetVal = GetWindowText(hwnd, WindowCaption, CaptionLength + 1)

'WindowCaption = Left(WindowCaption, InStr(WindowCaption, Chr(0)) - 1) '去掉尾部空格

Debug.Print WindowCaption

If InStr(WindowCaption, a) > 0 Then
Form1.Text1.Text = Form1.Text1.Text & WindowCaption & vbCrLf
Else
'''''''''''''''''
End If
EnumWindowsProc = 1 'True
End Function
fjwyshan 2009-03-24
  • 打赏
  • 举报
回复
友情up
lili830209 2009-03-24
  • 打赏
  • 举报
回复
友情up
zzyong00 2009-03-23
  • 打赏
  • 举报
回复
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long 
Dim RetVal As Long
Dim dd As String * 255
Dim a As String

a = "123"

RetVal = GetWindowText(hwnd, dd, 255)
If 0<> InStr(dd, a) Then
form1.text1.text = form1.text1.text & vbcrlf & dd
Else
End If
EnumWindowsProc=1
End Function
尊尚科技 2009-03-23
  • 打赏
  • 举报
回复
再次郁闷 按照chenhui530 大哥的写发 无论我把a="123"改成a="321"还是其他 点了Command2后 form1.text1.text一直是显示“Default IME“ 而在我的想法中 我需要form1.text1.text = dd 显示的是窗口标题[含有]“123”的完整窗口标题 form1.text1.text = hwnd 显示的是窗口标题[含有]“123”的窗口句柄 我是超级新手,请大哥们包含,再给我修改一次代码 O(∩_∩)O谢谢~~~~~
chenhui530 2009-03-23
  • 打赏
  • 举报
回复
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim RetVal As Long
Dim dd As String * 255
Dim a As String

a = "123"

RetVal = GetWindowText(hwnd, dd, 255)
If c = InStr(dd, a) Then
form1.text1.text = dd
Else
End If
EnumWindowsProc=1
End Function
尊尚科技 2009-03-23
  • 打赏
  • 举报
回复
chenhui530 的意思我明白了 谢谢 chenhui530 大哥 但是代码还是有问题 我点了
Command2之后 还是没办法把xx输出到TEXT1 再次求教
chenhui530 2009-03-23
  • 打赏
  • 举报
回复
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim RetVal As Long
Dim dd As String * 255
Dim a As String

a = "123"

RetVal = GetWindowText(hwnd, dd, 255)
If c = InStr(dd, a) Then
xx = dd
Else
End If
EnumWindowsProc=1
End Function
僵哥 2009-03-23
  • 打赏
  • 举报
回复
EnumWindows只能列举顶级窗体,对于非顶级窗体,需要继续使用EnumChildWindows

1,488

社区成员

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

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