SDI Improvements |
SDI Improvements: The Application |
To make your programming experience a little faster and efficient, the framework provides many other features for each class used in an application.
The Application: The programs we will create in this book use classes of the Microsoft Foundation Classes (MFC) library. MFC classes are created is various libraries called DLLs. In order to use MFC objects in your application as opposed to non-MFC objects, you must let the compiler know. This is done by specifying that you want to Use MFC In A Shared DLL, as we have done so far. Additionally, if you want your windows to have a 3-D appearance, call the Enable3dControls() method. If you do not want the 3-D appearance, call the Enable3dControlsStatic() method. The best way to deal with this is to ask the compiler to check if you had allowed using MFC in a shared DLL or not, and then tell the compiler which of these two functions to execute. This is done using a #ifdef preprocessor in your InitInstance() method. Here is an example:
#include <afxwin.h> class CSimpleFrame : public CFrameWnd { public: CSimpleFrame() { // Create the window's frame Create(NULL, "Windows Application"); } }; class CSimpleApp : public CWinApp { public: BOOL InitInstance(); }; BOOL CSimpleApp::InitInstance() { #ifdef _AFXDLL Enable3dControls( ); #else Enable3dControlsStatic(); #endif CSimpleFrame *Tester = new CSimpleFrame (); m_pMainWnd = Tester; Tester->ShowWindow(SW_SHOW); Tester->UpdateWindow(); return TRUE; } CSimpleApp theApp;
To provide your application the ability to create a new document, the CWinApp class provides the OnFileNew() method. Its syntax is:
afx_msg void OnFileNew();
To use this method, create a menu item identified as ID_FILE_NEW. You should also create a prompt for it so the menu item can be added to the string table. This menu item is traditionally and obviously added to the File menu. After creating this menu item, in the message table of the application's source, invoke the CWinApp::OnFileNew() method using the ON_COMMAND() macro. This can be done as follows:
BEGIN_MESSAGE_MAP(CExoApp, CWinApp) ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) END_MESSAGE_MAP()
CWinApp also provides an application the ability to easily open a document. This is done using the OnFileOpen() method. In the same way, it can help with printing a document. Here is a summary:
Menu ID | CWinApp Message Map | |
ID_FILE_NEW | ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) | |
ID_FILE_OPEN | ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) | |
ID_FILE_PRINT_SETUP | ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup |
One of the last minute assignment you may need to perform when the user is closing an application is to check if the displayed document is "dirty", that is, if the document has been changed since it was last accessed. To help with this, simply create a menu item identified as ID_APP_EXIT and set a caption accordingly, such as the Exit menu we created in the previous Practical Learning section. It is always helpful to add a prompt to a menu item.
These command messages are implemented in the CWinApp class and can be helpful for your application. If their behavior does not fulfill your goal, you can write your own intended implementation of these menu items.
When using an application over and over, sometimes a user may want to open the last accessed document or at least see a list of the last documents opened on an application. To provide this functionality, create a menu item called ID_FILE_MRU_FILE1 and set its prompt to a string such as Recent File. This menu item is usually added to the File menu above the Exit or quit. The actual list of recent files is stored in an INI file that accompanies your application. To make this list available, you must call the LoadStdProfileSettings() method of the CWinApp class in your InitInstance() method. The syntax of this method is:
void LoadStdProfileSettings(UINT nMaxMRU = _AFX_MRU_COUNT);
By default, this allows the list to display up to four names of documents. This method takes one argument as the number of document names to be displayed in the list. If you do not want the default of 4, specify the nMaxMRU value to your liking.
|
- To provide new functionality to the application, in the Resource View, change the IDentifier of the Exit menu item to ID_APP_EXIT and set its Prompt to Quit the application
- Add the following menu item under File:
Caption ID Prompt &New/tCtrl+N ID_FILE_NEW Create a new document &Open.../tCtrl+O ID_FILE_OPEN Open an existing document - P&rint Setup... ID_FILE_PRINT_SETUP Change the printer and printing options - Recent file ID_FILE_MRU_FILE1 Open this file - E&xit ID_APP_EXIT Quit the application; prompt the save document - To allow the application to treat documents, change the InitInstance() implementation as follows:
BEGIN_MESSAGE_MAP(CExerciseApp, CWinApp) ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP() BOOL CExerciseApp::InitInstance() { #ifdef _AFXDLL Enable3dControls( ); #else Enable3dControlsStatic(); #endif LoadStdProfileSettings(6); CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CExerciseDoc), RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CExerciseView)); AddDocTemplate(pDocTemplate); CCommandLineInfo cmdInfo; // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; m_pMainWnd->ShowWindow(m_nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; }
- Test the application and click the various menu items