1、建立一個(gè)標(biāo)準(zhǔn)工程
2、添加一個(gè)標(biāo)準(zhǔn)模塊
3、再工程里添加一個(gè)listbox列表框
4、添加2個(gè)command 1個(gè)timer
將下面代碼復(fù)制到,標(biāo)準(zhǔn)模塊內(nèi)
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
'該函數(shù)是EnumWindows的回調(diào)函數(shù),EnumWindows函數(shù)將遍歷的窗口句柄傳遞到hwnd參數(shù)中 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)) '得到窗口的標(biāo)題 If InStr(astr, " ") > 1 Then If InStr(LCase(astr), LCase("你要查找的標(biāo)題-關(guān)鍵字")) > 0 And InStr(LCase(astr), LCase(" - ")) = 0 Then '將關(guān)鍵字換成你的就行了 Form1.Combo1.AddItem Left(astr, InStr(1, astr, Chr(0)) - 1) & vbCrLf & hwnd End If End If EnumWindowsProc = True End Function
5、將下面代碼復(fù)制到form1模塊內(nèi)(窗體模塊內(nèi))
Option Explicit
Private Sub Command1_Click() Timer1.Enabled = True '啟動(dòng)開查找時(shí)鐘 End Sub
Private Sub command2_click() Timer1.Enabled = False '停止開始查找時(shí)鐘 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、將查找關(guān)鍵字換成你要查找的關(guān)鍵字,運(yùn)行即可,獲得窗體名稱與句柄。
|