Note:
As the example code provided by the original author requires the use of the RC resources, I feel that it is not general enough, so I will modify some source code and translate the article. The examples attached here are the interface section of the recently created screenshot program. Not only Office 2000 smart menu Oh! It also displays tabs and self-drawn menus in the Multi-document view. For details, see the attached instance code.
Compare the following two figures:
Use the Office 2000 menu in your project file, you can modify the CMultiDocTemplateEx, CMDIFrameEx, and CMDIFrameEx classes in the file to adapt to the single-document interface .), Follow these steps:
1. Add two strings to the string table in the resource: 1. ID_SHOWFULL, 32792. The full menu is displayed. Ii. ID_INTMODE, 32794. enable/disable smart menu mode.
2. include each. cpp and. h file in the OffMenu folder in your project file.
3. Edit MainFrm. h (header file of the main framework) in the project file ):
Declare the original class from:
Class CMainFrame: public CMDIFrameWnd
Changed:
# Include "MDIFrmEx. h"
# Include "ToolBarEx. h"
Class CMainFrame: public CMDIFrameEx
Then, change the toolbar class in the class from CToolBar to CToolbarEx:
CToolbar m_wndToolBar;
Variable declaration:
CToolbarEx m_wndToolBar;
4. Edit MainFrm. cpp in the project file (main framework file ):
Change the message ing from:
IMPLEMENT_DYNAMIC (CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP (CMainFrame, CMDIFrameWnd)
To:
IMPLEMENT_DYNAMIC (CMainFrame, CMDIFrameEx)
BEGIN_MESSAGE_MAP (CMainFrame, CMDIFrameEx)
In CMainFrame: OnCreate:
If (CMDIFrameWnd: OnCreate (lpCreateStruct) =-1)
Changed:
If (CMDIFrameEx: OnCreate (lpCreateStruct) =-1)
5. Edit MyApp. h (Application Class header file) in the project file ):
Change the class declaration from:
Class CTestMDIApp: public CWinApp
Changed:
# Include "WinAppEx. h"
Class CTestMDIApp: public CWinAppEx
6. Edit MyApp. cpp (Application Files) in the project file ):
Map the messages from:
BEGIN_MESSAGE_MAP (CTestMDIApp, CWinApp)
Changed:
# Include "DocTplEx. h"
BEGIN_MESSAGE_MAP (CTestMDIApp, CWinAppEx)
Next, run the following code in CMyApp: InitInstance () from:
CMultiDocTemplate * pDocTemplate;
PDocTemplate = new CMultiDocTemplate (
IDR_TESTMDTYPE,
RUNTIME_CLASS (cte1_didoc ),
// Custom MDI child frame
RUNTIME_CLASS (CChildFrame ),
RUNTIME_CLASS (cte1_diview ));
AddDocTemplate (pDocTemplate );
// Create main MDI Frame window
CMainFrame * pMainFrame = new CMainFrame;
If (! PMainFrame-> LoadFrame (IDR_MAINFRAME ))
Return FALSE;
M_pMainWnd = pMainFrame;
Replace:
UINT nID;
NID = IDR_MAINFRAME;
// Create main MDI Frame window
CMainFrame * pMainFrame = new CMainFrame;
If (! PMainFrame-> LoadFrame (nID ))
Return FALSE;
M_pMainWnd = pMainFrame;
CMultiDocTemplateEx * pDocTemplate;
PDocTemplate = new CMultiDocTemplateEx (
IDR_TESTMDTYPE,
RUNTIME_CLASS (cte1_didoc ),
// Custom MDI child frame
RUNTIME_CLASS (CChildFrame ),
RUNTIME_CLASS (cte1_diview ),
& NID, 1 );
AddDocTemplate (pDocTemplate );
CWinAppEx: InitInstance ();
7. Compile and run your program.
If you want to control the smart menu, you can directly load the following meanings in the CmenuBar class:
WORD CMenuBar: IncMenuUsage (CMenuEx * pParent,
UINT pos,
WORD curusage)
BOOL CMenuBar: EvaluateIfGrayed (CMenuEx * pParent,
UINT pos,
WORD & curusage,
Int iMed,
Int iRange)
The first function is used to add a useful method for the current menu bar. The second function determines whether the current menu bar is gray. It can also be used to modify the usage of the current menu bar. If you want to store the data defined in the application in the menu, you can use CMenuEx: GetExtraDataPtr (UINT) to include the data.