Windows NT Systems Programming

[ Home | Syllabus |Course Notes]


Converting from MsgEdit to Dialogs


Here is a description of what I did to convert the home grown message editor dialog window to use the CDialog Class.

 


Objectives


Current HomeGrown Solution

Questions

rainbow.gif (2243 bytes)
BOOL CMsgApp::InitInstance()
{
	m_pMainWnd = new CMainWindow();
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

// Window constructor
CMainWindow::CMainWindow()
{ 
	// Create the window
	Create(NULL, "Edit Message Demo", 
		WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
		CRect(0,0,500,700));
	MakeDlg(); // create edit control windows
}

Compare to OpenFolder Dialog

rainbow.gif (2243 bytes)

void CMainWindow::OnOpenFolder()
{
CString message;
CString folder;
CRect rect;

GetClientRect(&rect);
m_editSelection.GetWindowText(folder);
if(!msgFolderApp.msgFolder.OpenMsgFolder(folder))
MessageBox("Message Folder Corrupted!", "ERROR");
else {
SetWindowText("Message Folder Demo [" + folder + "]");
msgDlg.MakeDlg(rect, this, msgFolderApp.msgFolder.msgList);
}
cleanUpOpenFolder();


}


Compare Select Message Dialog

rainbow.gif (2243 bytes)


void CMsgDlg::OnOpenMsg()
{
	CString message;
	CString msgName;
	
	m_editSelection.GetWindowText(msgName);
	message = "You want to open " + msgName;
	MessageBox(message,"open Message");
	cleanUpOpenMsgDlg();


}

CDialog class

Let's rebuild the Message Editor Program using Dialog Boxes (CDialog class)


Resource Compiler

Let's build it the old-fashioned way.

Adding a dialog entry: (note units are DIALOGBOX units

rainbow.gif (2243 bytes)

OLD PROGRAM NEW PROGRAM
void CMainWindow::MakeDlg()
{
CRect rect; // used to get size of window

GetClientRect(&rect); // get size of window
// Create the window


// Create the labels
m_msgIDTitle.Create("Message ID",
WS_CHILD|WS_VISIBLE,
CRect(CPoint(0,0),CSize(150,24)),
this);
m_topicTitle.Create("Message Topic",
WS_CHILD|WS_VISIBLE,
CRect(CPoint(0,60),CSize(150,24)),
this);
m_textTitle.Create("Message Text",
WS_CHILD|WS_VISIBLE,
CRect(CPoint(0,120),CSize(150,24)),
this);

m_msgID.Create(
WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,
CRect(CPoint(50,30),CSize(440,24)),
this, IDC_MSGID);
m_msgID.LimitText(10);
// topic edit area
m_topic.Create(
WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,
CRect(CPoint(50,90),CSize(440,24)),
this, IDC_TOPIC);
m_topic.LimitText(SIZE_TOPIC);
// text edit area
m_text.Create( WS_CHILD|WS_VISIBLE|WS_BORDER|
                WS_TABSTOP|WS_VSCROLL|ES_MULTILINE,
CRect(CPoint(50,150),CSize(440,450)),
this, IDC_TEXT);
m_text.LimitText(SIZE_TEXT);

// Create buttons
m_save.Create("&Save",
      WS_CHILD|WS_VISIBLE|WS_TABSTOP
                |BS_PUSHBUTTON,
CRect(CPoint(0,600),CSize(100,50)),
this, IDC_SAVE);
m_load.Create("&Load",
WS_CHILD|WS_VISIBLE|WS_TABSTOP,
CRect(CPoint(200,600),CSize(100,50)),
this, IDC_LOAD);
m_clear.Create("&Clear",
WS_CHILD|WS_VISIBLE|WS_TABSTOP,
CRect(CPoint(400,600),CSize(100,50)),
this, IDC_CLEAR);
m_topic.SetFocus();
}
IDD_MSGDLG DIALOG 0, 0, 330, 198
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Edit Message"
BEGIN
    LTEXT            "Message &ID",IDC_ID_LABEL,7,16,38,8
    EDITTEXT         IDC_MSG_ID,18,25,70,14,ES_AUTOHSCROLL
    LTEXT            "&Topic",IDC_STATIC,7,48,19,8
    EDITTEXT         IDC_TOPIC,16,57,307,14,ES_AUTOHSCROLL
    LTEXT            "&Message Text",IDC_STATIC,7,75,44,8
    EDITTEXT         IDC_TEXT,16,87,307,78,ES_MULTILINE | ES_AUTOHSCROLL |
                    ES_WANTRETURN,WS_EX_ACCEPTFILES
    DEFPUSHBUTTON   "&Save",IDC_SAVE,23,177,50,14
    PUSHBUTTON       "&Load",IDC_LOAD,136,177,50,14
    PUSHBUTTON       "&Clear",IDC_CLEAR,242,177,50,14
END

rainbow.gif (2243 bytes)

 


Deriving CMsgDlg from CDialog

Here is what happens to the header files:

OLD PROGRAM (main.h) NEW PROGRAM (main.h and CMsgDlg.h)
const int IDC_TOPIC=100; // Topic edit object
const int IDC_TEXT=101; // Text edit object
const int IDC_MSGID=102; // MsgID edit Object
const int IDC_SAVE=103; // Save button
const int IDC_CLEAR=104; // Clear button
const int IDC_LOAD=105; // Load button
//...
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
~CMainWindow();
// Creates all the Message Edit Controls
void MakeDlg();

afx_msg void HandleSave();
afx_msg void HandleClear();
afx_msg void HandleLoad();
DECLARE_MESSAGE_MAP()

private:
// Here are all the edit objects
CEdit m_topic;
CEdit m_text;
CEdit m_msgID;
CStatic m_msgIDTitle;
CStatic m_topicTitle;
CStatic m_textTitle;
CButton m_save;
CButton m_load;
CButton m_clear;

};
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
~CMainWindow();


};

