菜鸟寒号(嚎):请各位翻译,没钱,有分
MFC Windows 程序设计:以下段落,中文版读了几遍,意思稍明白;可是思路不清(不是我中文水平不行,就是翻译狗屁不通)。原文如下(代码就不用翻译了):
TrackPopupMenu is typically called in response to WM_CONTEXTMENU messages. MFC's ON_WM_CONTEXTMENU macro maps WM_CONTEXTMENU messages to the message handler OnContextMenu. OnContextMenu receives two parameters: a CWnd pointer identifying the window in which the click occurred and a CPoint containing the cursor's screen coordinates:
afx_msg void OnContextMenu (CWnd* pWnd, CPoint point)
If necessary, you can translate the screen coordinates passed in point into client coordinates with CWnd::ScreenToClient. It might seem curious that OnContextMenu receives a pointer identifying a window since mouse messages go to the window under the cursor. However, there's a reason. Unlike other messages, WM_CONTEXTMENU messages percolate upward through the window hierarchy if a right-click occurs in a child window (for example, a push button control) and the child window doesn't process the message. Therefore, if a window contains child windows, it could receive WM_CONTEXTMENU messages with pWnd containing a pointer to one of its children.
It's important for an OnContextMenu handler to call the base class's OnContextMenu handler if it examines pWnd or point and decides not to process the message. Otherwise, WM_CONTEXTMENU messages won't percolate upward. Worse, right-clicking the window's title bar will no longer display the system menu. The following OnContextMenu handler displays the context menu referenced by pContextMenu if the button click occurs in the upper half of the window and passes it to the base class if the click occurs elsewhere:
void CChildView::OnContextMenu (CWnd* pWnd, CPoint point)
{
CPoint pos = point;
ScreenToClient (&pos);
CRect rect;
GetClientRect (&rect);
rect.bottom /= 2; // Divide the height by 2.
if (rect.PtInRect (pos)) {
pContextMenu->TrackPopupMenu (TPM_LEFTALIGN ¦
TPM_LEFTBUTTON ¦ TPM_RIGHTBUTTON, point.x, point.y,
AfxGetMainWnd ());
return;
}
CWnd::OnContextMenu (pWnd, point);
}
In a view-based application like Shapes, the WM_CONTEXTMENU handler is typically placed in the view class because that's where the objects that are subject to right clicks are displayed.
How do you get a pointer to a context menu in order to display it? One method is to construct a CMenu object and build the menu with CMenu member functions. Another is to load the menu from a resource in the same way that a top-level menu is loaded. The following menu template defines a menu that contains one submenu:
IDR_CONTEXTMENU MENU
BEGIN
POPUP ""
BEGIN
MENUITEM "©", ID_CONTEXT_COPY
MENUITEM "&Rename", ID_CONTEXT_RENAME
MENUITEM "&Delete", ID_CONTEXT_DELETE
END
END
The following statements load the menu into a CMenu object and display it as a context menu:
CMenu menu;
menu.LoadMenu (IDR_CONTEXTMENU);
CMenu* pContextMenu = menu.GetSubMenu (0);
pContextMenu->TrackPopupMenu (TPM_LEFTALIGN ¦
TPM_LEFTBUTTON ¦ TPM_RIGHTBUTTON, point.x, point.y,
AfxGetMainWnd ());
If your application uses several context menus, you can define each context menu as a separate submenu of IDR_CONTEXTMENU and retrieve CMenu pointers by varying the index passed to GetSubMenu. Or you can define each one as a separate menu resource. In any event, attaching the context menu to a CMenu object that resides on the stack ensures that the menu will be destroyed when the object goes out of scope. The menu is no longer needed after TrackPopupMenu returns, so deleting it frees up memory that can be put to other uses.