用对话框指针来控制创建关闭和清除子对话框

MFC 对话框 工程.主对话框是A类的实例  

A含有成员变量 指针*B  B也是一个对话框类


下面我们就利用这个指针 实现对子对话框的开启 关闭 和销毁


首先是结构上的准备工作,使两个类能够互相调用

添加Dialog_EX 类B

include "B.h"

public:

*B m_son;

init()

{

this->m_son-=NULL;

}

B

class A;

public:

*A m_father;

B.cpp

#include "A.h"


第一部分 创建(并显示)子对话框

A.cpp

if(this->m_son!=NULL)
{
::AfxMessageBox("m_son不等于NULL,不需要再创建");

}
else
{
::AfxMessageBox("m_son为NULL正在创建");
this->m_son=new B; //在堆中创建
this->m_son->Create(IDD_SON);
this->m_son->m_father=this; //建立联系
}

this->m_son->ShowWindow(SW_SHOW);


第二部分 关闭不销毁子对话框

B.cpp

void B::OnCancel()
{
//this->m_father->delete_son();
CDialogEx::OnCancel();
}


第三部分 关闭并销毁子对话框

B.cpp

void B::OnCancel()
{
this->m_father->delete_son();
//CDialogEx::OnCancel();
}


A.cpp

void A::delete_son()
{
delete this->m_son;
this->m_son=NULL;
}

你可能感兴趣的:(MFC学习笔记)