对话框数据交换Dialog Data Exchange DDX,它用来在对话框类中创建变量,并与对话框上的控件相关联。
对话框数据验证简称DDV,主要用来设定变量的合理范围。
如果使用DDX,通常在对话框的构造函数或OnInitDialog()中设置其成员变量,因为,在调用DoModal()或Create()函数显示对话框之前,DDX机制就会立即将对话框的成员变量值传送给相应的控件,这样当对话框显示时,控件值也相应的被显示。
UpdateData(TRUE):与控件相关的变量被控件中的文本更新,并且用DDV验证数据值有效性。
UpdataDara(FALSE):用变量值来更新控件上的显示文本。
EXAMPLE:
![对话框数据交换和验证 对话框数据交换和验证](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
建立基于对话框的工程Ishow
主要控件:组框:CButton;
单选按钮:
把三个单选按钮合为一组,先把按钮对其排好,用Tab order确定他们的序号连续,把第一个单选按钮属性选上group,只有选中group的单选组的ID才能在classwizard中添加变量。
把第一个直线单选按钮ID=IDC_RAD_LINE,添加变量为m_radLine(int);
组合框ID=IDC_CMB_STYLE
编辑框ID=IDC_EDT_WIDTH ,添加变量m_lineWidth(int)
用于图形显示的静态文本ID=IDC_ST_SHOW
在对话框头文件里添加变量m_style(int)用于获取线型组合框的值,在OnInitDialog()中初始化为1;
添加变量m_color(COLORREF),用于获取线条的颜色值,在OnInitDialog()中初始化为红色;
除了向组合框中添加选项,还调用SetCurSel()函数选择组合框的一个字符串选项,并将其显示在该组合框内,
该函数原型声明如下:
int SetCurSel(int nSelect)
nSelect用来指定选项的索引位置,它是一个基于0的索引,组合框子在默认情况下是以字符串的开头字母为顺序进行排序,因此如果想要选项按添加顺序显示的话,打开组合框的属性对话框把sort去掉。
如下:
class CIshowDlg : public CDialog
{
// Construction
public:
CIshowDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CIshowDlg)
enum { IDD = IDD_ISHOW_DIALOG };
int m_lineWidth;
int m_radLine;
int m_style;
COLORREF m_color;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CIshowDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CIshowDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnSelchangeCmbStyle();
afx_msg void OnChangeEdtWidth();
afx_msg void OnRadLine();
afx_msg void OnSetcolor();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ISHOWDLG_H__27030CA7_44E5_41EA_8D88_D9A8DA0A31DB__INCLUDED_)
BOOL CIshowDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_style=1;
m_color=RGB(255,0,0);
((CComboBox*)GetDlgItem(IDC_CMB_STYLE))->AddString("实线");
((CComboBox*)GetDlgItem(IDC_CMB_STYLE))->AddString("虚线");
((CComboBox*)GetDlgItem(IDC_CMB_STYLE))->AddString("点线");
((CComboBox*)GetDlgItem(IDC_CMB_STYLE))->SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
为了及时显示数据的变
编辑框,组合框,单选组一有数据的变动就立即显示在对话框上,再改变线宽时,编辑框向对话框发送EN_CHANGE消息。当重新选择一种线型时,组合框向对话框发送CBN_SELCHANGE消息。当单击单选按钮时,
该按钮向对话框发送BN_CLICKED消息。给每个函数都添加一个Invalidate()函数,使得该窗口无效从而让系统发送WM_PAINT消息来重会窗口。
如下:
void CIshowDlg::OnSelchangeCmbStyle()
{
Invalidate();
}
void CIshowDlg::OnChangeEdtWidth()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
Invalidate();
}
void CIshowDlg::OnRadLine()
{
Invalidate();
}
void CIshowDlg::OnPaint()
{
CPaintDC dc(this);
UpdateData();
m_style=((CComboBox*)GetDlgItem(IDC_CMB_STYLE))->GetCurSel();
CPen pen(m_style,m_lineWidth,m_color);
CPen *pOldPen=dc.SelectObject(&pen);
CRect rect;
GetDlgItem(IDC_ST_SHOW)->GetWindowRect(&rect);
ScreenToClient(&rect);
if(m_radLine==0)
{
dc.MoveTo(rect.left+10,rect.top+rect.Height()/2);
dc.LineTo(rect.right-10,rect.top+rect.Height()/2);
}
else if(m_radLine==1)
{
dc.Rectangle(rect.left+20,rect.top+30,rect.right-20,rect.bottom);
}
else
{
dc.Ellipse(rect.left+20,rect.top+30,rect.right-20,rect.bottom);
}
dc.SelectObject(pOldPen);
}
运行程序发现组合框和编辑框的改变可以很快更新到图形显示区,但是单选扭却没有反应;
利用MFC的消息映射宏ON_CONTROL_RANGE可以避免这种麻烦,该映射宏把多个ID连续的控件发出的消息映射到同一个处理函数上,这样,我们只要编写一个消息处理函数就可以对一组单选钮的BN_CLICKED消息作出响应,ClassWizard不支持该宏,必须手工创建。
在IshowDlg.cpp中添加消息映射蓝字部分 ON_CONTROL_RANGE(BN_CLICKED,IDC_RAD_LINE,IDC_RADIO3,OnRadLine)
其中BN_CLICKED是响应消息,IDC_RAD_LINE,IDC_RADIO3,分别是组中的第一个和最后一个,OnRadLine是消息响应函数名,利用这一个函数就可以处理组中任一个单选按钮的消息更新。
BEGIN_MESSAGE_MAP(CIshowDlg, CDialog)
//{{AFX_MSG_MAP(CIshowDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_CMB_STYLE, OnSelchangeCmbStyle)
ON_EN_CHANGE(IDC_EDT_WIDTH, OnChangeEdtWidth)
ON_CONTROL_RANGE(BN_CLICKED,IDC_RAD_LINE,IDC_RADIO3,OnRadLine)
ON_BN_CLICKED(IDC_RAD_LINE, OnRadLine)
ON_BN_CLICKED(IDC_SETCOLOR, OnSetcolor)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
在IshowDlg.h中添加消息声明蓝字部分
class CIshowDlg : public CDialog
{
// Construction
public:
CIshowDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CIshowDlg)
enum { IDD = IDD_ISHOW_DIALOG };
int m_lineWidth;
int m_radLine;
int m_style;
COLORREF m_color;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CIshowDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CIshowDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnSelchangeCmbStyle();
afx_msg void OnChangeEdtWidth();
afx_msg void OnRadLine();
afx_msg void OnSetcolor();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ISHOWDLG_H__27030CA7_44E5_41EA_8D88_D9A8DA0A31DB__INCLUDED_)
用WINDOWS自带的颜色对话框来实现新颜色的选定
颜色按钮的响应函数:
void CIshowDlg::OnSetcolor()
{
// TODO: Add your control notification handler code here
CColorDialog dlg;
if(dlg.DoModal() ==IDOK)
{
m_color=dlg.m_cc.rgbResult;
Invalidate();
}
}