Option ExplicitPublic Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPublic Function TitleToWnd(ByVal strTitle As String, Optional strClassName As String = vbNullString) As Long TitleToWnd = FindWindow(strClassName, strTitle) End FunctionPublic Function WndToProcId(ByVal hwnd As Long) As Long GetWindowThreadProcessId hwnd, WndToProcId End FunctionPublic Function WndToTitle(ByVal hwnd As Long) As String Dim WinText As String Dim TextSize As Long WinText = String(255, Chr(0)) TextSize = Len(WinText) GetWindowText hwnd, WinText, TextSize WinText = Left(WinText, InStr(1, WinText, Chr(0)) - 1) WndToTitle = WinText End Function
谢谢! 不过我是菜鸟,这段代码那个值是我要的标题? 比如我要显示出来: .....上面的代码.....Private Sub Form_Load() Text1.Text = ?? '这里应该写什么? End sub
用VB怎样运行EXE文件,代码怎么写,需要引用什么
谢谢! 不过我是菜鸟,这段代码那个值是我要的标题? 比如我要显示出来: .....上面的代码.....Private Sub Form_Load() Text1.Text = ?? '这里应该写什么? End sub关于 用VB怎样运行EXE文件,代码怎么写,需要引用什么 论坛里面有例程搜一下 “调用外部“
shell "路径+程序名", vbNormalFocus
Private Sub Form_Load() Text1.Text = TitleToWnd(Me.Caption) Text2.Text = me.hwnd End sub -------------- 得到的一样。
先用shell调用外部的exe文件获得pid,然后由pid获得hwnd,最后用GetWindowText获得窗口标题给你一段代码,由pid获得hwnd的(声明你自己添加吧),其它的你应该没有什么问题吧: Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long 'Find the first window test_hwnd = FindWindow(ByVal 0&, ByVal 0&) Do While test_hwnd <> 0 'Check if the window isn't a child If GetParent(test_hwnd) = 0 Then 'Get the window's thread test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then InstanceToWnd = test_hwnd Exit Do End If End If 'retrieve the next window test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End Function
转一个贴: 先关子窗体后关主窗体!!! Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long Const GW_HWNDNEXT = 2 Dim mWnd As Long Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long 'Find the first window test_hwnd = FindWindow(ByVal 0&, ByVal 0&) Do While test_hwnd <> 0 'Check if the window isn't a child If GetParent(test_hwnd) = 0 Then 'Get the window's thread test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then InstanceToWnd = test_hwnd Exit Do End If End If 'retrieve the next window test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End Function Private Sub Form_Load() Dim Pid As Long 'Lock the window update LockWindowUpdate GetDesktopWindow 'Execute notepad.Exe Pid = Shell("c:\windows\notepad.exe", vbNormalFocus) If Pid = 0 Then MsgBox "Error starting the app" 'retrieve the handle of the window mWnd = InstanceToWnd(Pid) 'Set the notepad's parent SetParent mWnd, Me.hwnd 'Put the focus on notepad Putfocus mWnd 'Unlock windowupdate LockWindowUpdate False End Sub Private Sub Form_Unload(Cancel As Integer) 'Unload notepad DestroyWindow mWnd 'End this program TerminateProcess GetCurrentProcess, 0 End Sub
//我的问题是用 // Shell "name.exe", vbNormalFocus //调用外部EXE然后通过 // FindWindow(vbNullString, "xxxxxxxx..") //把外部EXE的窗子引到我的程序里作为子窗体,所以我需要这个引入的exe窗体的标题名称,好备 // FindWindow(vbNullString, vbNullString)使用 //我用SPY++看过我要用到的外部程序的窗口类名每次启动都是变化的,标题名称也是变化的 晕了,既然你用shell调用了name.exe,还用FindWindow根据窗口标题获得句柄干什么? 这样获得句柄: dim pid as long pid=Shell("name.exe", vbNormalFocus) dim mhwnd as long'你要找的窗口句柄 mhwnd=InstanceToWnd(pid)'InstanceToWnd函数的代码我上面帖了
//dim pid as long //pid=Shell("name.exe", vbNormalFocus) //dim mhwnd as long'你要找的窗口句柄 //mhwnd=InstanceToWnd(pid)'InstanceToWnd函数的代码我上面帖了好代码啊!!!!收藏!!!!那请问一下,我能得到其他进程的相关窗体句柄吗?(程序不是由自己启动的)或者说,如何得到其他进程的PID呢?虽然我现在什么程序都不做,但多多地学习总没坏处的,呵呵
经测试, rainstormmaster(暴风雨 v2.0) 兄留的代码 pid 有数值而 mhwnd 为0 下面是代码,哪里错了吗?Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long 'Find the first window test_hwnd = FindWindow(ByVal 0&, ByVal 0&) Do While test_hwnd <> 0 'Check if the window isn't a child If GetParent(test_hwnd) = 0 Then 'Get the window's thread test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then InstanceToWnd = test_hwnd Exit Do End If End If 'retrieve the next window test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End FunctionPrivate Sub Form_Load() Dim pid As Long pid = Shell("c:\windows\notepad.exe", vbNormalFocus) Dim mhwnd As Long '你要找的窗口句柄 mhwnd = InstanceToWnd(pid) 'InstanceToWnd函数的代码我上面帖了 Text1.Text = mhwnd '测试输出为0 Text2.Text = pid '测试输出有数值
End Sub
呵呵,不好意思没看到暴风雨的回复,不过比你的全一些,COPY就可以用了。^-^
经测试, rainstormmaster(暴风雨 v2.0) 兄留的代码 pid 有数值而 mhwnd 为0 下面是代码,哪里错了吗?..............声名部分 略Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long 'Find the first window test_hwnd = FindWindow(ByVal 0&, ByVal 0&) Do While test_hwnd <> 0 'Check if the window isn't a child If GetParent(test_hwnd) = 0 Then 'Get the window's thread test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then InstanceToWnd = test_hwnd Exit Do End If End If 'retrieve the next window test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End FunctionPrivate Sub Form_Load() Dim pid As Long pid = Shell("c:\windows\notepad.exe", vbNormalFocus) Dim mhwnd As Long '你要找的窗口句柄 mhwnd = InstanceToWnd(pid) 'InstanceToWnd函数的代码我上面帖了 Text1.Text = mhwnd '测试输出为0 Text2.Text = pid '测试输出有数值
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPublic Function TitleToWnd(ByVal strTitle As String, Optional strClassName As String = vbNullString) As Long
TitleToWnd = FindWindow(strClassName, strTitle)
End FunctionPublic Function WndToProcId(ByVal hwnd As Long) As Long
GetWindowThreadProcessId hwnd, WndToProcId
End FunctionPublic Function WndToTitle(ByVal hwnd As Long) As String
Dim WinText As String
Dim TextSize As Long
WinText = String(255, Chr(0))
TextSize = Len(WinText)
GetWindowText hwnd, WinText, TextSize
WinText = Left(WinText, InStr(1, WinText, Chr(0)) - 1)
WndToTitle = WinText
End Function
不过我是菜鸟,这段代码那个值是我要的标题?
比如我要显示出来: .....上面的代码.....Private Sub Form_Load()
Text1.Text = ?? '这里应该写什么?
End sub
不过我是菜鸟,这段代码那个值是我要的标题?
比如我要显示出来: .....上面的代码.....Private Sub Form_Load()
Text1.Text = ?? '这里应该写什么?
End sub关于
用VB怎样运行EXE文件,代码怎么写,需要引用什么
论坛里面有例程搜一下 “调用外部“
Text1.Text = TitleToWnd(Me.Caption)
Text2.Text = me.hwnd
End sub
--------------
得到的一样。
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Shell "name.exe", vbNormalFocus
调用外部EXE然后通过
FindWindow(vbNullString, "xxxxxxxx..")
把外部EXE的窗子引到我的程序里作为子窗体,所以我需要这个引入的exe窗体的标题名称,好备
FindWindow(vbNullString, vbNullString)使用
我用SPY++看过我要用到的外部程序的窗口类名每次启动都是变化的,标题名称也是变化的
非常感谢上面的朋友,不过上面的回答还是不能解决问题啊!
先关子窗体后关主窗体!!!
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Form_Load()
Dim Pid As Long
'Lock the window update
LockWindowUpdate GetDesktopWindow
'Execute notepad.Exe
Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
If Pid = 0 Then MsgBox "Error starting the app"
'retrieve the handle of the window
mWnd = InstanceToWnd(Pid)
'Set the notepad's parent
SetParent mWnd, Me.hwnd
'Put the focus on notepad
Putfocus mWnd
'Unlock windowupdate
LockWindowUpdate False
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Unload notepad
DestroyWindow mWnd
'End this program
TerminateProcess GetCurrentProcess, 0
End Sub
// Shell "name.exe", vbNormalFocus
//调用外部EXE然后通过
// FindWindow(vbNullString, "xxxxxxxx..")
//把外部EXE的窗子引到我的程序里作为子窗体,所以我需要这个引入的exe窗体的标题名称,好备
// FindWindow(vbNullString, vbNullString)使用
//我用SPY++看过我要用到的外部程序的窗口类名每次启动都是变化的,标题名称也是变化的
晕了,既然你用shell调用了name.exe,还用FindWindow根据窗口标题获得句柄干什么?
这样获得句柄:
dim pid as long
pid=Shell("name.exe", vbNormalFocus)
dim mhwnd as long'你要找的窗口句柄
mhwnd=InstanceToWnd(pid)'InstanceToWnd函数的代码我上面帖了
还用FindWindow根据窗口类名获得句柄干什么
//pid=Shell("name.exe", vbNormalFocus)
//dim mhwnd as long'你要找的窗口句柄
//mhwnd=InstanceToWnd(pid)'InstanceToWnd函数的代码我上面帖了好代码啊!!!!收藏!!!!那请问一下,我能得到其他进程的相关窗体句柄吗?(程序不是由自己启动的)或者说,如何得到其他进程的PID呢?虽然我现在什么程序都不做,但多多地学习总没坏处的,呵呵
pid 有数值而 mhwnd 为0
下面是代码,哪里错了吗?Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End FunctionPrivate Sub Form_Load()
Dim pid As Long
pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
Dim mhwnd As Long '你要找的窗口句柄
mhwnd = InstanceToWnd(pid) 'InstanceToWnd函数的代码我上面帖了 Text1.Text = mhwnd '测试输出为0
Text2.Text = pid '测试输出有数值
End Sub
pid 有数值而 mhwnd 为0
下面是代码,哪里错了吗?..............声名部分 略Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End FunctionPrivate Sub Form_Load()
Dim pid As Long
pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
Dim mhwnd As Long '你要找的窗口句柄
mhwnd = InstanceToWnd(pid) 'InstanceToWnd函数的代码我上面帖了 Text1.Text = mhwnd '测试输出为0
Text2.Text = pid '测试输出有数值
End Sub