2

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

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

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

4

1 回答 1

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

于 2013-08-19T14:08:58.793 回答