如何取得无标题的窗口句柄

2025-05-21 22:18:49
推荐回答(1个)
回答(1):

枚举所有进程, 然后找到你的进程的ID
然后用这几个API函数
openprocess
terminateprocess
closehandle

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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const WM_CLOSE = &H10
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDFIRST = 0

Public Sub findrunningprogram()
Dim RenHWND
Dim HD
RenHWND = GetWindow(Form1.hwnd, GW_HWNDFIRST)
Do
RetValue = GetWindowText(RenHWND, WinText, 256)
If RetValue <> 0 Then
WinTextLength = GetWindowTextLength(RenHWND)
If Left$(WinText, WinTextLength) <> Form1.Caption And Left$(WinText, WinTextLength) <> App.Title Then
Text = WinText
If Text = "" Then
HD = RenHWND
End If
End If
End If
RenHWND = GetWindow(RenHWND, GW_HWNDNEXT)
Loop Until Text = ""
findrunningprogram = HD
End Sub
HD的返回值就是Handle了!