Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



Table 4.1 AppendMenu() Parameters

Parameter Does this
MF_CHECKED Acts as a toggle with MF_UNCHECKED to place the default check mark next to the item.
MF_UNCHECKED Acts as a toggle with MF_CHECKED to remove a check mark next to the item.
MF_DISABLED Disables the menu item so that it cannot be selected but does not dim it.
MF_ENABLED Enables the menu item so that it can be selected and restores it from its dimmed state.
MF_GRAYED Disables the menu item so that it cannot be selected and dims it.
MF_MENUBARBREAK Places the item on a new line in static menus or in a new column in popup menus.
MF_MENUBREAK Places the item on a new line in static menus or in a new column in popup menus.
MF_OWNERDRAW Specifies that the item is an owner-draw item.
MF_POPUP Specifies that the menu item has a popup menu associated with it.
MF_SEPARATOR Draws a horizontal dividing line.
MF_STRING Specifies that the menu item is a character string.

Using AppendMenu(), then, we can append our two new menu items to the popup menu this way:

void CMenusView::OnInitialUpdate()
{
    CView::OnInitialUpdate();

    m_menu = new CMenu();
    m_menu->CreatePopupMenu();

    CString caption(“Popu menu”);                               ⇐
    m_menu->AppendMenu(MF_STRING, ID_POPUP, caption);           ⇐

    CString item(“Beep”);                                       ⇐
    m_menu->AppendMenu(MF_STRING, ID_BEEP, item);               ⇐

}

Now we’ve created our new popup menu and placed it into a class variable named m_menu. We can display this new menu when the user right-clicks the view.

Displaying a Popup Menu

To intercept right mouse clicks, we use ClassWizard to connect the OnRButtonDown() function to the WM_RBUTTONDOWN Windows message.

void CMenusView::OnRButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default

    CView::OnRButtonDown(nFlags, point);
}

In this function, we display the popup menu using its TrackPopupMenu() function. That function takes a parameter you set to TPM_CENTERALIGN, TPM_LEFTALIGN, and TPM_RIGHTALIGN depending on how you want to align the menu vertically with the mouse position (we use TPM_LEFTALIGN), the position of the mouse, and a pointer to the window that owns the popup menu. In addition, this function uses screen coordinates (where the origin is at upper left of the whole screen) instead of client coordinates (where the origin is at upper left of the client area). However, we get the mouse coordinates in client coordinates in OnRButtonDown(), so we have to convert them to screen coordinates, which we do with the ClientToScreen() function.

void CMenusView::OnRButtonDown(UINT nFlags, CPoint point)
{
    ClientToScreen(&point);                                         ⇐

    m_menu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this); ⇐

    CView::OnRButtonDown(nFlags, point);
}

At this point, our popup menu appears on the screen. How do we connect our menu items to code? We’ve set up this menu in code, not with the Menu Editor. In fact, this menu doesn’t even exist until the program runs, so we can’t use the ClassWizard on it. How do we connect its items to code?

Connecting Popup Menu Items to Code

One simple way of getting ClassWizard to pay attention to our new popup menu items is simply to give them accelerator keys. Using the Accelerator Editor in the ResourceView, create a new accelerator with the ID ID_BEEP and give it the accelerator key Ctrl+B. Now when you open ClassWizard, you see ID_BEEP as one of the objects to which you can attach functions.

void CMenusView::OnBeep()
{
    // TODO: Add your command handler code here
}

We want this menu item to produce a beep with MessageBeep(), so add the following line now:

void CMenusView::OnBeep()
{
    MessageBeep(-1);                    ⇐
}

This has allowed us to add support for our popup menu in code. Run the program, as shown in Figure 4.10, right-click the view to display the popup menu, and click the Beep item in that menu to hear the computer beep. Now we’re using popup menus in Visual C++.

The code for this example, MenusView.h and MenusView.cpp, appears in Listing 4.1.


Previous Table of Contents Next


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.