Click here to Skip to main content
15,911,707 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: MFC dialog member sharing? (code included) Pin
BlackDice4-Mar-04 9:30
BlackDice4-Mar-04 9:30 
GeneralRe: MFC dialog member sharing? (code included) Pin
Christophocles4-Mar-04 10:27
sussChristophocles4-Mar-04 10:27 
GeneralRe: MFC dialog member sharing? (code included) Pin
BlackDice5-Mar-04 3:49
BlackDice5-Mar-04 3:49 
GeneralRe: MFC dialog member sharing? (or, My Marathon Ineptitude) Pin
Christophocles5-Mar-04 4:41
sussChristophocles5-Mar-04 4:41 
GeneralRe: MFC dialog member sharing? (or My Enduring Ineptitude) Pin
Christophocles5-Mar-04 6:25
sussChristophocles5-Mar-04 6:25 
GeneralRe: MFC dialog member sharing? (or My Enduring Ineptitude) Pin
BlackDice5-Mar-04 10:43
BlackDice5-Mar-04 10:43 
GeneralRe: MFC dialog member sharing? Pin
Christophocles5-Mar-04 12:01
sussChristophocles5-Mar-04 12:01 
GeneralRe: MFC dialog member sharing? Pin
Christophocles8-Mar-04 8:01
sussChristophocles8-Mar-04 8:01 
Here's your sample program, modified to function like mine. To recap -- from the embedded child dialog, I want Button2 to hide Button1 and to call "function()" (declared in CDlg1). In the Button2 message handler:
"m_pDlg1->funtion();" yields "Dlg2.cpp(53) : error C2039: 'funtion' : is not a member of 'CDlgtestDlg'" compile-time.
"m_pDlg1->m_button1.ShowWindow(SW_HIDE);" causes a "memory could not be read" run-time crash.

Here's the code, please take a look when you have time:

dlgtestDlg.cpp
// dlgtestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dlgtest.h"
#include "dlgtestDlg.h"
#include "dlg2.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()

/////////////////////////////////////////////////////////////////////////////
// CDlgtestDlg dialog

CDlgtestDlg::CDlgtestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgtestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgtestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDlgtestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgtestDlg)
	DDX_Control(pDX, IDC_BUTTON1, m_button1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlgtestDlg, CDialog)
	//{{AFX_MSG_MAP(CDlgtestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgtestDlg message handlers

BOOL CDlgtestDlg::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_dlg2.Create(CDlg2::IDD, this);
	CRect wRect; GetWindowRect(wRect);
	m_dlg2.SetWindowPos(GetDlgItem(IDD_DLGTEST_DIALOG), wRect.left, wRect.top, 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE);

	return TRUE; // return TRUE unless you set the focus to a control
}

void CDlgtestDlg::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 CDlgtestDlg::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 CDlgtestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDlgtestDlg::function() // My generic funtion
{
	AfxMessageBox("Generic function called.");
}

dlgtestDlg.h
// dlgtestDlg.h : header file
//

#if !defined(AFX_DLGTESTDLG_H__B42E61F1_E754_4EAD_B51A_EF1DC2AE9DA8__INCLUDED_)
#define AFX_DLGTESTDLG_H__B42E61F1_E754_4EAD_B51A_EF1DC2AE9DA8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CDlgtestDlg dialog

#include "Dlg2.h"

class CDlgtestDlg : public CDialog
{
// Construction
public:
	CDlgtestDlg(CWnd* pParent = NULL); // standard constructor

	CDlg2 m_dlg2; // Dialog 2
	void function(); // Generic function

