VB根据进程名获取对应窗体标题

2024-12-31 02:25:38
推荐回答(3个)
回答1:

Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
首先枚举所有的窗口。
然后
Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
判断窗口的所属进程。
建立一个集合。
最后通过索引显示就OK了。

回答2:

窗体代码如下 在TEXT输入进程PID
Private Sub Command1_Click()
List1.Clear
Find_Window Val(Text1.Text)
End Sub
Private Sub Form_Load()
Text1.Text = ""
Command1.Caption = "枚举窗口"
End Sub
模块代码如下
Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId 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
Dim IfPid As Long
Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Pid1 As Long
Dim wText As String * 255
GetWindowThreadProcessId hwnd, Pid1
If IfPid = Pid1 Then
GetWindowText hwnd, wText, 100
Form1.List1.AddItem "句柄:" & hwnd & " 标题:" & wText
End If
EnumWindowsProc = True
End Function
Public Sub Find_Window(ByVal Pid As Long)
IfPid = Pid
EnumWindows AddressOf EnumWindowsProc, 0
End Sub

回答3:

直接用FindWindow(0, "窗口标题")就可以获取窗口句柄,不用通过winmgmts,那个东西不好用,服务一停就没法工作了,FindWindow是API,任何情况都能使用