newer 发表于 2021-1-16 21:09:23

通过ActiveX client 取消(cancel)一个命令


问题:

If I send commands to the AutoCAD command line from an external VB application
using Windows' API functions such as SendKeys or SendMessage, this works except
when another command is busy. How can I cancel this command? If I send an Escape
(27) or an CTRL-C, I don't get any results. The escape character is send to the
AutoCAD window, but this only happens after I cancel the command by pressing the
Escape key.


解答:

Through an ActiveX client that is written in C++ or VB code, you should always
be able to cancel an active AutoCAD command at the command line.

From a client application written in C++, the following code cancels an active
AutoCAD command at the command line even with transparent commands.


HWND HWND_ACAD;

void SendCmdToAcad(CString& cmd)
{
            COPYDATASTRUCT cmdMsg;
            cmdMsg.dwData = (DWORD)1;
            cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
            cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;

            SendMessage(HWND_ACAD, WM_COPYDATA, NULL, (LPARAM)&cmdMsg);
}

void doCancel()
{
            CString txt = "\x1B\x1B";
            SendCmdToAcad(txt);
}


The trick is to find the AutoCAD window handle on the desktop. To do this,
traverse the Windows desktop to locate AutoCAD.


CString GetWindowTitle(HWND &hwnd)
{
            char s;
            if (!::GetWindowText(hwnd, s, sizeof(s)))
                        return "";                     // ignore empty titles
            else
                        return s;
}

void SendCmdToAcad(CString& cmd)
{
            COPYDATASTRUCT cmdMsg;
            cmdMsg.dwData = (DWORD)1;
            cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
            cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;

            SendMessage(HWND_ACAD, WM_COPYDATA, NULL, (LPARAM)&cmdMsg);
}

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
            CString title = GetWindowTitle(hwnd);
            if(title == "")
                        return TRUE;
            if(title.Find("AutoCAD") != -1)
            {
                        HWND_ACAD = hwnd;
                        return FALSE;
            }
            else
                        return TRUE;
}

BOOL FindAcadFrameWnd()
{
            //WNDENUMPROC lpEnumFunc = NULL;
            if(EnumWindows((WNDENUMPROC) EnumWindowsProc, NULL))
                        return TRUE;
            else
                        return FALSE;
}


The preceding code resides in an ActiveX client application. More specifically,
it is a dialog based MFC AppWizard EXE. The attached project shows all the code.

In a client written in VB code, make sure you call AppActivate before you use
SendKeys(). Otherwise the string is sent to whatever window is active, which may
not always be the AutoCAD window.


dim AcadObj as Object

Private Sub GetAcadObj_Click()
   On Error Resume Next
   Const acadAppName = "AutoCAD.Application"
   Set AcadObj = GetObject(, acadAppName)
   If Err Then
            Err.Clear
            Set AcadObj = CreateObject(acadAppName)
            If Err Then
                MsgBox Err.Source
                Exit Sub
            End If
   End If

   AcadObj.Visible = True 'hidden by default after creation
End Sub

Private Sub CancelCmd_Click()
   Dim cmdStr
   cmdStr = Chr(27) + Chr(27)
   AppActivate AcadObj.Application.Caption
   SendKeys cmdStr, True
End Sub



You can also convert the preceding C++ code to VB code to achieve the same
results. SendKeys() seems to be much easier in this case.

To find the handle of the AutoCAD, see DevNote #1610.

See these related solutions:
DevNote #1610
DevNote #2855
DevNote #2549
DevNote #17088


页: [1]
查看完整版本: 通过ActiveX client 取消(cancel)一个命令