Webocreation

Monday, November 23, 2009

Lab 8.3: Adding a Shortcut menu

Lab 8.3: Adding a Shortcut menu
In this lab, you will add a shortcut menu to an application.

Estimated time to complete this lab: 30 minutes

To complete the exercises in this lab, you must have the required software. For detailed information about the labs and setup for the labs, see Labs in this course.

Objectives

After completing this lab, you will be able to:

® Add a generic shortcut menu handler from the Component Gallery.
® Modify the menu resources associated with a shortcut menu.
® Process right-click messages to trigger a shortcut menu.
® Process messages sent from a shortcut menu.

Prerequisites

There are no prerequisites for this lab.

Exercise

The following exercise provides practice with the concepts and techniques covered in this chapter.

® Exercise 1: Adding a Shortcut Menu Component

In this exercise, you will add a shortcut menu to an application using the Component Gallery.


Exercise 1: Adding a Shortcut Menu Component
The code that forms the basis for this exercise is in \Labs\Ch08\Lab03\Baseline. Copy these files to your working directory.

In this exercise, you will add a shortcut menu and its associated test handlers to an application. When a user clicks the right mouse button in either pane of the ShowDiff application, the application displays a menu at the location of the mouse click. Usually, displaying a menu in this manner is a very simple task. However, since ShowDiff uses CRichEditView based on the Rich Edit Control, the DefWindowProc will not translate WM_RBUTTONUP to WM_CONTEXTMENU automatically; you must do that translation yourself.

The following illustration shows how the application should look.



Insert the Pop-up Menu Component into CDiffView
1. On the Project menu, click Add to Project, and then click Components and Controls. In the Gallery dialog box, double-click Developer Studio Components The following illustration shows the Components and Controls Gallery dialog box.



2. Select Pop-up Menu, click Insert, and then click OK.
The Pop-up Menu dialog box will be displayed.
3. Add the pop-up menu to the CDiffView class. Leave the menu resource ID at its default, CG_IDR_POPUP_DIFF_VIEW, and click OK. The following illustration shows the completed Pop-up Menu dialog box.



4. The Component Gallery will insert an OnContextMenu handler into the implementation of CDiffView, DiffView.cpp, as shown in this code:

void CDiffView::OnContextMenu(CWnd*, CPoint point)
{
// CG: This block was added by the Popup Menu component
{
if (point.x == -1 && point.y == -1){
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);

point = rect.TopLeft();

point.Offset(5, 5);
}

CMenu menu;

VERIFY(menu.LoadMenu(CG_IDR_POPUP_DIFF_VIEW));

CMenu* pPopup = menu.GetSubMenu(0);

ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;

while (pWndPopupOwner->GetStyle() & WS_CHILD)

pWndPopupOwner = pWndPopupOwner->GetParent();

pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,

pWndPopupOwner);
}
}

Change the default menu to support ShowDiff

1. The Component Gallery will also add a menu resource to the project. Open the Menu editor in ResourceView and edit CG_IDR_POPUP_DIFF_VIEW.
2.. Change the menu as shown in this table. The items and their IDs are included.
Menu item ID

Copy ID_EDIT_COPY

Separator
Find... ID_EDIT_FIND
Separator
Font... ID_EDIT_FONT
Color (popup)
Foreground... IDC_EDIT_COLOR_FOREGROUND
Background... IDC_EDIT_COLOR_BACKGROUND
The following illustration shows the completed shortcut menu in the Menu editor.


3. Save Diff.rc.

Add a handler for WM_RBUTTONUP
1. Open ClassWizard from the View menu, or press CTRL+W.
2. Select the CDiffView class and CDiffView object ID.
3. Select the WM_RBUTTONUP message and then click Add Function.
4. Click Edit Code.
5. You will not use the default WM_RBUTTONUP handler in CRichEditView. Comment it out.
6. CView::OnRButtonUp is passed a CPoint relative to the client window. If you explore OnContextMenu, you will discover that CMenu::TrackPopupMenu requires a CPoint relative to the screen. Use CWnd::ClientToScreen to do this conversion, as shown in this code:
ClientToScreen (&point);
7. Call CDiffView::OnContextMenu with this point.
OnContextMenu ((CWnd *)NULL, point);
8. Save DiffView.cpp. The complete function follows.
void CDiffView::OnRButtonUp(UINT nFlags, CPoint point)
{
ClientToScreen (&point);
OnContextMenu ((CWnd *)NULL, point);
//CRichEditView::OnRButtonUp(nFlags, point);
}

Add message handlers for the menu items
1. Open ClassWizard from the View menu or press CTRL+W.
2. Select the CDiffView class.
3. Select the menu object IDs, adding a function for each COMMAND and accepting their default function names, as detailed in this table.

Object ID Function name

ID_EDIT_FONT OnEditFont

IDC_EDIT_COLOR_FOREGROUND OnEditColorForeground
IDC_EDIT_COLOR_BACKGROUND OnEditColorBackground
4. Choose one of these functions and click Edit Code to navigate to the handlers.
5. In each of the function bodies, add a message box to show that the handler has been called, as shown in this code:
void CDiffView::OnEditFont()
{
AfxMessageBox ("In OnEditFont()");
}
void CDiffView::OnEditColorForeground()
{
AfxMessageBox ("In OnEditColorForeground()");
}
void CDiffView::OnEditColorBackground()
{
AfxMessageBox ("In OnEditColorBackground()");
}
6. Save DiffView.cpp. Build ShowDiff and run it.

The completed code for this exercise is in \Labs\Ch08\Lab03\Ex01.

No comments:

Post a Comment