admin 发表于 2024-3-9 13:47:13

C++ ARX二次开发-MFC 非模态对话框modaless dialog

一、本节课程
C++ ARX二次开发-MFC 非模态对话框modaless dialog



二、本节要讲解的知识点
显示一个非模态对话框的步骤和代码编写。

三、具体内容
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);

m采用strEnY.Format(TEXT("%.2f"),pt);

m采用strEnZ.Format(TEXT("%.2f"),pt);



}

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);

m采用strStY.Format(TEXT("%.2f"),pt);

m采用strStZ.Format(TEXT("%.2f"),pt);



}

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)
页: [1]
查看完整版本: C++ ARX二次开发-MFC 非模态对话框modaless dialog