Using MFC in C++ Part 1: A Basic Application Skeleton
Microsoft Foundation Classes allow C++ developers to write windows apps easier and faster. In this article, Mitch starts from scratch and digs deep into the MFC toolbox to provide you with all of the code and details to create a simple MFC based Win32 app.
Using MFC in C++ Part 1: A Basic Application Skeleton - Adding the code to MyClass.cpp (Page 4 of 7 )
Now that we have declared the two main classes of our application, we will want to create the implementation code for their constructors and functions. Double click on the MyClass.cpp file. Start by entering the follow code in its code window:
These are just simple include directives which reference our header file and another special MFC file, afxwin.h. afxwin.h contains all of the declarations and references for access to the MFC classes, including the CframeWnd and CwinApp classes from which we have derived our classes in MyClass.h. All MFC applications require the afxwin.h header file to be included. For now, just remember that the afxwin.h file contains all of the MFC classes which create windows and send and respond to messages.
Create(NULL, "My MFC Application");
This is the implementation code for the constructor of the CMainWin class, which is responsible for creating an instance of our application.
The Create() function is available to our app through the CframeWnd class which is what our CMainWin class is derived from. The create function takes 8 parameters, but we only need the first two. The second parameter, “My MFC Application” will simply set the title bar text in our application. That’s all we need for our CMainWin constructor. Let’s move on to the InitInstance() function of our CApp class.
m_pMainWnd = new CMainWin;
This might be the hardest function to get your head around. It’s the InitInstance function, takes no parameters and returns a BOOL value. In MFC, all of the simple data types such as bool, unsigned int and char have been typedef’d into upper case versions such as BOOL, UINT and LPCTSTR. This is to distinguish regular C++ code from MFC code.
A try catch block surrounds the entire function to grab any errors that the code in the try block might throw. The try block starts by setting the member pointer m_pMainWnd to a new instance of our CMainWin class. The m_pMainWnd variable is a pointer to a CWnd* object, which tells windows where the entry point into our application is.
Next, we call the show window member function of the m_pMainWnd object, which tells windows to display our dialog window. The paramater, m_nCmdShow, is an MFC defined value. Lastly, we call the UpdateWindow() function which tells windows to redraw our main dialog window. If all goes well, the function returns TRUE (Once again, TRUE is an MFC typedef’d value of the C++ variable true). A return value of TRUE is required to load our app successfully.
Now that we have all of the class declarations and implementation code out of the way, we will talk more about messages. Remember how we have the DECLARE_MESSAGE_MAP() macro declared in our CMainWin class in MyClass.h? Well, this macro tells windows that the CMainWin class (our main class) will send and respond to windows messages respectively.
To actually tell windows which messages our application can send and receive, we need to use the BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() macros in our MyClass.cpp file. Add the following code to MyClass.cpp. I will explain it in a minute:
These two simple lines are MFC defined macros. They represent several MFC functions (you can see this for yourself by finding the afxwin.h file on your system and looking at it in notepad) and take two parameters: The name of the Window we are working with and it’s base class. We are working with our main window, CMainWin, which is derived from CframeWnd. Between these two macros, we will put the macros for the messages that our application will respond to. But first, let’s create an instance of our app and make sure our application runs.
Add the following code at the end of the MyClass.cpp file:
This one simple line will create an instance of our CApp class, which in turn will bring everything together to display our app. We’ve created the bulk of our code, and can now see what our application looks like.