找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1776|回复: 0

通过 ActiveX client 取消命令

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-2-2 12:00:02 | 显示全部楼层 |阅读模式

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

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

×
Cancelling a command through an ActiveX client

问题:

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. }
  15. The trick is to find the AutoCAD window handle on the desktop. To do this,
  16. traverse the Windows desktop to locate AutoCAD.


  17. CString GetWindowTitle(HWND &hwnd)
  18. {
  19.     char s[256];
  20.     if (!::GetWindowText(hwnd, s, sizeof(s)))
  21.         return "";          // ignore empty titles
  22.     else
  23.         return s;
  24. }

  25. void SendCmdToAcad(CString& cmd)
  26. {
  27.     COPYDATASTRUCT cmdMsg;
  28.     cmdMsg.dwData = (DWORD)1;
  29.     cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
  30.     cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;

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

  33. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
  34. {
  35.     CString title = GetWindowTitle(hwnd);
  36.     if(title == "")
  37.         return TRUE;
  38.     if(title.Find("AutoCAD") != -1)
  39.     {
  40.         HWND_ACAD = hwnd;
  41.         return FALSE;
  42.     }
  43.     else
  44.         return TRUE;
  45. }

  46. BOOL FindAcadFrameWnd()
  47. {
  48.     //WNDENUMPROC lpEnumFunc = NULL;
  49.     if(EnumWindows((WNDENUMPROC) EnumWindowsProc, NULL))
  50.         return TRUE;
  51.     else
  52.         return FALSE;
  53. }


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

<coce_end>

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 #17088

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

本版积分规则

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

GMT+8, 2024-11-21 17:49 , Processed in 0.226713 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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