Const WM_CLOSE = &H10call PostMessage(Form.hwnd,WM_CLOSE,0&,0&) 'Form.hWnd为要关闭的窗体句柄试试?

解决方案 »

  1.   

    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Const PROCESS_TERMINATE = &H1''用于结束外部进程,hCloseWnd 是要结束的程序的主窗口的 HWND
    Public Function TernamiteProcessByHWND(ByVal hCloseWnd As Long) As Boolean
    Dim hProcessID  As Long
    Dim hProcess    As Long
    On Error GoTo PROC_EXIT
        If hCloseWnd = 0 Then GoTo PROC_EXIT
        If GetWindowThreadProcessId(hCloseWnd, hProcessID) = 0 Then GoTo PROC_EXIT
        hProcess = OpenProcess(PROCESS_TERMINATE, False, hProcessID)
        If hProcess = 0 Then GoTo PROC_EXIT
        If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT
        TernamiteProcessByHWND = True
    PROC_EXIT:
        If Err.Number <> 0 Then
            Debug.Print Err.Description
            Err.Clear
        End If
    End Function