|
- Option Explicit
- Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare Function GetDesktopWindow Lib "user32" () As Long
- Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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
- Const GW_CHILD = 5
- Const GW_HWNDNEXT = &H2
- Dim HwndVal&
- Private Sub Command1_Click()
- Call GetHandle("计算器")
- End Sub
- Public Function GetHandle(Title As String) As Long
- Dim Tmp$, hwnd&, lngProcID&
- Dim StrTitle As String * 255 '用来存储窗口的标题
- HwndVal = FindWindow(vbNullString, Title) 'GetDesktopWindow() '取得桌面窗口
- HwndVal = GetWindow(HwndVal, GW_CHILD) '取得桌面窗口的第一个子窗口
- List1.Clear
- Do While HwndVal <> 0 '通过循环枚举所有的窗口
- GetWindowText HwndVal, StrTitle, Len(StrTitle) '取得下一个窗口的标题,并写入到列表框中
- If Left(StrTitle, 1) <> vbNullChar Then
- Tmp = Left(StrTitle, InStr(StrTitle, vbNullChar))
- List1.AddItem CStr(HwndVal) & "---" & Tmp
- If Left(Tmp, Len(Title)) = Title Then GetHandle = HwndVal
- End If
- HwndVal = GetWindow(HwndVal, GW_HWNDNEXT) '调用GetWindow函数,来取得下一个窗口
- Loop
- End Function
复制代码 |
|