Introduction To MFC: References
Introduction To MFC: References
Introduction To MFC: References
References:
“Introduction to MFC”, Deitel, Deitel, Nieto &
Strassberger, Prentice Hall © 2000
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
MFC Classes Intro to MFC 2
Only a small subset of the MFC class hierarchy will be covered herein.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
Messages Intro to MFC 3
GUI Events
GUI programs are event driven. Applications responds to user actions which
generates events, (mouse, keyboard, etc.). Events are communicated to a program
through messages sent by the Op Sys. Program processing characterized by an event-
loop which receives and responds to messages. MFC provides the necessary event-
loop logic in an object-oriented application framework.
Message Handling
A MFC application, (app) derives classes from the MFC hierarchy that contain code
to react to events (message handlers). Every message, (msg), is specified by a
unique int, (message identifier). Message handlers are associated with message
identifiers through a message map. The map registers handlers with the event-loop.
When the app receives a msg it looks up the identifier to find the corresponding
handler to execute for the appropriate response. The map is defined by the
DECLARE_MESSAGE_MAP() macro: Derived window class
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
MFC Resources Intro to MFC 4
Resource Definitions
MSVC++ supports a resource definition language, (RDL), for the specification of
GUI controls: (type, location, size, msg id, etc.).
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
Hungarian Notation Intro to MFC 5
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
MFC Project Intro to MFC 6
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
HelloWorld.h Intro to MFC 7
Code 1. //HelloWorld.h
2. class CHelloWindow : public CFrameWnd {
3. public:
4. CHelloWindow(); // constructor initializes window
5. ~CHelloWindow(); // destructor releases resources
6. private:
7. CStatic m_Hello; // contains Hello World string
8. };
9. //HelloWorld.cpp
10. // include application framework windows class definitions:
11. #include <afxwin.h> // application frameworks header
12. #include "HelloWorld.h" // class definition for application
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
HelloWorld.cpp (continued) Intro to MFC 9
29. CHelloWindow::~CHelloWindow()
30. {
31. }
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
Hello World Explanation Intro to MFC 10
Window Creation
Line 11. (#include <afxwin.h> // application frameworks header)
Includes standard MFC message Ids, handlers and map.
Lines 17-20:
17. Create( NULL, // default CFrameWnd class
18. "Hello", // window title
19. WS_OVERLAPPEDWINDOW, // full-featured window
20. CRect( 200, 100, 350, 200 ) ); // screen coordinates
Creates the main application window. The NULL argument instructs Windows to use
the default window properties for this window class. The WS_OVERLAPPEDWINDOW
setting creates a resizable window with standard window controls.The last Create()
argument instantiates a CRect(rectangle) object to store the window screen
coordinates. The first (x,y) pair gives the top-left coordinate and the last (x,y) pair
gives the lower-right coordinate. This defines a window that is 150 x 100 pixels,
(width, height).
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
Hello World Explanation (continued) Intro to MFC 11
CStatic creation
Lines 21-27: creates a CStatic object (used for text labels).
21. m_Hello.Create( // create Windows control
22. "Hello World", // text
23. WS_CHILD | WS_VISIBLE | WS_BORDER // window styles
24. | SS_CENTER, // static object styles
25. CRect( 20, 30, 120, 50 ), // window coordinates
26. this ); // context that owns child window
27. }
The first argument is the text label to be displayed. The second argument is mask to
set the CStatic window characteristics. It is formed by logically OR’ing together
pre-defined window style, (WS), constants: (WS_CHILD : sub-window; WS_VISIBLE :
viewable; WS_BORDER : rectangular border; SS_CENTER : center text).
Line 26: gives the owner (parent) argument for the CStatic subwindow, this
window is the CHelloWindow widow, (establishes an association between the sub-
window and parent window, which allows the MS Win OS to move the window in
memory).
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
Hello World Explanation (continued) Intro to MFC 12
All MFC app’s must have 1 (& only 1) instance of a class derived from CWinApp.
The CWinApp class controls application instantiation, execution (event loop), and
destruction. The main() is replaced is replaced by CWinApp. InitInstance()
instantiates the app main window object and begins execution. The <x>Window()
FNs above are inherited from CWinApp. MFC apps (must) use a pre-defined BOOL
(int) type with TRUE/FALSE constants instead of the standard C++ bool type. The
m_nCmdShow inherited variable indicates that the win is to be initially displayed
unmaximized and unminimized.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
Simple Menus App Intro to MFC 13
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinMenus.h Intro to MFC 14
Code
1. // WinMenus.h
2. // create menus with MFC
3.
4. class CMenusWin : public CFrameWnd {
5. public: Menu “callback” FNs.
6. CMenusWin(); Invoked when menu options
7. afx_msg void OnExit();
are selected.
8. afx_msg void OnCount();
9. afx_msg void OnShowCount();
10. private:
11. int m_iTotal; // count menu options selected
13. DECLARE_MESSAGE_MAP()
14. };
Invokes the message map
macro to map message IDs
to the message handler FNs.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinMenus.cpp Intro to MFC 15
15. // WinMenus.cpp
16. // create simple menus with MFC
17. #include <afxwin.h> // MFC application framework
18. #include <strstrea.h> // C-style string stream
19. #include <iomanip.h> // I/O manipulators
20. #include "WinMenusIDs.h" // application message ID symbols
21. #include "WinMenus.h"
26. m_iTotal = 0;
27. }
• The CRect constructor could have been replaced by the MFC pre-defined CRect
object rectDefault, to allow Windows to choose the initial size and placement.
• The second NULL argument indicates that this is a root window having no parent.
• The "Count" argument associates the menu defined in the resource file with the
window.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinMenus.cpp (continued) Intro to MFC 16
28. // afx_msg precedes each message handler function
29. afx_msg void CMenusWin::OnExit() Afx_msg is the MFC prefix used to
30. {
mark a msg handler. The WM_CLOSE
31. SendMessage( WM_CLOSE );
32. } msg terminates execution.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinMenus.cpp (continued) Intro to MFC 17
47. BEGIN_MESSAGE_MAP( CMenusWin, CFrameWnd )
48. Invokes msg map macro to
49. ON_COMMAND( IDM_EXIT, OnExit )
associate msg Ids with handler FNs.
50. ON_COMMAND_RANGE(IDM_M1O1, IDM_M2O3, OnCount)
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinMenusIDs.h Intro to MFC 18
Message Identifiers
Predefined MFC message identifiers are in the range: [0 … 1023]
Programmer-defined message identifiers are in the range: [1024 … 65535]
63. // WinMenusIDs.h
64. // define messages used by menus.cpp and menus.rc
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinMenus.rc Intro to MFC 19
72. // WinMenus.rc
73. // resource script for menus example
74. #include <afxres.h>
75. #include "WinMenusIDs.h" 93. POPUP "Show"
94. {
76. Count MENU 95. MENUITEM "Count", IDM_SHOW_COUNT
77. { 96. }
78. POPUP "File" 97. }
79. {
80. MENUITEM "Exit", IDM_EXIT
81. } WinMenus.rc resource file defines
the menu and options to msg Id
82. POPUP "Menu1"
83. {
associations. The lines are resource
84. MENUITEM "M1Opt1", IDM_M1O1 definition statements, (the MS Win
85. MENUITEM "M1Opt2", IDM_M1O2 GUI description language). Can be
86. } created in a text editor and added
87. POPUP "Menu2" to the project.
88. {
89. MENUITEM "M2Opt1", IDM_M2O1 Note: creating a resource file
90. MENUITEM "M2Opt2", IDM_M2O2
91. MENUITEM "M2Opt3", IDM_M2O3
within the MS VC IDE and
92. } opening it will invoke the
graphical resource editor which is
not covered in these notes.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
WinDialog App Intro to MFC 20
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
DayWeek_ids.h & DayWeek.h Intro to MFC 21
// DayWeek_ids.h
// Define Message Numbers
Defines the dialog controls message Ids.
#define IDC_DATE 2000
#define IDC_DAY 2001
#define IDC_WEEK 2002
#define IDC_CLEAR 2003
9. private:
10. int m_nDay, m_nMon, m_nYear; // Date
Dialog box button “callback”
11. DECLARE_MESSAGE_MAP() FNs. Invoked when buttons
12. }; are clicked.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
DayWeek.cpp Intro to MFC 22
1. // DayWeek.cpp
2. // Day of Week MFC dialog program GetDlgItem() returns base type
3. #include <afxwin.h> (CWnd*) pointers, which are type
4. #include <string> cast to the derived pointer type,
5. #include <sstream> (CEdit * ).
6. #include "DayWeek_ids.h"
7. #include "DayWeek.h"
GetWindowText() returns the
8. // clicked the "Day" button dialog edit box control text, (C-style
9. afx_msg void CDayWeek::OnDay() string), using the dialog control
10. {
pointer.
11. const int TEXT_SIZE = 11;
12. char tmp, szText[ TEXT_SIZE + 1 ]; // buffer for conversions
13.
14. // get addresses of Edit Box Controls
15. CEdit *pDate = ( CEdit * ) ( GetDlgItem( IDC_DATE ) );
16. CEdit *pWeek = ( CEdit * ) ( GetDlgItem( IDC_WEEK ) );
17.
18. pDate->GetWindowText( szText, TEXT_SIZE ); // get Date text
19. std::string DayOfWeek(szText); // init string
Lines 26-27: get the addresses of the edit dialog boxes for manipulation. The IDC_
codes, defined in DayWeek_ids.h, are passed to GetDlgItem() for the address
lookup.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
DayWeek.cpp (continued) Intro to MFC 23
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
DayWeek.cpp (continued) Intro to MFC 24
The GetDlgItem() FN must be re-called every time to manipulate the controls, (because the
OS reallocates memory every time Windows are created and destroyed).
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
DayWeek.cpp (continued) Intro to MFC 25
The GetDlgItem() FN must be re-called every time to manipulate the controls, (because the
OS reallocates memory every time Windows are created and destroyed).
Derives the app class from CWinApp and instantiates it. The dialog window is
instantiated and the DoModal() is invoked to display it as modal window, (modal
windows require the user to respond to them before anything else can be done).
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND
DayWeek.rc Intro to MFC 26
78. // DayWeek.rc
79. // resource script for DayWeek Dialog win location: upper left corner is at (50,50)
80. #include "afxres.h" pixels. The last 2 numbers give horizontal, vertical
81. #include "DayWeek_ids.h" dialog unit size. Horizontal units = 0.25 char
width, vertical = 0.125 char height.
82. DayWeek DIALOG 50, 50, 130, 130
83. STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
84. CAPTION "Day of Week" Dialog box title. Dialog styles prefixed with DS_.
85. {
86. LTEXT "Enter Date (MM/DD/YYYY):", IDC_STATIC, 30, 20, 98, 8
87. EDITTEXT IDC_DATE, 30, 30, 46, 16, ES_AUTOHSCROLL
Edit style allows horizontal
88. DEFPUSHBUTTON "Day", IDC_DAY, 50, 50, 30, 15
scrolling w/o scroll bar.
89. LTEXT "Week Day:", IDC_STATIC, 30, 70, 50, 8
90. EDITTEXT IDC_WEEK, 30, 80, 42, 16,
91. ES_READONLY | NOT WS_TABSTOP Defines a output
control w/o focus,
92. PUSHBUTTON "Clear", IDC_CLEAR, 50, 100, 30, 15, (when user hits tab key
93. NOT WS_TABSTOP
control is skipped).
94. }
IDC_STATIC controls do not generate msgs, (does not require control IDC_number). LTEXT is a
left aligned control, (RTEXT , CTEXT also available). WS_SYSMENU includes a system win menu
with Move & Close options (right-click title bar). WS_ POPUP wins are parentless.
Computer Science Dept Va Tech Dec. 2001 OO Software Design and Construction ©2001 Barnette, ND