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:
- Start visual c++
- File/New/(Projects)/Win32Application: we'll add MFC as needed by hand
- After the project is built, change the settings to include MFC. Select
"Project/Settings"
- In the "General" tab set "Microsoft Foundation Classes" to
"Use MFC in a shared DLL"
- Create a dialog box using the dialog editor (Select
"Insert/Resource/DialogBox")
- Add a Static Text and an Edit Control, edit the Static Text Control so that the
dialog box looks like this

- Create a header file for the dialog object called "titleDlg.h" add the
following code
#include "resource.h"
class CTitleDlg : public CDialog
{
public:
CTitleDlg(CWnd* pParentWnd = NULL) :
CDialog(IDD_DIALOG1
, pParentWnd) {}
virtual BOOL OnInitDialog();
CString m_title;
protected:
virtual void OnOK();
DECLARE_MESSAGE_MAP() // set up message map handling
};

- "resource.h" is built by the dialog editor and contains the symbolic
constants for the controls it built.
- Derived from "CDialog" class
- Identify parent of this dialog (if any) and use initializer constructor to build
base class members first
- IDD_DIALOG1 is the ID assigned by the dialog editor to the dialog it built.
The constructor for CDialog will use this ID to find its definition as built by the
resource compiler.
This is what matches this class with the dialog box created in the visual editor.
- Remember the title the user typed here so that the main window can get at it
- When the user clicks OK, it dialog box receives an OnOK message - this is the
handler for it.
Now look at the implementation for the above
BOOL CTitleDlg::OnInitDialog( )
{
CDialog::OnInitDialog( );
return TRUE; // says init was OK
}
void CTitleDlg::OnOK()
{
((CEdit*)GetDlgItem(IDC_EDIT1))->GetWindowText(m_title);
//GetParent()->SetWindowText(m_title);
CDialog::OnOK(); // clean up and return successfully
}
- The base class's function does all the work
- "GetDlgItem" takes the ID of the edit control (IDC_EDIT1) set by the
dialog editor and returns a pointer to the CEdit object that it created.
Then you can call the "GetWindowText" member function of CEdit (inherited
from CWnd) and retrieve the text the user typed and store that in the CString data member
of this object (see titleDlg.h)
- This commented out line of code would set the window title of the parent window
directly - but that would limit the value of this object which could return a string for
any purpose not just changing the title.
I put it here to demonstrate "GetParent" and to comment on design decisions
- The base class's "OnOk" will remove the dialog box and return control
after a "DoModal" function call (seen later on)
Now let's create the application and main window, here is main.h
/
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
~CMainWindow();
protected:
// Handlers for MainFrame when not in dialog mode
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP() // set up message map handling
private:
};
- User presses left mouse button to display dialog box
- It would be interesting to add a menu entry to do this instead (as seen here)
Now look at main.cpp
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( )
== IDOK) {
SetWindowText(myDlg.m_title);
}
}

- "DoModal" displays the dialog box and waits until the users is done
If the user clicks the OK button, returns the value IDOK so you can test if action
should be taken
- Sets the title of the main window (this window) to CString saved in CTitleDlg
object
- (click here for an already created project.)
Copyright chris wild 1999/2000.
For problems or questions regarding this web contact [Dr.
Wild].
Last updated: January 25, 2000.