MFC中实现父对话框与子对话框之间的参数传递,实例可测
2017-04-19 10:28
351 查看
一、构件工程:
1.新建一个工程CPassParametersDlg,在界面上添加一个Edit(添加变量:m_result),
添加两个Button其他设置默认,修改Caption分别为:启动子对话框||发送数据给子窗口
2.在CPassParametersDlg.h中加入:
public:
CEdit m_result;
CString FarherData;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
在CPassParametersDlg.cpp中加入:
BOOL CPassParametersDlg::OnInitDialog()
{
...
int a = 20;
FarherData.Format(_T("%d"), a);
m_result.SetWindowText(FarherData);
...
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
添加两个Button其他设置默认,修改Caption分别为:获得父窗口参数||发送数据给父窗口
在CChild.h中添加
public:
CString ChildData;
CEdit m_result2;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
virtual BOOL OnInitDialog();在CChild.cpp中添加
void CChild::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CPassParametersDlg *pFatherDlg = (CPassParametersDlg*)GetParent();
ChildData = pFatherDlg->FarherData;
m_result2.SetWindowText(ChildData);
}
BOOL CChild::OnInitDialog()
{
CDialogEx::OnInitDialog();
CPassParametersDlg *pFatherDlg;
pFatherDlg = (CPassParametersDlg*)GetParent();
m_result2.SetWindowText(ChildData);
return TRUE;
}
void CChild::OnBnClickedButton2()
{
CPassParametersDlg * parent = (CPassParametersDlg*)GetParent();
//CString FarherData;
this->m_result2.GetWindowText(ChildData);//获取当前子窗口编辑框中的值
parent->m_result.SetWindowText(ChildData);//把当前编辑框中的值回传给父窗口
}4.在CChild.h中加入#include "PassParametersDlg.h"
在CPassParametersDlg.h中加入#include "Child.h"
5,运行效果如下图:
1.点击启动子对话框,并点击获得父窗口参数,结果:
2,在子窗口Edit中写入hello world,点击发送数据给父窗口,父窗口显示结果:
3.点击子窗口确定按钮,在父窗口中输入 new year,点击发送数据给子窗口,子窗口显示:
ps:此处需要关闭子窗口才能操作,如何不关闭直接操作父窗口待做。
二、思考
1.Edit如何显示字符?
m_result.SetWindowText(_T("火星人"));
2.如何将一个int整数显示在Edit上?
int a =20;
CString str;
str.Format(_T("%d"),a);
m_result.SetWindowText(str);
3.完整代码见:http://download.csdn.net/detail/huoxingrenhdh/9819228
1.新建一个工程CPassParametersDlg,在界面上添加一个Edit(添加变量:m_result),
添加两个Button其他设置默认,修改Caption分别为:启动子对话框||发送数据给子窗口
2.在CPassParametersDlg.h中加入:
public:
CEdit m_result;
CString FarherData;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
在CPassParametersDlg.cpp中加入:
BOOL CPassParametersDlg::OnInitDialog()
{
...
int a = 20;
FarherData.Format(_T("%d"), a);
m_result.SetWindowText(FarherData);
...
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CPassParametersDlg::OnBnClickedButton1() { CChild dlg; dlg.SetBackgroundColor(RGB(255, 155, 0)); dlg.DoModal(); //设置子对话框背景颜色黄色 CDiologEx中 } void CPassParametersDlg::OnBnClickedButton2() { CChild dlg; //CString FarherData; m_result.GetWindowText(FarherData);//获取当前对话框中的文本框内容 dlg.SetBackgroundColor(RGB(255, 155, 0)); dlg.ChildData = FarherData; dlg.DoModal(); //设置子对话框背景颜色黄色 CDiologEx中 }3.双击左侧资源文件PassParameters.rc,在Dialog处右键添加资源,选择类,类名为CChild。并在对话框上同样添加一个Edit(添加变量:m_result2),
添加两个Button其他设置默认,修改Caption分别为:获得父窗口参数||发送数据给父窗口
在CChild.h中添加
public:
CString ChildData;
CEdit m_result2;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
virtual BOOL OnInitDialog();在CChild.cpp中添加
void CChild::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CPassParametersDlg *pFatherDlg = (CPassParametersDlg*)GetParent();
ChildData = pFatherDlg->FarherData;
m_result2.SetWindowText(ChildData);
}
BOOL CChild::OnInitDialog()
{
CDialogEx::OnInitDialog();
CPassParametersDlg *pFatherDlg;
pFatherDlg = (CPassParametersDlg*)GetParent();
m_result2.SetWindowText(ChildData);
return TRUE;
}
void CChild::OnBnClickedButton2()
{
CPassParametersDlg * parent = (CPassParametersDlg*)GetParent();
//CString FarherData;
this->m_result2.GetWindowText(ChildData);//获取当前子窗口编辑框中的值
parent->m_result.SetWindowText(ChildData);//把当前编辑框中的值回传给父窗口
}4.在CChild.h中加入#include "PassParametersDlg.h"
在CPassParametersDlg.h中加入#include "Child.h"
5,运行效果如下图:
1.点击启动子对话框,并点击获得父窗口参数,结果:
2,在子窗口Edit中写入hello world,点击发送数据给父窗口,父窗口显示结果:
3.点击子窗口确定按钮,在父窗口中输入 new year,点击发送数据给子窗口,子窗口显示:
ps:此处需要关闭子窗口才能操作,如何不关闭直接操作父窗口待做。
二、思考
1.Edit如何显示字符?
m_result.SetWindowText(_T("火星人"));
2.如何将一个int整数显示在Edit上?
int a =20;
CString str;
str.Format(_T("%d"),a);
m_result.SetWindowText(str);
3.完整代码见:http://download.csdn.net/detail/huoxingrenhdh/9819228
相关文章推荐
- javascript实现html页面之间参数传递的四种方法实例分析
- javascript实现html页面之间参数传递的四种方法实例分析
- Delphi exe实例之间传递cmd参数
- Form之间传递参数或联动的范型代理实现
- 用Intent 对象实现两个(多个)Activity之间的跳转和参数传递
- 对话框之间的参数传递
- JavaScript实现页面之间传递参数的方法
- 用javascript实现html页面之间的参数传递
- 用javascript实现html页面之间的参数传递
- MFC中两个对话框之间数据传递
- 11. jsp与servlet之间页面跳转及参数传递实例
- 用javascript实现html页面之间的参数传递的四种方法
- javascript实现页面之间传递参数的方法
- MFC对话框之间传递数据
- MFC中对话框之间消息传递
- VC 两个对话框之间的参数传递(1)
- Delphi框架中怎样实现主程序与插件之间的互相调用以及参数传递
- 使用Intent对象来实现两个(或多个)Activity之间的跳转,并实现在Activity之间传递参数。
- MFC不同对话框之间的数据传递
- C#如何在对话框之间传递参数