通过程序来关闭另一个程序时出现的问题??

doubleh 2003-09-19 10:53:28
Dim pID As Long

Private Sub Command1_Click()
pID = Shell("c:\test\jj.exe", vbNormalFocus)
End Sub

Private Sub Command2_Click()
Dim hWnd As Long
Dim i As Integer
hWnd = FindProcessWindow(pID)
Text1.Text = CStr(hWnd) & vbCrLf & Text1.Text
SetForegroundWindow hWnd
PostMessage hWnd, WM_CLOSE, 0, 0&
End Sub

问题:按Command1打开jj.exe后,要按多次Command2才能关闭,大侠们说说是为什么?

...全文
21 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
doubleh 2003-09-19
  • 打赏
  • 举报
回复
还是不行,每次点击Comamd2得到的hWnd = FindProcessWindow(pID)都不同,照理应是一样的?

代码在这里,你也可以试一下,是另一个大虾给的,这里暂时盗版一下了。:

'窗体中的代码
Option Explicit

Dim pID As Long

Private Sub Command1_Click()
pID = Shell("c:\test\jj.exe", vbNormalFocus) 'jj.exe是用VB编的另一个程序
End Sub

Private Sub Command2_Click()
Dim hWnd As Long
Dim i As Integer

hWnd = FindProcessWindow(pID)
SetForegroundWindow hWnd
'PostMessage hWnd, WM_CLOSE, 0, 0& '用post
SendMessage hWnd, WM_CLOSE, 0, 0&

End Sub





'模板中的代码
Option Explicit

Public Const WM_CLOSE = &H10
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Dim hWndProcess As Long

Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim pID As Long

GetWindowThreadProcessId hWnd, pID
If pID = lParam Then
If GetParent(hWnd) = 0 Then
hWndProcess = hWnd
EnumWindowsProc = False
End If
End If
EnumWindowsProc = True
End Function

Function FindProcessWindow(ByVal pID As Long) As Long
hWndProcess = 0
EnumWindows AddressOf EnumWindowsProc, pID
FindProcessWindow = hWndProcess
End Function
lwm1977 2003-09-19
  • 打赏
  • 举报
回复
学习ing
lwm1977 2003-09-19
  • 打赏
  • 举报
回复
学习ing
Garfield 2003-09-19
  • 打赏
  • 举报
回复
PostMessage是把消息放到目标程序的消息队列
如果目标程序还没处理这个消息就不会立即退出。

可以试一下 SendMessage 代替 PostMessage。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