2个回答
展开全部
下面的代码以记事本为例,你修改一下进程名即可:
Option Explicit
Private Declare Function ShowWindow Lib "user32" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2
' 通过进程ID获得该进程的窗口句柄
Public Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long
InstanceToWnd = 0
'On Error Resume Next
' 获得首个handle.
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
' 循环查找直到找到为给定进程ID的窗口句柄
Do While test_hwnd <> 0
'检查窗口句柄是否为顶级窗口
If GetParent(test_hwnd) = 0 Then
' 是顶级窗口
' 取该窗口所属的进程ID
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
' 是我们指定进程的窗口,则将该窗口的句柄返回到函数名,并退出
InstanceToWnd = test_hwnd
Exit Do
End If
End If
' 取下一个窗口的句柄
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Command1_Click()
Dim WinWnd As Long, PID As Long, pro, obj
Set pro = GetObject("winmgmts:\\.\root\cimv2").execquery("select * from win32_process")
For Each obj In pro
Debug.Print obj.Name
If obj.Name = "NOTEPAD.EXE" Then
PID = obj.processid
Exit For
End If
Next
WinWnd = InstanceToWnd(PID)
' WinWnd = FindWindow(vbNullString, "无标题 - 记事本")
If WinWnd <> 0 Then
ShowWindow WinWnd, SW_SHOWMAXIMIZED
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询