Knowledge Base Nr: 00117 contextmenu.cpp - http://www.swe-kaiser.de

Downloads:

MFC: verschiedene Varianten von kontextsensitiven Popupmenüs:

  
//variante 1: neues menü in resource hinzufügen und an jeder beliebigen stelle zulassen
void CWinAmpInfoDlg::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
CMenu menu;

menu.LoadMenu(IDR_MYPOPUPMENU);

menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON
, point.x, point.y, this);
}

//variante 2: existierendes (unter-)menü benutzen
void CVideoServerView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME);
menu.GetSubMenu(2)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON
, point.x, point.y, this);
}

//variante 3: zweig des aktiven menüs:benutzen
void CVideoServerView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu* menu_bar = AfxGetMainWnd()->GetMenu();
CMenu* file_menu = menu_bar->GetSubMenu(0); //(meistens 'File'-menü)
file_menu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON
, point.x, point.y, this);
}

//variante 4: kontextsensitiv für position innerhalb eines dialogs
void CVideoServerView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CWnd* pWndPreview = GetDlgItem(IDC_FRAME);

CPoint p(point);
ScreenToClient(&p);
CWnd* pWndHit = this->ChildWindowFromPoint(p);

if (pWndHit != pWndPreview) //contextmenü nur wenn rechte maustaste 'in' video-frame gedrückt
return;
/*
//umrechnung von maus(screen)-koordinaten in clientkoordinaten des controls
RECT rect;
pWndPreview->GetWindowRect(&rect);
ScreenToClient(&rect);

m_nMouseX = p.x - rect.left;
m_nMouseY = p.y - rect.top;
*/
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME);
menu.GetSubMenu(2)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this);
}