office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VB或VBA穫取無標題窗口的句柄

2017-09-20 08:10:00
zstmtony
原創
7151
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用鼠標選 
或用樓上説的枚舉
分享