已知进程名称或PID,是无法准确定位窗体句柄的。
这是因为:一旦程序启动后,系统只分配给它一个PID,而一个程序往往有多个窗口。所以,要想从PID反向查找对应的窗口不具有唯一性,并已经过实践证明,往这方面研究只能走入死胡同。
解决此问题办法是:先枚举所有窗口句柄,然后取得对应的进程名称或PID,如果该进程或PID满足你的条件,则可以进行相应处理。
你很厉害啊~今天才学VB就想玩API了
窗体代码如下 在TEXT输入进程PID
Private Sub Command1_Click()
List1.Clear
Find_Window Val(Text1.Text)
End Sub
Private Sub Form_Load()
Text1.Text = ""
Command1.Caption = "枚举窗口"
End Sub
模块代码如下
Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Dim IfPid As Long
Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Pid1 As Long
Dim wText As String * 255
GetWindowThreadProcessId hwnd, Pid1
If IfPid = Pid1 Then
GetWindowText hwnd, wText, 100
Form1.List1.AddItem "句柄:" & hwnd & " 标题:" & wText
End If
EnumWindowsProc = True
End Function
Public Sub Find_Window(ByVal Pid As Long)
IfPid = Pid
EnumWindows AddressOf EnumWindowsProc, 0
End Sub
问题写得模模糊糊..API,获得指定名称的窗口的句柄:Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long**********************************************************API,使指定句柄的窗口得到焦点:Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long**********************************************************具体用法可以查阅MSDN,不知道我理解对没有
这部分代码只能放在 模块里用。
不然系统不能取得 正确的 地址。
人才啊!我学俩月才敢试着接触动 应用程序接口函数~