要实现的功能是:
执行ArxModal命令,弹出如图所示对话框选择点,则得到点坐标,选择角度则得到角度值。
步骤一:新建基于MFC的ObjectArx项目,
参考: http://www.cnblogs.com/greatverve/archive/2010/05/31/ObjectARX-HelloWorld.html
打开资源视图添加一个对话框ID修改为IDD_ARX_MODAL
(右击资源视图中的对话框打开属性面板,可以修改ID)
设计如图界面,ID如下:
IDC_BUTTON_POINT
IDC_BUTTON_ANGLE
IDC_EDIT_XPT
IDC_EDIT_YPT
IDC_EDIT_ZPT
IDC_EDIT_ANGLE
选择两个Button把Owner Draw设置为True
完成界面。
步骤二:
打开类视图,右击项目->添加类(这里不是右击对话框添加类)
这张图有点小错误,这里Dialog ID:IDD_ARX_MODAL Class name:CArxDialog
在类视图中右击CArxDialog类添加变量
这样会在头文件中生成
源文件中生成
根据这个规律添加其他变量
CAcUiPickButton m_btnAngle;
CAcUiPickButton m_btnPoint;
CAcUiNumericEdit m_editXpt;
CAcUiNumericEdit m_editYpt;
CAcUiNumericEdit m_editZpt;
CAcUiAngleEdit m_editAngle;
void CArxDialog::DoDataExchange (CDataExchange * pDX) {
CAcUiDialog::DoDataExchange (pDX) ;
DDX_Control(pDX, IDC_BUTTON_ANGLE, m_btnAngle);
DDX_Control(pDX, IDC_BUTTON_POINT, m_btnPoint);
DDX_Control(pDX, IDC_EDIT_XPT, m_editXpt);
DDX_Control(pDX, IDC_EDIT_YPT, m_editYpt);
DDX_Control(pDX, IDC_EDIT_ZPT, m_editZpt);
DDX_Control(pDX, IDC_EDIT_ANGLE, m_editAngle);
}
步骤三:
为CArxDialog添加InitDialog消息响应。
方法是打开类视图,右击->属性
再添加OnClose()响应函数
在头文件中添加几个变量
public:
CString m_strAngle;
CString m_strZPt;
CString m_strYPt;
CString m_strXPt;
在头文件中定义两函数
void DisplayPoint();
void DisplayAngle();
分别为两个按钮添加单击事件,为四个编辑框添加失去焦点事件。
步骤四:
打开acrxEntryPoint.cpp添加#include “ArxDialog.h”
运行结果如图
源码如下
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
// -----------------------------------------------------------------------------
// ----- acrxEntryPoint.h
// -----------------------------------------------------------------------------
#include " StdAfx.h "
#include " resource.h "
#include " ArxDialog.h "
// -----------------------------------------------------------------------------
#define szRDS _RXST("")
// -----------------------------------------------------------------------------
// ----- ObjectARX EntryPoint
class CCADMFCApp : public AcRxArxApp {
public :
CCADMFCApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg ( void * pkt) {
// TODO: Load dependencies here
// You *must* call On_kInitAppMsg here
AcRx::AppRetCode retCode = AcRxArxApp::On_kInitAppMsg (pkt) ;
// TODO: Add your initialization code here
return (retCode) ;
}
virtual AcRx::AppRetCode On_kUnloadAppMsg ( void * pkt) {
// TODO: Add your code here
// You *must* call On_kUnloadAppMsg here
AcRx::AppRetCode retCode = AcRxArxApp::On_kUnloadAppMsg (pkt) ;
// TODO: Unload dependencies here
return (retCode) ;
}
virtual void RegisterServerComponents () {
}
// - CADMFC.showDlg command (do not rename)
static void CADMFCshowDlg( void )
{
// Add your code for command CADMFC.showDlg here
// 防止资源冲突
CAcModuleResourceOverride resOverride;
// 显示ObjectARX的模态对话框
CArxDialog theDialog;
theDialog.DoModal();
}
} ;
// -----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CCADMFCApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CCADMFCApp, CADMFC, showDlg, MyCommand1, ACRX_CMD_TRANSPARENT, NULL)
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
// -----------------------------------------------------------------------------
// ----- ArxDialog.h : Declaration of the CArxDialog
// -----------------------------------------------------------------------------
#pragma once
// -----------------------------------------------------------------------------
#include " acui.h "
// -----------------------------------------------------------------------------
class CArxDialog : public CAcUiDialog {
DECLARE_DYNAMIC (CArxDialog)
public :
CArxDialog (CWnd * pParent = NULL, HINSTANCE hInstance = NULL) ;
enum { IDD = IDD_ARX_MODAL} ;
protected :
virtual void DoDataExchange (CDataExchange * pDX) ;
afx_msg LRESULT OnAcadKeepFocus (WPARAM, LPARAM) ;
DECLARE_MESSAGE_MAP()
private :
CAcUiPickButton m_btnAngle;
CAcUiPickButton m_btnPoint;
CAcUiNumericEdit m_editXpt;
CAcUiNumericEdit m_editYpt;
CAcUiNumericEdit m_editZpt;
CAcUiAngleEdit m_editAngle;
public :
virtual BOOL OnInitDialog();
afx_msg void OnClose();
public :
CString m_strAngle;
CString m_strZPt;
CString m_strYPt;
CString m_strXPt;
void DisplayPoint();
void DisplayAngle();
afx_msg void OnBnClickedButtonPoint();
afx_msg void OnBnClickedButtonAngle();
afx_msg void OnEnKillfocusEditXpt();
afx_msg void OnEnKillfocusEditYpt();
afx_msg void OnEnKillfocusEditZpt();
afx_msg void OnEnKillfocusEditAngle();
} ;
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
// -----------------------------------------------------------------------------
// ----- ArxDialog.cpp : Implementation of CArxDialog
// -----------------------------------------------------------------------------
#include " StdAfx.h "
#include " resource.h "
#include " ArxDialog.h "
// -----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC (CArxDialog, CAcUiDialog)
BEGIN_MESSAGE_MAP(CArxDialog, CAcUiDialog)
ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_POINT, & CArxDialog::OnBnClickedButtonPoint)
ON_BN_CLICKED(IDC_BUTTON_ANGLE, & CArxDialog::OnBnClickedButtonAngle)
ON_EN_KILLFOCUS(IDC_EDIT_XPT, & CArxDialog::OnEnKillfocusEditXpt)
ON_EN_KILLFOCUS(IDC_EDIT_YPT, & CArxDialog::OnEnKillfocusEditYpt)
ON_EN_KILLFOCUS(IDC_EDIT_ZPT, & CArxDialog::OnEnKillfocusEditZpt)
ON_EN_KILLFOCUS(IDC_EDIT_ANGLE, & CArxDialog::OnEnKillfocusEditAngle)
END_MESSAGE_MAP()
// -----------------------------------------------------------------------------
CArxDialog::CArxDialog (CWnd * pParent /* =NULL */ , HINSTANCE hInstance /* =NULL */ ) : CAcUiDialog (CArxDialog::IDD, pParent, hInstance) {
}
// -----------------------------------------------------------------------------
void CArxDialog::DoDataExchange (CDataExchange * pDX) {
CAcUiDialog::DoDataExchange (pDX) ;
DDX_Control(pDX, IDC_BUTTON_ANGLE, m_btnAngle);
DDX_Control(pDX, IDC_BUTTON_POINT, m_btnPoint);
DDX_Control(pDX, IDC_EDIT_XPT, m_editXpt);
DDX_Control(pDX, IDC_EDIT_YPT, m_editYpt);
DDX_Control(pDX, IDC_EDIT_ZPT, m_editZpt);
DDX_Control(pDX, IDC_EDIT_ANGLE, m_editAngle);
}
// -----------------------------------------------------------------------------
// ----- Needed for modeless dialogs to keep focus.
// ----- Return FALSE to not keep the focus, return TRUE to keep the focus
LRESULT CArxDialog::OnAcadKeepFocus (WPARAM, LPARAM) {
return (TRUE) ;
}
BOOL CArxDialog::OnInitDialog()
{
CAcUiDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
// 设置点的范围
m_editXpt.SetRange( - 100.0 , 100.0 );
m_editYpt.SetRange( - 100.0 , 100.0 );
m_editZpt.SetRange( - 100.0 , 100.0 );
// 设置角度的输入范围
m_editAngle.SetRange( 0.0 , 90.0 );
// 加载默认的位图
m_btnPoint.AutoLoad();
m_btnAngle.AutoLoad();
// 设置文本框的默认值
m_strAngle = _T( " 0.0 " );
m_strXPt = _T( " 0.0 " );
m_strYPt = _T( " 0.0 " );
m_strZPt = _T( " 0.0 " );
// 显示初始点的坐标和角度值
DisplayPoint();
DisplayAngle();
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CArxDialog::DisplayPoint()
{
// 在对话框中显示点的坐标
m_editXpt.SetWindowText(m_strXPt);
m_editXpt.Convert(); // 更新控件和其关联的成员变量
m_editYpt.SetWindowText(m_strYPt);
m_editYpt.Convert();
m_editZpt.SetWindowText(m_strZPt);
m_editZpt.Convert();
}
void CArxDialog::DisplayAngle()
{
m_editAngle.SetWindowText(m_strAngle);
m_editAngle.Convert();
}
void CArxDialog::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
// double x=atof(m_strXPt); // 错误 1 error C2664: “atof”: 不能将参数 1 从“CString”转换为“const char *”
double x = _tstof(m_strXPt);
double y = _tstof(m_strYPt);
double z = _tstof(m_strZPt);
acutPrintf(_T( " \n选择的点坐标为(%.2f,%.2f,%.2f). " ),x,y,z);
CAcUiDialog::OnClose();
}
void CArxDialog::OnBnClickedButtonPoint()
{
// TODO: 在此添加控件通知处理程序代码
// 隐藏对话框把控制权交给AutoCad
BeginEditorCommand();
// 提示用户输入一个点
ads_point pt;
if (acedGetPoint(NULL,_T( " \n输入一个点: " ),pt) == RTNORM)
{
// 如果点有效,继续执行
CompleteEditorCommand();
m_strXPt.Format(_T( " %.2f " ),pt[X]);
m_strYPt.Format(_T( " %.2f " ),pt[Y]);
m_strZPt.Format(_T( " %.2f " ),pt[Z]);
// 显示点的坐标
DisplayPoint();
}
else
{
CancelEditorCommand();
}
}
void CArxDialog::OnBnClickedButtonAngle()
{
// TODO: 在此添加控件通知处理程序代码
// 把隐藏对话框把控制权交给autocad
BeginEditorCommand();
// 将当前选择的点的位置作为基点
ads_point pt;
acdbDisToF(m_strXPt, - 1 , & pt[X]);
acdbDisToF(m_strYPt, - 1 , & pt[Y]);
acdbDisToF(m_strZPt, - 1 , & pt[Z]);
// 提示用户输入一点
double angle;
const double PI = 4 * atan( 1.0 ); // 错误 1 error C2668: “atan”: 对重载函数的调用不明确 d:\codes\cpp\dlg\dlg\arxdlg.cpp 167
if (acedGetAngle(pt,_T( " \n输入角度: " ), & angle) == RTNORM)
{
// 如果正确获得角度,返回对话框
CompleteEditorCommand();
// 将角度值转换为弧度值
m_strAngle.Format(_T( " %.2f " ),angle * ( 180.0 / PI));
// 显示角度值
DisplayAngle();
}
else
{
// 否则取消命令(包括对话框)
CancelEditorCommand();
}
}
void CArxDialog::OnEnKillfocusEditXpt()
{
// TODO: 在此添加控件通知处理程序代码
m_editXpt.Convert();
m_editXpt.GetWindowText(m_strXPt);
}
void CArxDialog::OnEnKillfocusEditYpt()
{
// TODO: 在此添加控件通知处理程序代码
m_editYpt.Convert();
m_editYpt.GetWindowText(m_strYPt);
}
void CArxDialog::OnEnKillfocusEditZpt()
{
// TODO: 在此添加控件通知处理程序代码
// 获得并更新用户输入的值
m_editZpt.Convert();
m_editZpt.GetWindowText(m_strZPt);
}
void CArxDialog::OnEnKillfocusEditAngle()
{
// TODO: 在此添加控件通知处理程序代码
m_editAngle.Convert();
m_editAngle.GetWindowText(m_strAngle);
}
参考:
本文word:http://files.cnblogs.com/greatverve/cad-mfc.rar