Windows NT Systems Programming: Spring 2000

[ Home | Syllabus | Course Notes | Assignments | Search]


Simple Dialog Using the Dialog Editor

Objective: demonstrate building a simple dialog using the dialog editor but not the class wizard.
This solution solves the problem with sharing the main window to show the dialog controls.

Steps:

dialog.gif (2988 bytes)

rainbow.gif (2243 bytes)
#include "resource.h"hptrd_left.gif (955 bytes)

class CTitleDlg : public CDialog  hptrd_left.gif (955 bytes)
{
public:
	CTitleDlg(CWnd* pParentWnd = NULL) :hptrd_left.gif (955 bytes)
	  CDialog(IDD_DIALOG1hptrd_left.gif (955 bytes), pParentWnd) {}
	virtual BOOL OnInitDialog();
	CString m_title;hptrd_left.gif (955 bytes)
protected:
	virtual void OnOK();hptrd_left.gif (955 bytes)

	DECLARE_MESSAGE_MAP()	// set up message map handling
	
};
rainbow.gif (2243 bytes)

Now look at the implementation for the above

rainbow.gif (2243 bytes)

BOOL CTitleDlg::OnInitDialog( )
{
	CDialog::OnInitDialog( );hptrd_left.gif (955 bytes)
	return TRUE; // says init was OK
}
void CTitleDlg::OnOK()
{	
	((CEdit*)GetDlgItem(IDC_EDIT1))->GetWindowText(m_title);hptrd_left.gif (955 bytes)
	//GetParent()->SetWindowText(m_title);hptrd_left.gif (955 bytes)
	CDialog::OnOK();	// clean up and return successfullyhptrd_left.gif (955 bytes)
}
rainbow.gif (2243 bytes)

Now let's create the application and main window, here is main.h

rainbow.gif (2243 bytes)
/
class CMainWindow : public CFrameWnd
{ 
public:
	CMainWindow(); 
	~CMainWindow();
	
protected:
	// Handlers for MainFrame when not in dialog mode
	afx_msg void OnLButtonDown(UINT, CPoint);hptrd_left.gif (955 bytes)
	
	DECLARE_MESSAGE_MAP()   // set up message map handling
		
private:
};
rainbow.gif (2243 bytes)

Now look at main.cpp

rainbow.gif (2243 bytes)
BEGIN_MESSAGE_MAP( CMainWindow, CWnd )
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

// Window constructor
CMainWindow::CMainWindow()
{ 
	Create(NULL, "Default Title - Left Button to Change");
}

CMainWindow::~CMainWindow()
{
}

// create a dialog box and wait for user input
void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CTitleDlg myDlg;

	if(myDlg.DoModal( ) hptrd_left.gif (955 bytes)== IDOK) {
		SetWindowText(myDlg.m_title);hptrd_left.gif (955 bytes)
	}

}

rainbow.gif (2243 bytes)

Copyright chris wild 1999/2000.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: January 25, 2000.