class CMsgDlg : public CDialog
{
public:
CMsgDlg(CWnd* pParentWnd = NULL) :
  CDialog(IDD_DIALOGBAR, pParentWnd) {}
  virtual BOOL OnInitDialog();

protected:
afx_msg void OnSave();
afx_msg void OnLoad();
afx_msg void OnClear();
DECLARE_MESSAGE_MAP()
};


CMsgDlg.cpp

OLD PROGRAM (main.cpp) NEW PROGRAM (CMsgDlg.cpp)
// Handle the save button
void CMainWindow::HandleSave()
// Saves the message
{
CString topic;
CString text;
CString fileName;
HANDLE msgFile;

m_topic.GetWindowText(topic);
msgApp.msg.SetTopic((LPCTSTR)topic);
m_text.GetWindowText(text);
msgApp.msg.SetText((LPCTSTR)text);

m_msgID.GetWindowText(fileName);
msgFile = CreateFile(fileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0);
if(msgFile == INVALID_HANDLE_VALUE) {
cerr << "Error: " << GetLastError() <<
" when creating file:" << fileName << endl;
ExitProcess(1);
}
if(!msgApp.msg.WriteMsg(msgFile)) {
cerr << " Error writing message msg1\n";
ExitProcess(1);
}
CloseHandle(msgFile);


}
// Handle the save button
void CMsgDlg::OnSave()
// Saves the message
{
CString topic;
CString text;
CString fileName;
HANDLE msgFile;

GetDlgItemText(IDC_TOPIC, topic);
msgApp.msg.SetTopic(topic);
GetDlgItemText(IDC_TEXT, text);
msgApp.msg.SetText(text);
GetDlgItemText(IDC_MSG_ID, fileName);

msgFile = CreateFile(fileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0);
if(msgFile == INVALID_HANDLE_VALUE) {
cerr << "Error: " << GetLastError() <<
" when creating file:" << fileName << endl;
ExitProcess(1);
}
if(!msgApp.msg.WriteMsg(msgFile)) {
cerr << " Error writing message msg1\n";
ExitProcess(1);
}
CloseHandle(msgFile);


}

Accessing Child Controls in a Dialog Box

CWnd::GetDlgItem

CWnd* GetDlgItem( int nID ) const;

void CWnd::GetDlgItem( int nID, HWND* phWnd ) const;


Return Value
A pointer to the given control or child window.
 
Parameters
nID   Specifies the identifier of the control or child window to be retrieved.
phWnd   A pointer to a child window.

 


Type-Safe Access

 

CEdit* pTopic = (CEdit*) GetDlgItem(IDC_TOPIC);
pTopic->
GetWindowText(topic);
GetDlgIteORmText(IDC_TOPIC, topic);

OR Could include these inline member functions in CMsgDlg

rainbow.gif (2243 bytes)

CEdit& getTopic ( ) { return *(CEdit*) GetDlgItem(IDC_TOPIC);}

rainbow.gif (2243 bytes)

Then can access using

rainbow.gif (2243 bytes)

getTopic().GetWindowText(topic);


First Failure

Putting the above together with the following in main.cpp didn't show anything.

BOOL CMsgApp::InitInstance()
{
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}

// Window constructor
CMainWindow::CMainWindow()
{
// Create the window
Create(NULL, "Edit Message Demo",
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CRect(0,0,500,700));
CMsgDlg dlg(this);
dlg.DoModal();
}


Another Failure

extern CMsgApp msgApp;  
// Message map - maps message IDs to their handlers
BEGIN_MESSAGE_MAP( CMsgDlg, CWnd )
	ON_COMMAND(IDC_SAVE, OnSave)
	ON_COMMAND(IDC_CLEAR, OnClear)
	ON_COMMAND(IDC_LOAD, OnLoad)
END_MESSAGE_MAP()

Copyright chris wild 1997.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: September 29, 1997.