从vb6关闭外部应用程序

时间:2013-08-19 10:18:12

标签: vb6

我使用以下代码打开ACAD应用程序:

Shell“C:\ Program Files \ Autodesk \ AutoCAD 2011 \ acad.exe”

现在我要关闭它。 请帮忙。

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_QUIT As Long = &H12
Private Sub Form_Load()
    Shell "C:\Program Files\Autodesk\AutoCAD 2011\acad.exe", vbNormalFocus
End Sub
Private Sub cmdClose_Click()
    Dim handle As Long
    handle = FindWindow("AutoCAD", vbNullString)
    If handle Then
        PostMessage handle, WM_QUIT, 0&, 0&
    End If
End Sub

注意这一行:

FindWindow("AutoCAD", vbNullString)

您必须将"AutoCAD"更改为该类的名称。您可以使用Spy ++或其他等效程序获取AutoCAD的确切类名。

这里可以找到另一种解决方案,它不使用Shell,而是一种用于跟踪句柄的API(这种方法要好得多,但可能更复杂):

http://www.freevbcode.com/ShowCode.asp?ID=670