找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1528|回复: 0

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

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-16 21:09:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

问题:

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.

  1. HWND HWND_ACAD;

  2. void SendCmdToAcad(CString& cmd)
  3. {
  4.             COPYDATASTRUCT cmdMsg;
  5.             cmdMsg.dwData = (DWORD)1;
  6.             cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
  7.             cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;

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

  10. void doCancel()
  11. {
  12.             CString txt = "\x1B\x1B";
  13.             SendCmdToAcad(txt);
  14. }


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

  1. CString GetWindowTitle(HWND &hwnd)
  2. {
  3.             char s[256];
  4.             if (!::GetWindowText(hwnd, s, sizeof(s)))
  5.                         return "";                       // ignore empty titles
  6.             else
  7.                         return s;
  8. }

  9. void SendCmdToAcad(CString& cmd)
  10. {
  11.             COPYDATASTRUCT cmdMsg;
  12.             cmdMsg.dwData = (DWORD)1;
  13.             cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
  14.             cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;

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

  17. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
  18. {
  19.             CString title = GetWindowTitle(hwnd);
  20.             if(title == "")
  21.                         return TRUE;
  22.             if(title.Find("AutoCAD") != -1)
  23.             {
  24.                         HWND_ACAD = hwnd;
  25.                         return FALSE;
  26.             }
  27.             else
  28.                         return TRUE;
  29. }

  30. BOOL FindAcadFrameWnd()
  31. {
  32.             //WNDENUMPROC lpEnumFunc = NULL;
  33.             if(EnumWindows((WNDENUMPROC) EnumWindowsProc, NULL))
  34.                         return TRUE;
  35.             else
  36.                         return FALSE;
  37. }


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.

  1. dim AcadObj as Object

  2. Private Sub GetAcadObj_Click()
  3.    On Error Resume Next
  4.    Const acadAppName = "AutoCAD.Application"
  5.    Set AcadObj = GetObject(, acadAppName)
  6.    If Err Then
  7.             Err.Clear
  8.             Set AcadObj = CreateObject(acadAppName)
  9.             If Err Then
  10.                 MsgBox Err.Source
  11.                 Exit Sub
  12.             End If
  13.    End If

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

  16. Private Sub CancelCmd_Click()
  17.    Dim cmdStr
  18.    cmdStr = Chr(27) + Chr(27)
  19.    AppActivate AcadObj.Application.Caption
  20.    SendKeys cmdStr, True
  21. 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


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-18 10:39 , Processed in 0.271601 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表