![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
To implement submenus, add a new menu item to the Choices menu, Submenus, and double-click that item to open its properties page, as shown in Figure 4.4. Click the box marked Popup, as shown in Figure 4.4, and close the property page. This adds a right-pointing arrow to the Submenus menu item in the Menu Editor. Select that item to open the new submenu in the Menu Editor, as shown in Figure 4.5. Add three new items to the submenuItem 1, Item 2, and Item 3using the Menu Editor. Double-click each item and give them the IDs ID_SUBITEM1, ID_SUBITEM2, and ID_SUBITEM3. Next, right-click each new menu item and add a handler function to each with ClassWizard.
void CMenusView::OnSubitem1() { // TODO: Add your command handler code here }
void CMenusView::OnSubitem2() { // TODO: Add your command handler code here } void CMenusView::OnSubitem3() { // TODO: Add your command handler code here } We dont need the submenu items do anything spectacular. We might just have them sound a beep, using MessageBeep(). void CMenusView::OnSubitem1() { MessageBeep(-1); ⇐ } void CMenusView::OnSubitem2() { MessageBeep(-1); ⇐ } void CMenusView::OnSubitem3() { MessageBeep(-1); ⇐ } Run the program, as shown in Figure 4.6. As you can see, our program now supports submenus. When you click a submenu item, the program beeps, as weve set it up to do. Our program is working as we designed it, and were making progress with menu programming. Weve supported both check mark menu items and submenus. Next, we learn to disable menu items. Disabling Menu ItemsWe need to add one more item to the Choices menu: Disable Me.
------------------------------------------------------ |File Edit View Choices Draw Help| |------------------ | --------- ----------------------| | | Check Me | | | | Submenus | | | |------------- | | | | Disable Me | | | ------------- | | | | | | | | | ------------------------------------------------------ Once the user selects this item, we disable it, making it appear gray. When disabled, the user can no longer select this menu item. Using Menu SeparatorsBefore adding the Disable Me item, we might add a menu separator to separate this item from the other items in this menu. To do that, just add a new item to the bottom of the Choices menu, double-click it to open its property page, then click the box marked Separator in that page. This creates a menu separator. Such separators are used to group menu items together logically in a menu, and you see them in most menus. Add the Disable Me menu item to the bottom of the Choices menu and connect both the COMMAND and UPDATE_COMMAND_UI messages to functions in the CMenusView class. void CMenusView::OnDisable() { // TODO: Add your command handler code here } void CMenusView::OnUpdateDisable(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here } Just as with the check mark item, we set up a new flag, m_enabled, which we set when the user selects the Disable Me item. void CMenusView::OnDisable() { m_enabled = !m_enabled; ⇐ } Then, when the program is about to display the Choices menu, we use the Enable() function to enable or disable the Disable Me menu item. void CMenusView::OnUpdateDisable(CCmdUI* pCmdUI) { pCmdUI->Enable(m_enabled); ⇐ } Open the program and select the Disable Me item. When you open the menu again, that item is disabled, as also shown in Figure 4.7. Now were able to enable and disable menu items.
Before leaving conventional menu handling, we should note one last thing: how to use menu accelerators. Using Menu AcceleratorsMenu accelerators are special keys that the user can type at any time to select the corresponding menu item. They differ from shortcut keys, which can only be used when the target item is visible (that is, its menu is open). To see how this works, we can add a menu accelerator to the Disable Me item. To add an accelerator key, open the Accelerator Editor, as shown in Figure 4.8, and press the Ins key to insert a new blank accelerator. Double-click the new accelerator to open its property page. We can make the accelerator for this item Ctrl+D; to do that, enter D in the Key box in the property page and click the box marked Ctrl. Finally, close the property page. To indicate that this is the accelerator key for this menu item, we also change the Disable Me items caption from Disable Me to Disable Me\tCtrl+D (the \t is a Tab character and spaces the accelerator key apart from the rest of the caption). This new caption appears in Figure 4.8.
We continue with more advanced material, including using bitmapped menu items and changing menus on the fly. Bitmapped Menu ItemsIn this next example, we use a new menu, Draw, which has one item in it: Circle. ------------------------------------------------------- |File Edit View Choices Draw Help| |--------------------------- | ------- --------------| | | Circle | | | ------------- | | | | | | | | | | | | | ------------------------------------------------------ When the user selects that item, the program draws a circle. It then modifies the Circle item in the Draw menu, replacing it with a bitmap showing a small box. From then on, selecting that menu item displays a box, not a circle. We start this new example by adding two new flags: m_box, which is true if were supposed to draw a box, and m_circle, if were supposed to draw a circle. We draw those figures in OnDraw() as follows (we check both flags, because there are timesfor example, when the program first startswhen were not supposed to draw any figure at all): { CMenusDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if(m_circle){ ⇐ pDC->Ellipse(100, 100, 200, 200); ⇐ } ⇐ else{ ⇐ if(m_box){ ⇐ pDC->Rectangle(100, 100, 200, 200); ⇐ } ⇐ } ⇐ } Add the new item to the Draw menu in the program, giving it the ID ID_FIGURE and the caption Circle and connecting it with ClassWizard to a view class function named OnFigure(). void CMenusView::OnFigure() { // TODO: Add your command handler code here }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |