| 
UID5280积分9539精华贡献 威望 活跃度 D豆 在线时间 小时注册时间2002-5-18最后登录1970-1-1 
 | 
 
| 
×
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册 
    问题:
 
 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[256];
            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
 
 
 
 | 
 |