AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VB编程

获取 标题窗口有空格,类名称是乱码的窗口句柄方法

51自学网 http://www.wanshiok.com

原帖及讨论:http://bbs.bccn.net/thread-123451-1-1.html

感谢 szyicol 的支持
1、建立一个标准工程

2、添加一个标准模块

3、再工程里添加一个listbox列表框

4、添加2个command   1个timer

将下面代码复制到,标准模块内

Option Explicit

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


'该函数是EnumWindows的回调函数,EnumWindows函数将遍历的窗口句柄传递到hwnd参数中
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
  Dim astr As String * 256
  Dim L As Long
  
  L = GetWindowText(hwnd, astr, Len(astr))  '得到窗口的标题
  If InStr(astr, " ") > 1 Then
        If InStr(LCase(astr), LCase("你要查找的标题-关键字")) > 0 And InStr(LCase(astr), LCase(" - ")) = 0 Then   '将关键字换成你的就行了
            
            Form1.Combo1.AddItem Left(astr, InStr(1, astr, Chr(0)) - 1) & vbCrLf & hwnd
        End If
  End If
  EnumWindowsProc = True
End Function


5、将下面代码复制到form1模块内(窗体模块内)

Option Explicit

Private Sub Command1_Click()
    Timer1.Enabled = True  '启动开查找时钟
End Sub

Private Sub command2_click()
    Timer1.Enabled = False   '停止开始查找时钟
End Sub

Private Sub Timer1_Timer()
    Call RefreshList
End Sub
Sub RefreshList()
    On Error Resume Next
    Dim L As Long
    Dim k As Long
    k = Combo1.ListIndex
    Combo1.Clear
    L = EnumWindows(AddressOf EnumWindowsProc, 0)
    If Combo1.ListCount > 0 Then Combo1.ListIndex = k

End Sub

6、将查找关键字换成你要查找的关键字,运行即可,获得窗体名称与句柄。

 

 

 
上一篇:VB编程控制Windows中文输入法  下一篇:关于Visual Basic 9.0的动态标识符