Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 5: The Document/View Architecture Visual C++ and MFC Fundamentals


};

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.
Free download pdf