对话框之模式对话框与无模式对话框以及自定义消息

模式对话框

创建模式对话框步骤

  1. 创建模式对话框模板 IDD_COptionsDialog
    IDD_COptionsDialog
  2. 构造一个CDialog对象,并封装对话框模板
COptionsDialog::COptionsDialog(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_COptionsDialog, pParent)
	, m_nHeight(0)
	, m_nWidth(0)
	, m_nUnits(-1)
{
	//  m_nHeight = 0;
	//  m_nUnits = 0;
}

  1. 调用CDialog:Domal()显示对话框
//点击Options 菜单项
void CChildView::OnFileOptions()
{
	// TODO: 在此添加命令处理程序代码
		COptionsDialog m_CODlg;
	    m_CODlg.m_nWidth = m_nWidth;
		m_CODlg.m_nHeight = m_nHeight ;
		m_CODlg.m_nUnits = m_nUnits  ;
		if (m_CODlg.DoModal()==IDOK)
		{
			m_nWidth = m_CODlg.m_nWidth;
			m_nHeight = m_CODlg.m_nHeight;
			m_nUnits = m_CODlg.m_nUnits;
			Invalidate();
		}
}
无模式对话框

在这里插入图片描述

  1. 创建对话框模板 IDD_COptionsDialog
    IDD_COptionsDialog
  2. 构造一个CDialog对象,并封装进对话框模板
COptionsDialog::COptionsDialog(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_COptionsDialog, pParent)
	, m_nHeight(0)
	, m_nWidth(0)
	, m_nUnits(-1)
{
}

  1. 单击菜单项显示无模式对话框
void CChildView::OnFileOptions()
{
	// TODO: 在此添加命令处理程序代码
	if (m_pDialog != NULL)
		m_pDialog->SetFocus();
	else 
	{
		m_pDialog = new COptionsDialog;
		m_pDialog->m_nWidth = m_nWidth;
		m_pDialog->m_nHeight = m_nHeight;
		m_pDialog->m_nUnits = m_nUnits;
		m_pDialog->Create(IDD_COptionsDialog);
		m_pDialog->ShowWindow(SW_SHOW);
	}
}
  1. 覆盖OnOKOnCancel
void COptionsDialog::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

		UpdateData(TRUE);//上传Edit 里面的数据到成员
		RECTPROP rect;
		rect.m_nHeight = m_nHeight;
		rect.m_nwidth = m_nWidth;
		rect.m_nUnits = m_nUnits;
		if (m_nWidth > 0 && m_nWidth <= 128)
		{
			AfxGetMainWnd()->SendMessage(WM_USER_APPLY, 0, (LPARAM)&rect);
		}

	//CDialog::OnOK();
}

void COptionsDialog::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类
	DestroyWindow(); //调用PostNcDestroy函数
	//CDialog::OnCancel();
}

void COptionsDialog::PostNcDestroy()
{
	// TODO: 在此添加专用代码和/或调用基类
	CDialog::PostNcDestroy();
	AfxGetMainWnd()->SendMessage(WM_USER_DESTROYDIALOG, 0, 0);
	delete this;
}
  • 对话框消息传递给MainFrame
Created with Raphaël 2.2.0 对框框窗口向MainFrame 消息WM_USER_DESTROYDIALOG MainFrame 向childView 发送WM_USER_DESTROYDIALOG childView 处理WM_USER_DESTROYDIALOG 结束
  • 代码
//WM_USER_DESTROYDIALOG 自定义消息
//#define WM_USER_DESTROYDIALOG 0x400 + 0x100
//利用ON_MESSAGE() 宏映射自定义消息
/* 
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_MESSAGE(WM_USER_APPLY,OnApply)
	ON_MESSAGE(WM_USER_DESTROYDIALOG, OnDestoryDialog)//
END_MESSAGE_MAP()
*/
//COptionsDialog.cpp
void COptionsDialog::PostNcDestroy()
{
	// TODO: 在此添加专用代码和/或调用基类
	CDialog::PostNcDestroy();
	AfxGetMainWnd()->SendMessage(WM_USER_DESTROYDIALOG, 0, 0);
	delete this;//自己删除new出来的dialog 对象
}

//MainFrame.cpp
LRESULT CMainFrame::OnDestoryDialog(WPARAM, LPARAM)
{
	m_wndView.SendMessage(WM_USER_DESTROYDIALOG, 0, 0);
	return 0;
}


利用自定义消息传递数据
//自定义结构体
//targetver.h
typedef struct 
{
	int m_nwidth;
	int m_nHeight;
	int m_nUnits;
}RECTPROP;

