主对话框中有一个combobox控件,想在弹出对话框中的editbox中输入字符串写入控件下拉列表,如何实现
基于MFC的程序,主要问题就是如何在弹出子对话框中调用父对话框中的combobox控件的addstring方法...
基于MFC的程序,主要问题就是如何在弹出子对话框中调用父对话框中的combobox控件的addstring方法
展开
3个回答
展开全部
将父对话框的类指针传给弹出的对话框过去,然后用父对话框指针控制父对话框就可以了.
主对话框类为: CTest1Dlg
弹出对话框类为: CDary
主对话框里面加 combobox 控件,相关control变量为: m_BoxControl
主对话框里面加一个button, 里面写:
void CTest1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CDary test;
test.testDlg = this;
test.DoModal();
}
弹出对话框类的头文件里面包含下主对话框的头文件,并加个主类的指针变量:
public:
CTest1Dlg* testDlg;
弹出对话框类里面加个button, 代码写成
void CDary::OnButton1()
{
// TODO: Add your control notification handler code here
testDlg->m_BoxControl.AddString("daryxiong");
}
/////////////////////////////////////////////
主对话框头文件:
// test1Dlg.h : header file
//
#if !defined(AFX_TEST1DLG_H__1AA3DA4E_67CA_4095_86DF_49A30BC9653C__INCLUDED_)
#define AFX_TEST1DLG_H__1AA3DA4E_67CA_4095_86DF_49A30BC9653C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog
class CTest1Dlg : public CDialog
{
// Construction
public:
CTest1Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CTest1Dlg)
enum { IDD = IDD_TEST1_DIALOG };
CComboBox m_BoxControl;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTest1Dlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CTest1Dlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_TEST1DLG_H__1AA3DA4E_67CA_4095_86DF_49A30BC9653C__INCLUDED_)
主对话框CPP文件:
// test1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test1.h"
#include "test1Dlg.h"
#include "Dary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog
CTest1Dlg::CTest1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest1Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest1Dlg)
DDX_Control(pDX, IDC_COMBO1, m_BoxControl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest1Dlg, CDialog)
//{{AFX_MSG_MAP(CTest1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg message handlers
BOOL CTest1Dlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTest1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTest1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTest1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CDary test;
test.testDlg = this;
test.DoModal();
}
弹出对话框头文件:
#if !defined(AFX_DARY_H__EC699B44_E662_4D1B_9722_99E4EBB337FE__INCLUDED_)
#define AFX_DARY_H__EC699B44_E662_4D1B_9722_99E4EBB337FE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Dary.h : header file
//
#include "test1Dlg.h"
/////////////////////////////////////////////////////////////////////////////
// CDary dialog
class CDary : public CDialog
{
// Construction
public:
CDary(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CDary)
enum { IDD = IDD_DIALOG1 };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDary)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
public:
CTest1Dlg* testDlg;
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CDary)
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_DARY_H__EC699B44_E662_4D1B_9722_99E4EBB337FE__INCLUDED_)
弹出对话框CPP文件
// Dary.cpp : implementation file
//
#include "stdafx.h"
#include "test1.h"
#include "Dary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDary dialog
CDary::CDary(CWnd* pParent /*=NULL*/)
: CDialog(CDary::IDD, pParent)
{
//{{AFX_DATA_INIT(CDary)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDary::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDary)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDary, CDialog)
//{{AFX_MSG_MAP(CDary)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDary message handlers
void CDary::OnButton1()
{
// TODO: Add your control notification handler code here
testDlg->m_BoxControl.AddString("daryxiong");
}
这样还不行的话偶也就没办法了.
主对话框类为: CTest1Dlg
弹出对话框类为: CDary
主对话框里面加 combobox 控件,相关control变量为: m_BoxControl
主对话框里面加一个button, 里面写:
void CTest1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CDary test;
test.testDlg = this;
test.DoModal();
}
弹出对话框类的头文件里面包含下主对话框的头文件,并加个主类的指针变量:
public:
CTest1Dlg* testDlg;
弹出对话框类里面加个button, 代码写成
void CDary::OnButton1()
{
// TODO: Add your control notification handler code here
testDlg->m_BoxControl.AddString("daryxiong");
}
/////////////////////////////////////////////
主对话框头文件:
// test1Dlg.h : header file
//
#if !defined(AFX_TEST1DLG_H__1AA3DA4E_67CA_4095_86DF_49A30BC9653C__INCLUDED_)
#define AFX_TEST1DLG_H__1AA3DA4E_67CA_4095_86DF_49A30BC9653C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog
class CTest1Dlg : public CDialog
{
// Construction
public:
CTest1Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CTest1Dlg)
enum { IDD = IDD_TEST1_DIALOG };
CComboBox m_BoxControl;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTest1Dlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CTest1Dlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_TEST1DLG_H__1AA3DA4E_67CA_4095_86DF_49A30BC9653C__INCLUDED_)
主对话框CPP文件:
// test1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test1.h"
#include "test1Dlg.h"
#include "Dary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog
CTest1Dlg::CTest1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest1Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest1Dlg)
DDX_Control(pDX, IDC_COMBO1, m_BoxControl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest1Dlg, CDialog)
//{{AFX_MSG_MAP(CTest1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg message handlers
BOOL CTest1Dlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTest1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTest1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTest1Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CDary test;
test.testDlg = this;
test.DoModal();
}
弹出对话框头文件:
#if !defined(AFX_DARY_H__EC699B44_E662_4D1B_9722_99E4EBB337FE__INCLUDED_)
#define AFX_DARY_H__EC699B44_E662_4D1B_9722_99E4EBB337FE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Dary.h : header file
//
#include "test1Dlg.h"
/////////////////////////////////////////////////////////////////////////////
// CDary dialog
class CDary : public CDialog
{
// Construction
public:
CDary(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CDary)
enum { IDD = IDD_DIALOG1 };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDary)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
public:
CTest1Dlg* testDlg;
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CDary)
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_DARY_H__EC699B44_E662_4D1B_9722_99E4EBB337FE__INCLUDED_)
弹出对话框CPP文件
// Dary.cpp : implementation file
//
#include "stdafx.h"
#include "test1.h"
#include "Dary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDary dialog
CDary::CDary(CWnd* pParent /*=NULL*/)
: CDialog(CDary::IDD, pParent)
{
//{{AFX_DATA_INIT(CDary)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDary::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDary)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDary, CDialog)
//{{AFX_MSG_MAP(CDary)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDary message handlers
void CDary::OnButton1()
{
// TODO: Add your control notification handler code here
testDlg->m_BoxControl.AddString("daryxiong");
}
这样还不行的话偶也就没办法了.
展开全部
楼上写得太长了。我觉得也是这样做的,
创建一个弹出对话框后,将主对话框类的指针传给子对话框,可以通过构造函数传,然后在子对话框创建一个父对话框的类的指针,这个指针指向构造函数传下来的指针,这样可以对父对话框进行操作。
创建一个弹出对话框后,将主对话框类的指针传给子对话框,可以通过构造函数传,然后在子对话框创建一个父对话框的类的指针,这个指针指向构造函数传下来的指针,这样可以对父对话框进行操作。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在子对话框中为editbox定义一个CString变量,public的
父对话框中:
子对话框 dlg;
if( dlg.Domodal == IDOK)
下拉框.AddString( dlg.变量名);
父对话框中:
子对话框 dlg;
if( dlg.Domodal == IDOK)
下拉框.AddString( dlg.变量名);
追问
怎么才能实现下次启动程序时下拉框可以显示本次添加的内容呢,能否不用读写文件的方式实现
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询