C++ ARX二次开发-MFC 非模态对话框modaless dialog
一、本节课程
C++ ARX二次开发-MFC 非模态对话框modaless dialog
yunyou.ke.qq.com
二、本节要讲解的知识点
显示一个非模态对话框的步骤和代码编写。
三、具体内容
1、思路:
显示一个非模式对话框:
yunyou.ke.qq.com
pDialog=new CModalessDlg(acedGetAcadFrame());
pDialog->Create(IDD_DIALOG_MODALESS);
pDialog->ShowWindow(SW_SHOW);
销毁对话框是需要关闭对话框,释放对话框的内存空间。
在关闭对话框的事件中,调用DestroyWindow函数来销毁对话框,重写对话框的PostNcDestroy函数,在这个函数里面要释放分配的内存空间。
非模式对话框修改图形数据库内容的时候,必须在操作之前锁定文档,操作完成之后解锁文档。
实现:
(1)用向导建立一个项目叫做ModalessDialog,记得需要勾选使用MFC。
添加对话框资源和控件。yunyou.ke.qq.com
(3)为对话框添加类:CModalessDlg,添加成员变量,消息响应函数,具体过程见视频回放。
对应的头文件:yunyou.ke.qq.com
//-----------------------------------------------------------------------------
//----- ModalessDlg.h : Declaration of the CModalessDlg
//-----------------------------------------------------------------------------
#pragma once
//-----------------------------------------------------------------------------
#include "acui.h"
//-----------------------------------------------------------------------------
class CModalessDlg : public CAcUiDialog {
DECLARE_DYNAMIC (CModalessDlg)
public:
CModalessDlg (CWnd *pParent =NULL, HINSTANCE hInstance =NULL) ;
enum { IDD = IDD_DIALOG1} ;
protected:
virtual void DoDataExchange (CDataExchange *pDX) ;
afx_msg LRESULT OnAcadKeepFocus (WPARAM, LPARAM) ;
DECLARE_MESSAGE_MAP()
public:
CString m_strEnX;
CString m_strEnY;
CString m_strEnZ;
CString m_strStX;
CString m_strStY;
CString m_strStZ;
CAcUiPickButton m_btnPickSt;
CAcUiPickButton m_btnPickEn;
afx_msg void OnClickedButtonPickEn();
afx_msg void OnClickedButtonPickSt();
afx_msg void OnClickedButtonLine();
afx_msg void OnClickedButtonClose();
afx_msg void OnClose();
virtual BOOL OnInitDialog();
virtual void PostNcDestroy();
} ;
对应的cpp文件:yunyou.ke.qq.com
//-----------------------------------------------------------------------------
//----- ModalessDlg.cpp : Implementation of CModalessDlg
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "ModalessDlg.h"
#include "LineUtil.h"
extern CModalessDlg *pDialog;
//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC (CModalessDlg, CAcUiDialog)
BEGIN_MESSAGE_MAP(CModalessDlg, CAcUiDialog)
ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)
ON_BN_CLICKED(IDC_BUTTON_PICK_EN, &CModalessDlg::OnClickedButtonPickEn)
ON_BN_CLICKED(IDC_BUTTON_PICK_ST, &CModalessDlg::OnClickedButtonPickSt)
ON_BN_CLICKED(ID_BUTTON_LINE, &CModalessDlg::OnClickedButtonLine)
ON_BN_CLICKED(ID_BUTTON_CLOSE, &CModalessDlg::OnClickedButtonClose)
ON_WM_CLOSE()
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
CModalessDlg::CModalessDlg (CWnd *pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (CModalessDlg::IDD, pParent, hInstance)
, m_strEnX(_T(""))
, m_strEnY(_T(""))
, m_strEnZ(_T(""))
, m_strStX(_T(""))
, m_strStY(_T(""))
, m_strStZ(_T(""))
{
}
//-----------------------------------------------------------------------------
void CModalessDlg::DoDataExchange (CDataExchange *pDX) {
CAcUiDialog::DoDataExchange (pDX) ;
DDX_Text(pDX, IDC_EDIT_ENX, m_strEnX);
DDX_Text(pDX, IDC_EDIT_ENY, m_strEnY);
DDX_Text(pDX, IDC_EDIT_ENZ, m_strEnZ);
DDX_Text(pDX, IDC_EDIT_STX, m_strStX);
DDX_Text(pDX, IDC_EDIT_STY, m_strStY);
DDX_Text(pDX, IDC_EDIT_STZ, m_strStZ);
DDX_Control(pDX, IDC_BUTTON_PICK_ST, m_btnPickSt);
DDX_Control(pDX, IDC_BUTTON_PICK_EN, m_btnPickEn);
}
//-----------------------------------------------------------------------------
//----- Needed for modeless dialogs to keep focus.
//----- Return FALSE to not keep the focus, return TRUE to keep the focus
LRESULT CModalessDlg::OnAcadKeepFocus (WPARAM, LPARAM) {
return (TRUE) ;
}
void CModalessDlg::OnClickedButtonPickEn()
{
BeginEditorCommand();//隐藏对话框把控制权交给AUTOCAD
ads_point pt;
if (acedGetPoint(NULL,TEXT("\n输入一个点:"),pt)==RTNORM)
{
CompleteEditorCommand();
m_strEnX.Format(TEXT("%.2f"),pt[X]);
m_strEnY.Format(TEXT("%.2f"),pt[Y]);
m_strEnZ.Format(TEXT("%.2f"),pt[Z]);
}
else
{
CancelEditorCommand();//要取消命令
}
//成员变量的值来更新文本框显示的内容
UpdateData(FALSE);
}
void CModalessDlg::OnClickedButtonPickSt()
{
BeginEditorCommand();//隐藏对话框把控制权交给AUTOCAD
ads_point pt;
if (acedGetPoint(NULL,TEXT("\n输入一个点:"),pt)==RTNORM)
{
CompleteEditorCommand();
m_strStX.Format(TEXT("%.2f"),pt[X]);
m_strStY.Format(TEXT("%.2f"),pt[Y]);
m_strStZ.Format(TEXT("%.2f"),pt[Z]);
}
else
{
CancelEditorCommand();//要取消命令
}
//成员变量的值来更新文本框显示的内容
UpdateData(FALSE);
}
void CModalessDlg::OnClickedButtonLine()
{
AcGePoint3d ptStart(_wtof(m_strStX),_wtof(m_strStY),_wtof(m_strStZ));
AcGePoint3d ptEnd(_wtof(m_strEnX),_wtof(m_strEnY),_wtof(m_strEnZ));
acDocManager->lockDocument(curDoc());
CLineUtil::Add(ptStart,ptEnd);
acDocManager->unlockDocument(curDoc());
}
void CModalessDlg::OnClickedButtonClose()
{
DestroyWindow();
}
void CModalessDlg::OnClose()
{
CAcUiDialog::OnClose();
DestroyWindow();
}
BOOL CModalessDlg::OnInitDialog()
{
CAcUiDialog::OnInitDialog();
m_btnPickEn.AutoLoad();
m_btnPickSt.AutoLoad();
return TRUE;
}
void CModalessDlg::PostNcDestroy()
{
// 用来释放费模态对话框的内存
delete this;
if (pDialog!=NULL)
{
pDialog=NULL;
}
CAcUiDialog::PostNcDestroy();
}
(4)ModalessDialog.cpp里面添加全局对话框变量声明(红色字):
//-----------------------------------------------------------------------------
//- ModalessDialog.cpp : Initialization functions
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include <afxdllx.h>
#include "ModalessDlg.h"
AC_IMPLEMENT_EXTENSION_MODULE(ModalessDialogDLL)
CModalessDlg *pDialog=NULL;
acrxEntryPoint.cpp里面添加命令声明和实现代码
命令实现:
static void yunyouMyGroupModalessDlg () {
CAcModuleResourceOverride resOverride;//防止资源冲突
if (pDialog==NULL)
{
pDialog=new CModalessDlg(acedGetAcadFrame());
pDialog->Create(IDD_DIALOG1);
pDialog->ShowWindow(SW_SHOW);
}
else
{
pDialog->ShowWindow(SW_SHOW);
}
}
命令声明:
ACED_ARXCOMMAND_ENTRY_AUTO(CModalessDialogApp, yunyouMyGroup, ModalessDlg, ModalessDlg, ACRX_CMD_MODAL, NULL)
对话框关闭和资源销毁处理:
ModalessDlg.cpp里面:
void CModalessDlg::OnClickedButtonClose()
{
DestroyWindow();
}
void CModalessDlg::OnClose()
{
CAcUiDialog::OnClose();
DestroyWindow();
}
void CModalessDlg::PostNcDestroy()
{
// 用来释放费模态对话框的内存
delete this;
if (pDialog!=NULL)
{
pDialog=NULL;
}
CAcUiDialog::PostNcDestroy();
}
acrxEntryPoint.cpp里面:
extern CModalessDlg *pDialog;
BOOL CloseDialog()
{
if (pDialog==NULL)
{
return TRUE;
}
BOOL bRet=pDialog->DestroyWindow();
if (bRet)
{
pDialog=NULL;
}
return bRet;
}
.....
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;
CloseDialog();
return (retCode) ;
}
acrxEntryPoint.cpp的完整代码:
//-----------------------------------------------------------------------------
//----- acrxEntryPoint.cpp
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "ModalessDlg.h"
//-----------------------------------------------------------------------------
#define szRDS _RXST("yunyou")
extern CModalessDlg *pDialog;
BOOL CloseDialog()
{
if (pDialog==NULL)
{
return TRUE;
}
BOOL bRet=pDialog->DestroyWindow();
if (bRet)
{
pDialog=NULL;
}
return bRet;
}
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CModalessDialogApp : public AcRxArxApp {
public:
CModalessDialogApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
return (retCode) ;
}
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;
CloseDialog();
return (retCode) ;
}
virtual void RegisterServerComponents () {
}
static void yunyouMyGroupModalessDlg () {
CAcModuleResourceOverride resOverride;//防止资源冲突
if (pDialog==NULL)
{
pDialog=new CModalessDlg(acedGetAcadFrame());
pDialog->Create(IDD_DIALOG1);
pDialog->ShowWindow(SW_SHOW);
}
else
{
pDialog->ShowWindow(SW_SHOW);
}
}
} ;
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CModalessDialogApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CModalessDialogApp, yunyouMyGroup, ModalessDlg, ModalessDlg, ACRX_CMD_MODAL, NULL)
项目完整结构:
————————————————
版权声明:本文为CSDN博主「云幽学院」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liujiangtaocsu/article/details/82917740