您的位置:首页 > 编程语言 > VB

vb利用FindWindow,GetWindowText,SendMessage查找程序并发送最小化消息

2009-06-26 14:05 411 查看
' 查找运行的窗体
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

' 发送系统消息
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060&
Private Const SC_MINIMIZE = &HF020&
Private Const cch = 255

'
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long '
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Const MAX_PATH As Integer = 260

Private Const PROCESS_ALL_ACCESS = &H100000 + &HF0000 + &HFFF

Private Type PROCESSENTRY32
dwSize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type

Private Sub Form_Load()

Dim hwnd As Long
Dim lpString As String * 256

hwnd = FindWindow(vbNullString, "Macromedia Flash Player 8")
Label1.Caption = hwnd
Label2.Caption = Left(lpString, GetWindowText(hwnd, lpString, cch))

'MsgBox SendMessage(hwnd, WM_CLOSE, 0, 0)   '关闭窗体命令
'MsgBox SendMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0)
'MsgBox SendMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0)

Dim MySnapHandle As Long
Dim hProcess As Long
Dim ProcessInfo As PROCESSENTRY32
MySnapHandle = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
ProcessInfo.dwSize = Len(ProcessInfo)
If ProcessFirst(MySnapHandle, ProcessInfo) <> 0 Then
Do
If Left(LCase(ProcessInfo.szExeFile), InStr(ProcessInfo.szExeFile, ".") + 3) = "flash.exe" Then
'遍历进程,查找unmsg.exe,找到后执行操作.......,进程名全小写
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessInfo.th32ProcessID)
TerminateProcess hProcess, 1
CloseHandle hProcess
End If
Loop While ProcessNext(MySnapHandle, ProcessInfo) <> 0
End If
CloseHandle MySnapHandle

End Sub


以上代码实现最小化、关闭指定的程序,然后结束进程

vb6 + xp sp3 测试通过

更简单的方法 ' 发送最小化命令,Alt+空格,然后按N键 SendKeys "%{ }+n", True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: