手帐简单小素材图片:VB 只知道进程ID,如何获得窗口句柄,来看看这个程序阿555555

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/05 17:38:45
我用Pid=shell(*.exe,*)后,得到进程Id:pid,(由于这个文件*.exe是变化的,所以我并不知道它的窗口类也不知道窗口标题),怎样才能获得它的窗口的句柄呢?下面的这个程序(是个函数)找不到符合的窗口句柄,为什么啊?高手帮帮忙看看...
'此函数得到指定进程的窗口句柄:
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'找到第一个窗口:
test_hwnd = FindWindow( 0&, 0&)
Do While test_hwnd <> 0
' 检查窗口是否不是一个子窗口
If GetParent(test_hwnd) = 0 Then '如果是父窗口(窗口没有父)
'得到窗口的线程(test_thread_id得到的是线程,不是进程,test_pid才得到进程)
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) 'test_pid是个变量,得到窗口进程
If test_pid = target_pid Then 'target_pid是函数的参数,进程标识符
InstanceToWnd = test_hwnd '函数返回窗口句柄
Exit Do
End If
End If
'retrieve the next window 继续查找下一个窗口
test_hwnd = GetWindow(test_hwnd, 2)
Loop
End Function
InstanceToWnd(pid)返回0,为什么啊?
我将test_hwnd = FindWindow( 0&, 0&)里的0&改成
vbnullstring,也不对,改成vbNull 也不对。晕死我了,救救我啊。。。。。。。。。
梦归雾散,你的XXXX是什么东西啊?
我猜,会不会是因为If GetParent(test_hwnd) = 0 Then 这一句导致失败的?
,因为所有的都有个父,就是桌面窗口。
leo239,都说我不可能知道窗体标题,你还要我自己填。。。我要找的是所有可能符合的任何窗体。所以用(0&,0&)
这个程序,我改用了EnumWindows (枚举窗口列表中的所有父窗口)这个函数来实现,成功了。
但是对于《跑跑卡丁车》的启动程序还是找不到它的窗口句柄!会不会它根本没有父窗口阿?

晕哦,不可能的.
你把你的代码发过来,软件的具体说明说一下.
你使用SPY工具可以查看到当前已经打开的每个窗口的CLASS.
还不清楚,QQ452362812

XXXX是窗口的标题,也就是form.caption 属性.
使用此函数将返回指定窗口的句柄.

VB声明
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
说明
寻找窗口列表中第一个符合指定条件的顶级窗口(在vb里使用:FindWindow最常见的一个用途是获得ThunderRTMain类的隐藏窗口的句柄;该类是所有运行中vb执行程序的一部分。获得句柄后,可用api函数GetWindowText取得这个窗口的名称;该名也是应用程序的标题)
返回值
Long,找到窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError
参数表
参数 类型及说明
lpClassName String,指向包含了窗口类名的空中止(C语言)字串的指针;或设为零,表示接收任何类
lpWindowName String,指向包含了窗口文本(或标签)的空中止(C语言)字串的指针;或设为零,表示接收任何窗口标题
注解
很少要求同时按类与窗口名搜索。为向自己不准备参数传递一个零,最简便的办法是传递vbNullString常数

示例
Dim hw&, cnt&
Dim rttitle As String * 256
hw& = FindWindow("ThunderRT6FormDC", "窗口名称自己填") ' ThunderRT6FormDC under VB6
cnt = GetWindowText(hw&, rttitle, 255)
MsgBox Left$(rttitle, cnt), 0, "RTMain title"

下面的代码是关于窗口的API
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(WinWnd, lpClassName, 256)
'Show the classname
MsgBox "Classname: " + Left$(lpClassName, RetVal)
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

FindWindow(0&, 0&)好像不行,FindWindow(VBNullString, XXXX)倒是可以