[ Home | Syllabus | Course Notes | Assignments | Search]
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
![]()
// full source here #define IDC_EDIT_TITLE 200#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);
// Handlers for Dialog Controls
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;
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(); };
![]()
BEGIN_MESSAGE_MAP( CMainWindow, CWnd ) ON_COMMAND(IDC_CHANGE, OnChange)ON_COMMAND(IDC_CLEAR, OnClear) ON_COMMAND(IDC_CANCEL, OnCancel) ON_WM_LBUTTONDOWN() END_MESSAGE_MAP() void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) { //MessageBox
("need code to open here","open"); // used for skeleton CRect rect; GetClientRect(rect); makeDialog(rect, this);
} //*************** 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);
message = "Ok to change title to: \n " + title + "?"; // use Message Box with Yes/No/Cancel buttons int result = MessageBox
(message,"Open",MB_YESNOCANCEL); if(result == IDYES) { SetWindowText(title);
cleanUpDialog();
} 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("");
// the following line will also work //SetDlgItemText(IDC_EDIT_TITLE,"");
}
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)),
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);
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();
m_labelEdit.DestroyWindow();
m_btnChange.DestroyWindow();
m_btnClear.DestroyWindow();
m_btnCancel.DestroyWindow();
}
