在菜单栏显示出最近打开的文件的方法
2006-03-27 23:09
369 查看
SDI Improvements |
SDI Improvements: The 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 |
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.
![]() Practical Learning: Improving the Application |
Add the following menu item under File:
![](http://www.functionx.com/MFCFundamentals/lessons/images/menudes7.gif)
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 |
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; } |
相关文章推荐
- 禁止Win8的Start界面显示最近打开文件的方法
- mysql导出csv文件excel打开后数字用科学计数法显示且低位变0的解决方法
- ubuntu 中用vim打开txt等文件中文显示乱码的解决方法
- 关于CPP文件损坏打开显示二进制数字的解决方法
- Win7 64位系统打开 chm 文件右侧窗口显示空白的解决方法
- 删除文件时候显示“文件已在资源管理器中打开”快速解决方法
- iis打开aspx文件时显示无法找到该页问题解决方法
- 取消开始菜单文档记录,不显示最近使用的文件近期打开过的文件
- vs visual studio 2015中如何在当前的项目管理器中打开文件?如何打开指定的方法名称,如何显示到指定的方法的那个位置
- chm文件无法打开的解决方法,chm电子书显示“此程序无法显示网页”的解决办法
- chm文件无法打开的解决方法,chm电子书显示“此程序无法显示网页”的解决办法
- C# vs 不显示最近打开文件项目
- xcode 最近打开文件列表显示为空或不显示最近打开的项目或(no recent projects)解决办法
- chm文件打开后“无法显示网页”解决方法
- chm文件打开后,只能看到目录,不显示内容解决方法
- VC打开并缩放显示位图(.BMP)文件的方法
- CHM文件打开显示乱码的解决方法
- chm文件无法打开的解决方法,chm电子书显示“此程序无法显示网页”的解决办法
- mysql导出csv文件excel打开后数字用科学计数法显示且低位变0的解决方法
- ubuntu 10.04 vi 与 gedit 打开文件显示中文乱码的解决方法