VB或VBA获取无标题窗口的句柄

2017-09-20 08:10:00
zstmtony
原创
792
VB 请问如何获取一个无标题的窗口句柄?
请问:如何获取一个无标题的窗口句柄?知道他的应用程序名称。 
谢谢指点

------解决方案--------------------
方法一:调用EnumWindows,枚举所有窗口句柄,然后在枚举过程中,调用GetModuleFileName,获取该窗口句柄的进程名,如果等于你已经知道的进程名字,那这个窗口就是你想要找的窗口了。 
方法二:用工具找到这个程序的类名(一般程序类名都是不一样的),然后用FindWindow就可以找到这个窗口的句柄。 
如果不明白,请继续提问。 
------解决方案--------------------
'关闭进程 
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32 " (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long 
Private Declare Function Process32First Lib "kernel32 " (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long 
Private Declare Function Process32Next Lib "kernel32 " (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long 
Private Declare Function OpenProcess Lib "kernel32 " (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long 
Private Declare Function TerminateProcess Lib "kernel32 " (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long 
Private Declare Sub CloseHandle Lib "kernel32 " (ByVal hPass As Long) 
Private Const TH32CS_SNAPPROCESS = &H2& 

Private Type PROCESSENTRY32 
dwSize As Long 
cntUsage As Long 
th32ProcessID As Long 
th32DefaultHeapID As Long 
th32ModuleID As Long 
cntThreads As Long 
th32ParentProcessID As Long 
pcPriClassBase As Long 
dwFlags As Long 
szExeFile As String * 260 
End Type 
'关闭进程xxx.exe[里面根据文件名判断.也列出了所有进程] 
Public Sub Sys_KillProcess(Ret As String) 
Dim lSnapShot As Long 
Dim lNextProcess As Long 
Dim tPE As PROCESSENTRY32 
On Error Resume Next 
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) 
If lSnapShot <> -1 Then 
tPE.dwSize = Len(tPE) 
lNextProcess = Process32First(lSnapShot, tPE) 
Do While lNextProcess 
If InStr(1, UCase(Ret), UCase(Left(tPE.szExeFile, InStr(1, tPE.szExeFile, Chr(0)) - 1))) <> 0 Then 
Dim lProcess As Long 
Dim lExitCode As Long 
lProcess = OpenProcess(1, False, tPE.th32ProcessID) 
TerminateProcess lProcess, lExitCode 
CloseHandle lProcess 
End If 
lNextProcess = Process32Next(lSnapShot, tPE) 
Loop 
CloseHandle (lSnapShot) 
End If 
DoEvents 
End Sub 

------解决方案--------------------
如果它是前台窗口可用GetForegroundWindow或GetFocus 
或用GetCursorPos加WindowFromPoint用鼠标选 
或用楼上说的枚举
分享