MFC: Application & FrameWnd Classes
Application & FrameWnd Classes
This MFC video tutorial discusses the Application and FrameWnd classes, which are part of the MFC Document/View architecture. To follow along with this video and article, download the code from our MFC Lesson 1 support page.
The Frame Window of an MFC application represents the main window of your program and includes the system buttons (min, max, and close) and menu, the program's toolbars, status bar, and menus. In your program, the Frame Window is the class derived from CFrameWnd. Open the MainFrm.cpp file. Briefly, the PreCreateWindow() and OnCreate() functions allow you to add/change/remove the menus, toolbars, and status bar of the application. We will cover these items more in depth in another lesson.
The Application class in an MFC program, derived from CWinApp, represents the application thread of execution. For our purpose here, the most important function in this class is InitInstance(). Open up the Lesson1.cpp file in our MFC Lesson 1 solution. Here, you can add variables and objects that you'd like to have initialized before the main window of your program appears.
If you let the MFC AppWizard generate a basic MFC program for you, you will see that there are a few important things to note in InitInstance(). First, this line:
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
sets a registry key. You can change this string to your company or group name and then use this branch in the registry to store data from any of your applications in the future.
CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CLesson1Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CLesson1View)); if (!pDocTemplate) return FALSE; AddDocTemplate(pDocTemplate);
This block of code creates a CSingleDocTemplate object and adds it to the application. This is a critical step because here the View, Document, and Frame Window are tied together for this application. Hereafter, when the Document requests the View or vice versa, the CSingleDocTemplate will return the correct object according to what was passed into its constructor here.
If you pass in command-line parameters, the InitInstance() function is where they will get parsed. You can pass in generic things like file names for this application to open, etc.
Finally, this function makes a call to show and update the main window of the program:
Now, the user can begin interacting with your application.