	// Dialog Data
	//{{AFX_DATA(CDlgtestDlg)
	enum { IDD = IDD_DLGTEST_DIALOG };
	CButton	m_button1;
	//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDlgtestDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CDlgtestDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DLGTESTDLG_H__B42E61F1_E754_4EAD_B51A_EF1DC2AE9DA8__INCLUDED_)

Dlg2.cpp
// Dlg2.cpp : implementation file
//

#include "stdafx.h"
#include "dlgtest.h"
#include "Dlg2.h"

#include "DlgtestDlg.h" // Circular??

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlg2 dialog

CDlg2::CDlg2(CWnd* pParent /*=NULL*/)
	: CDialog(CDlg2::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlg2)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CDlg2::CDlg2(CDlgtestDlg* pDlg, CWnd* pParent /*=NULL*/)
	:CDialog(CDlg2::IDD,pParent)
{
	m_pDlg1 = 0; // Not necessary?
	m_pDlg1 = pDlg;
}

void CDlg2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlg2)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlg2, CDialog)
	//{{AFX_MSG_MAP(CDlg2)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlg2 message handlers

void CDlg2::OnButton2() 
{ // I want button2 to call function() & hide button1
	//m_pDlg1->funtion();                     // Doesn't compile
	//m_pDlg1->m_button1.ShowWindow(SW_HIDE); // Crashes
}

Dlg2.h
#if !defined(AFX_DLG2_H__11E9112B_9C78_4698_9C02_403F39EEE0FA__INCLUDED_)
#define AFX_DLG2_H__11E9112B_9C78_4698_9C02_403F39EEE0FA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Dlg2.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CDlg2 dialog
class CDlgtestDlg;
class CDlg2 : public CDialog
{
// Construction
public:
	CDlg2(CWnd* pParent = NULL); // standard constructor
	CDlg2(CDlgtestDlg* pDlg, CWnd* pParent = NULL); // standard constructor
	// Dialog Data
	//{{AFX_DATA(CDlg2)
	enum { IDD = IDD_DIALOG1 };
	// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA
	CDlgtestDlg* m_pDlg1;

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDlg2)
protected:
	virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:

	// Generated message map functions
	//{{AFX_MSG(CDlg2)
	afx_msg void OnButton2();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DLG2_H__11E9112B_9C78_4698_9C02_403F39EEE0FA__INCLUDED_)

GeneralAdvice required.... Pin
slyone3-Mar-04 10:54
slyone3-Mar-04 10:54 
GeneralRe: Advice required.... Pin
Roger Allen4-Mar-04 6:55
Roger Allen4-Mar-04 6:55 
GeneralRe: Advice required.... Pin
slyone4-Mar-04 12:02
slyone4-Mar-04 12:02 
GeneralUNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
cnd120013-Mar-04 10:36
cnd120013-Mar-04 10:36 
GeneralRe: UNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
Rick York3-Mar-04 11:09
mveRick York3-Mar-04 11:09 
GeneralRe: UNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
Tom Larsen3-Mar-04 11:23
Tom Larsen3-Mar-04 11:23 
GeneralRe: UNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
cnd120013-Mar-04 11:58
cnd120013-Mar-04 11:58 
GeneralRe: UNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
Christian Graus3-Mar-04 16:47
protectorChristian Graus3-Mar-04 16:47 
GeneralRe: UNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
Tom Larsen4-Mar-04 5:04
Tom Larsen4-Mar-04 5:04 
GeneralRe: UNWANTED DEBUG OF LIBRARY FUNCTIONS Pin
Jonas Larsson4-Mar-04 4:45
Jonas Larsson4-Mar-04 4:45 
GeneralAnti-Spam Project Pin
tempgp3-Mar-04 10:35
tempgp3-Mar-04 10:35 
GeneralRe: Anti-Spam Project Pin
Prakash Nadar3-Mar-04 13:31
Prakash Nadar3-Mar-04 13:31 
GeneralRe: Anti-Spam Project Pin
tempgp3-Mar-04 13:37
tempgp3-Mar-04 13:37 
GeneralRe: Anti-Spam Project Pin
David Crow4-Mar-04 7:23
David Crow4-Mar-04 7:23 
GeneralRe: Anti-Spam Project Pin
tempgp4-Mar-04 8:25
tempgp4-Mar-04 8:25 
GeneralRe: Anti-Spam Project Pin
David Crow4-Mar-04 8:30
David Crow4-Mar-04 8:30 
GeneralRe: Anti-Spam Project Pin
tempgp4-Mar-04 8:39
tempgp4-Mar-04 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.