//COptionsDialogs.cpp
void COptionsDialog::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

		UpdateData(TRUE);//上传Edit 里面的数据到成员
		RECTPROP rect;
		rect.m_nHeight = m_nHeight;
		rect.m_nwidth = m_nWidth;
		rect.m_nUnits = m_nUnits;
		if (m_nWidth > 0 && m_nWidth <= 128)
		{
			//发送数据rect
			AfxGetMainWnd()->SendMessage(WM_USER_APPLY, 0, (LPARAM)&rect);
		}

	//CDialog::OnOK();
}

//childview.cpp 取出数据
LRESULT CChildView::OnApply(WPARAM wParam, LPARAM lParam)
{
	RECTPROP* prp = (RECTPROP*)lParam;
	m_nWidth = prp->m_nwidth;
	m_nHeight = prp->m_nHeight;
	m_nUnits = prp->m_nUnits;
	
	switch (m_nUnits)
	{
	case 0:
		m_nViewHeight = m_nHeight * 100;
		break;
	case 1:
		m_nViewHeight = m_nHeight * 100;
		break;
	case 2:
		m_nViewHeight = m_nHeight;
		break;
	}
属性表CProperSheet与CProperPage
  • 创建 模式属性表的步骤如下:

1.针对属性表的每一页创建一个对话框模板,定义页的内容和特性。将对话框标题
设置成您希望在属性表页上方标签中显现的标题。
2.针对属性表的每一页由CPropertyPage 派生出一个类似对话框的类,其中包含通过
DDXDDV与页中控件相联系的公用数据成员。
3.由CPropertySheet****派生出一个属性表类。将该属性表类和第2步中得到的属性表页
类实例化
。利用CPropertySheet :AddPage将各页按期望中的显示顺序添加到属性表中。
4.调用属性表的 DoModal函数将属性表显示在屏幕上。

  • 设置模板sizepage,colorpage
    sziepagecolorpage
  • 派生CPropertyPage colorpage与sizepage
//colorpage
class CColorPage : public CMFCPropertyPage
{
	DECLARE_DYNAMIC(CColorPage)

public:

	CColorPage();
	virtual ~CColorPage();

protected:
	DECLARE_MESSAGE_MAP()
public:
	int m_nColor;
	afx_msg void OnBnClickedRadio();
	virtual void DoDataExchange(CDataExchange* pDX);
};

///sizepage
class CSizePage : public CMFCPropertyPage
{
	DECLARE_DYNAMIC(CSizePage)
public:
	int m_nWidth;
	int m_nHeight;
	int m_nUnits;
public:
	CSizePage();
	virtual ~CSizePage();

protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnEnChangeEdit();
	
	afx_msg void OnBnClickedRadio();
	virtual void DoDataExchange(CDataExchange* pDX);
};
  • 将page实例化并添加到CPropertySheet

// CMyPropertySheet.h

class CMyPropertySheet : public CPropertySheet
{
	DECLARE_DYNAMIC(CMyPropertySheet)

public:
	CMyPropertySheet(UINT nIDCaption, CWnd* pParentWnd = nullptr, UINT iSelectPage = 0);
	CMyPropertySheet(LPCTSTR pszCaption, CWnd* pParentWnd = nullptr, UINT iSelectPage = 0);
	virtual ~CMyPropertySheet();

protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnApply();
public:
	int m_nWidth;
	int m_nHeight;
	int m_nUnits;
	int m_nColor;
	CColorPage m_CColor;
	CSizePage m_CSize;
};

//CMyPropertySheet.cpp 实现
CMyPropertySheet::CMyPropertySheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{

	m_nWidth = 0;
	m_nHeight = 0;
	m_nUnits = 0;
	m_nColor = 0;
	//添加page到sheet
	AddPage(&m_CSize);
	AddPage(&m_CColor);
}
  • Domal() 显示CProperSheet
void CChildView::OnFileOptions()
{
	// TODO: 在此添加命令处理程序代码
	CMyPropertySheet ps(TEXT("Properties"));

	ps.m_CSize.m_nWidth = m_nWidth;
	ps.m_CSize.m_nHeight = m_nHeight;
	ps.m_CSize.m_nUnits = m_nUnits;
	ps.m_CColor.m_nColor = m_nColor;

	if (ps.DoModal() == IDOK)
	{
		m_nWidth = ps.m_CSize.m_nWidth;
		m_nHeight = ps.m_CSize.m_nHeight;
		m_nUnits = ps.m_CSize.m_nUnits;
		m_nColor = ps.m_CColor.m_nColor;
		Invalidate();
	}

}
  • 运行效果

sizecolor

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

木兮₀₃₂₁

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值