My problem is that I can't display a right click popup menu for the list control in the second dialog

My problem is that I can't display a right click popup menu for the list control in the second dialog

I have a modal dialog with a list control. Right clicking one of the items in the list control, shows a popup menu. Clicking this popup menu, opens another modal dialog with another list control.

My problem is that I can't display a right click popup menu for the list control in the second dialog.


I tried


void CMyListCtrl::OnContextMenu(CWnd* , CPoint point)
{
if (GetSelectedCount() == 0)
return ;

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(IDR_HISTORY_MENU));

CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this->GetParent();
SetForegroundWindow();

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

}


and I also added to the parent dialog ON_WM_INITMENUPOPUP

Also tried just to create a popup menu on the fly


void CHistoryListCtrl::OnContextMenu(CWnd* , CPoint point)
{
if (GetSelectedCount() == 0)
return ;

if (point.x == -1 && point.y == -1)
{
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);

point = rect.TopLeft();
point.Offset(5, 5);
}

CMenu addMenu;
addMenu.CreatePopupMenu();
// add all possible interface items
CString mstr; mstr="Compare";
addMenu.AppendMenu(MF_ENABLED | MF_STRING,ID_HISTORY_COMPARE , (LPCTSTR)mstr);

addMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON,point.x,point.y,this,NULL);

addMenu.DestroyMenu();
}

It didn't work as well. Tried to add SetForgroundWindow before the call to TrackPopupMenu, but it also failed.


Any idea what to do ?
I think the reason is MFC doesn't expect two modal dialogs on the screen at the same time.  If you make the first one a modeless dialog, does it work?  I think the problem is the way MFC pumps messages after the first dialog is shown.  The messages are consumed by the first dialog, so that what is required to support the second TrackPopupMenu is not done.  Unfortunately, I don't know a workaround.

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright