VB 如何得到当前任务栏在运行的所有窗体的标题
如题目。我想获得所有窗口的标题。然后分析哪个窗口是我需要的。最后就是控制该窗口,例如在QQ聊天进行输入文字、发送等操作。假如可以,当然所有都回答!假如不可以的话,就先回答...
如题目。我想获得所有窗口的标题。然后分析哪个窗口是我需要的。最后就是控制该窗口,例如在QQ聊天进行输入文字、发送等操作。假如可以,当然所有都回答!假如不可以的话,就先回答第一个啦。感谢
展开
展开全部
需要api的支持,有人已经写出来了:
'获取任务栏窗体名称
Option Explicit
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Const GW_OWNER = 4
Function lpfunc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim pstr As String, Ret As Long
If IsWindowVisible(hwnd) = False Then GoTo continu
If GetWindow(hwnd, GW_OWNER) <> 0 Then GoTo continu
Ret = GetWindowTextLength(hwnd)
pstr = Space(Ret)
If GetWindowText(hwnd, pstr, Ret + 1) = 0 Then GoTo continu
If pstr <> "" And pstr <> "Program Manager" Then Form1.List1.AddItem pstr
continu:
lpfunc = True
End Function
Private Sub Command1_Click()
List1.Clear
EnumWindows AddressOf lpfunc, ByVal 0&
End Sub
'根据窗体标题获取窗体句柄
Private Sub Command2_Click()
Dim i As Integer
Dim hwndNext As Long
Dim hwnd() As Long
i = 0
hwndNext = FindWindowEx(0, 0, vbNullString, "窗体标题")
Do While hwndNext <> 0
ReDim Preserve hwnd(i) As Long
hwnd(i) = hwndNext
i = i + 1
hwndNext = FindWindowEx(0, hwnd(i - 1), vbNullString, "窗体标题")
Loop
If i <> 0 Then
For i = 0 To UBound(hwnd)
List1.AddItem hwnd(i)
Next
Else
MsgBox "未找到相应标题名的窗口"
End If
End Sub
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long
Dim HwndVal&, ChildHwnd&
Private Sub Command1_Click()
HwndVal = FindWindow(vbNullString, "窗体标题")
'Print "窗口句柄是:" & CStr(HwndVal) & Space(3) & "进程号是:" & CStr(ProcIDFromWnd(HwndVal))
ChildHwnd = FindWindowEx(HwndVal, 0, "Static", vbNullString)
'Text1 = "子句柄是:" & ChildHwnd
List1.AddItem CStr(HwndVal) & vbTab & CStr(ProcIDFromWnd(HwndVal)) & vbTab & ChildHwnd
End Sub
'获取任务栏窗体名称
Option Explicit
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Const GW_OWNER = 4
Function lpfunc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim pstr As String, Ret As Long
If IsWindowVisible(hwnd) = False Then GoTo continu
If GetWindow(hwnd, GW_OWNER) <> 0 Then GoTo continu
Ret = GetWindowTextLength(hwnd)
pstr = Space(Ret)
If GetWindowText(hwnd, pstr, Ret + 1) = 0 Then GoTo continu
If pstr <> "" And pstr <> "Program Manager" Then Form1.List1.AddItem pstr
continu:
lpfunc = True
End Function
Private Sub Command1_Click()
List1.Clear
EnumWindows AddressOf lpfunc, ByVal 0&
End Sub
'根据窗体标题获取窗体句柄
Private Sub Command2_Click()
Dim i As Integer
Dim hwndNext As Long
Dim hwnd() As Long
i = 0
hwndNext = FindWindowEx(0, 0, vbNullString, "窗体标题")
Do While hwndNext <> 0
ReDim Preserve hwnd(i) As Long
hwnd(i) = hwndNext
i = i + 1
hwndNext = FindWindowEx(0, hwnd(i - 1), vbNullString, "窗体标题")
Loop
If i <> 0 Then
For i = 0 To UBound(hwnd)
List1.AddItem hwnd(i)
Next
Else
MsgBox "未找到相应标题名的窗口"
End If
End Sub
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long
Dim HwndVal&, ChildHwnd&
Private Sub Command1_Click()
HwndVal = FindWindow(vbNullString, "窗体标题")
'Print "窗口句柄是:" & CStr(HwndVal) & Space(3) & "进程号是:" & CStr(ProcIDFromWnd(HwndVal))
ChildHwnd = FindWindowEx(HwndVal, 0, "Static", vbNullString)
'Text1 = "子句柄是:" & ChildHwnd
List1.AddItem CStr(HwndVal) & vbTab & CStr(ProcIDFromWnd(HwndVal)) & vbTab & ChildHwnd
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询