![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
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 weve 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 MenuTo 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? Weve set up this menu in code, not with the Menu Editor. In fact, this menu doesnt even exist until the program runs, so we cant use the ClassWizard on it. How do we connect its items to code? Connecting Popup Menu Items to CodeOne 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 were using popup menus in Visual C++. The code for this example, MenusView.h and MenusView.cpp, appears in Listing 4.1.
|
![]() |
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. |