两个程序之间如何实现通信,可以通过程序来关闭另一个程序吗??

doubleh 2003-09-19 10:01:16
两个程序之间如何实现通信,可以通过程序来关闭另一个程序吗??
...全文
129 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
doubleh 2003-09-19
  • 打赏
  • 举报
回复
点击5下就关闭了
doubleh 2003-09-19
  • 打赏
  • 举报
回复
谢谢 Gelim(Gelim) 兄:
基本可以了,但关闭我的程序的时候,不知道为什么要连续点击多下Command2才能关闭。
Gelim 2003-09-19
  • 打赏
  • 举报
回复
我给你相关代码,看能否帮助你!

以下为放在FORM中的代码:
Option Explicit

Dim pID As Long

Private Sub Command1_Click()
pID = Shell("notepad", vbNormalFocus)
End Sub

Private Sub Command2_Click()
Dim hWnd As Long

hWnd = FindProcessWindow(pID)
SetForegroundWindow hWnd
PostMessage hWnd, WM_CLOSE, 0, 0&
End Sub

以下为放在modules中的代码:
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 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


doubleh 2003-09-19
  • 打赏
  • 举报
回复
谢谢,有代码吗??
射天狼 2003-09-19
  • 打赏
  • 举报
回复
通过DDE通信
先根据FINDWINDOWS函数取得程序的句柄,然后通过CLOSEWINDOW关闭程序.
northwolves 2003-09-19
  • 打赏
  • 举报
回复
关闭其进程

7,763

社区成员

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

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