vb.net获取当前运行的所有窗口标题
时间: 2024-03-02 13:50:03 浏览: 172
你可以使用 Windows API 中的 EnumWindows 函数和 GetWindowText 函数来获取当前运行的所有窗口标题。
以下是一个示例代码,可以获取当前运行的所有窗口标题:
```
Imports System.Runtime.InteropServices
Public Class Form1
' Windows API
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function EnumWindows(ByVal lpEnumFunc As EnumWindowsCallback, ByVal lParam As IntPtr) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpWindowText As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
Private Delegate Function EnumWindowsCallback(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' 枚举所有窗口
EnumWindows(AddressOf EnumWindowsCallbackFunction, IntPtr.Zero)
End Sub
Private Function EnumWindowsCallbackFunction(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
' 获取窗口标题
Dim title As New StringBuilder(256)
GetWindowText(hWnd, title, title.Capacity)
' 输出标题
Debug.WriteLine(title.ToString())
Return True
End Function
End Class
```
在代码中,点击按钮后,使用 EnumWindows 函数枚举所有窗口,并使用 GetWindowText 函数获取每个窗口的标题。然后输出标题,你可以根据需要修改输出方式。
阅读全文