Windows NT Systems Programming: Spring 2000

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


Building a Dialog Box in Visual C++: A simple example

Description: Allow the user to type a title for the main window. To better understand the issues behind building dialog box, build this version by hand, avoiding the use of the resource editor and CDialog class. This is done to better understand the issues that determine what goes on behind the scenes in MFC, etc.

Step by step to building

rainbow.gif (2243 bytes)

// full source here
#define IDC_EDIT_TITLE   200hptrd_left.gif (955 bytes)
#define IDC_CHANGE 201
#define IDC_CLEAR 202
#define IDC_CANCEL 203

class CMsgFolderApp : public CWinApp
{
public:	virtual BOOL InitInstance(); // everybody overrides this one
};
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)
   // Handlers for Dialog Controls hptrd_left.gif (955 bytes)
   afx_msg void OnChange();
   afx_msg void OnClear();
   afx_msg void OnCancel();
   DECLARE_MESSAGE_MAP()   // set up message map handling
private:
   // Define the controls for the dialog
   // they won't be visible until you call "makeDialog"
   CButton m_btnChange; hptrd_left.gif (955 bytes)
   CButton m_btnClear;
   CButton m_btnCancel;
   CStatic m_labelEdit;       // label for selection
   CEdit m_editTitle;
   // helper functions for managing the dialog controls
   //***
   // Pass size and pointer to parent window
   // remember each control is a child window
   void makeDialog(CRect rect, CWnd* parent);
   void cleanUpDialog();
};

rainbow.gif (2243 bytes)

rainbow.gif (2243 bytes)

BEGIN_MESSAGE_MAP( CMainWindow, CWnd )
	ON_COMMAND(IDC_CHANGE, OnChange)hptrd_left.gif (955 bytes)
	ON_COMMAND(IDC_CLEAR, OnClear)
	ON_COMMAND(IDC_CANCEL, OnCancel)
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//MessageBoxhptrd_left.gif (955 bytes)("need code to open here","open"); // used for skeleton
	CRect rect;
	GetClientRect(rect);
	makeDialog(rect, this);hptrd_left.gif (955 bytes)
	
}

//*************** Handlers for Dialog Control Messages

//** comes here when user clicks "Change" button
// using a MessageBox ask user if really want change
// If yes, make change, clean up dialog
// If No, no change, clean up dialog
// If cancel, go back to dialog
void CMainWindow::OnChange()
{
	CString title, message;
	m_editTitle.GetWindowText(title);hptrd_left.gif (955 bytes)
	message = "Ok to change title to: \n " + title + "?";
	// use Message Box with Yes/No/Cancel buttons
	int result = MessageBoxhptrd_left.gif (955 bytes)(message,"Open",MB_YESNOCANCEL);
	if(result == IDYES) {
		SetWindowText(title);hptrd_left.gif (955 bytes)
		cleanUpDialog();hptrd_left.gif (955 bytes)
	}
	else if(result == IDNO) {
		MessageBox("Title Unchanged","no change"); // tell user no change
		cleanUpDialog();
	}
	else if(result == IDCANCEL) { // do nothing
		
	}
}

void CMainWindow::OnClear()
{
	m_editTitle.SetWindowText("");hptrd_left.gif (955 bytes)
	// the following line will also work
	//SetDlgItemText(IDC_EDIT_TITLE,"");hptrd_left.gif (955 bytes)
}
void CMainWindow::OnCancel()
{
	if(MessageBox("Are you sure you want to Cancel?", "Change Title Cancel",
		MB_YESNO) == IDYES) {
		cleanUpDialog();
	}
}


//*******
// create windows for all controls to make them visible
void CMainWindow::makeDialog(CRect rect, CWnd* parent)
{
	
	rect.right -= 100; // make room for buttons on right
	rect.bottom -= 100; // make room for selection on bottom
	m_labelEdit.Create("Type New Title:", WS_CHILD|WS_VISIBLE,
		CRect(CPoint(0,rect.bottom),CSize(200,25)),hptrd_left.gif (955 bytes)
		parent);
	m_editTitle.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,
		CRect(CPoint(0,rect.bottom+50),CSize(rect.right-110,25)),
		parent,IDC_EDIT_TITLE);
	m_btnChange.Create("Change", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON,
		CRect(CPoint(rect.right+5,0),CSize(75,30)),
		parent,IDC_CHANGE); hptrd_left.gif (955 bytes)
	m_btnClear.Create("Clear", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON,
		CRect(CPoint(rect.right+5,40),CSize(75,30)),
		parent,IDC_CLEAR);
	m_btnCancel.Create("Cancel", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON,
		CRect(CPoint(rect.right+5,80),CSize(75,30)),
		parent,IDC_CANCEL);
}
void CMainWindow::cleanUpDialog()
{
m_editTitle.DestroyWindow();hptrd_left.gif (955 bytes)
m_labelEdit.DestroyWindow();
m_btnChange.DestroyWindow();
m_btnClear.DestroyWindow();
m_btnCancel.DestroyWindow();

}
rainbow.gif (2243 bytes)

Full source code here.


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