Click to See Complete Forum and Search --> : Pop-Up menu MFC?


June 10th, 1999, 03:50 PM
I have created a dlg MFC app. Then I added a componenet/control, which was a pop-up menu to the dlg I created. So now my app runs great and when you right click it pops up a menu. How do I control this so that the menu only pop's up when I click on a button that I have added with the id of ID_OPTIONS? I know how to add the message handle for the ID_OPTIONS button. I just don't know how my program is currently calling this function? I can't figure out where my app is catching that messages of a right click and sending it to that function that was added when I added the component/control. Thanks for any help.

Eugene N
July 22nd, 1999, 06:41 AM
I will try to help you :)

1. Do you want to display popup when user click on button-control IDC_OPTIONS ?



void CYouDialog::OnOptions()
{
CRect r;
GetDlgItem( IDC_OPTIONS )->GetWindowRect( r );

CMenu menu;
if (menu.LoadMenu(ID_POPUP_MENU))
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, r.left, r.top,
this/*or window which must handle this command*/);
}
}





2. Yo want to display popup when user right-click on control IDC_OPTIONS ?



void CYouDialog::OnContextMenu(CWnd* pWnd, CPoint point) // WM_CONTEXTMENU message handler
{
CRect r;
GetDlgItem( IDC_OPTIONS )->GetWindowRect( r );

if ( r.PtInRect( point ) ) // this condition can be replaced by next line:
// if ( ::GetWindowLong( pWnd->GetSafeHwnd(), GWL_ID ) == IDC_OPTIONS )
{
CMenu menu;
menu.LoadMenu( IDR_CONTEXT_MENU );
menu.GetSubMenu(0)->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON,
point.x, point.y, this );
}
}





If any else, email me. Try to exclude the word "button" when you think "right-click or "left-click" - it is hard to understand for me (click button control or mouse button ?). If you are speak russian - simpify my life please :)

Eugene N
July 22nd, 1999, 06:44 AM
I will try to help you :)

1. Do you want to display popup when user click on button-control IDC_OPTIONS ?



void CYouDialog::OnOptions()
{
CRect r;
GetDlgItem( IDC_OPTIONS )->GetWindowRect( r );

CMenu menu;
if (menu.LoadMenu(ID_POPUP_MENU))
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, r.left, r.top,
this/*or window which must handle this command*/);
}
}





2. Yo want to display popup when user right-click on control IDC_OPTIONS ?



void CYouDialog::OnContextMenu(CWnd* pWnd, CPoint point) // WM_CONTEXTMENU message handler
{
CRect r;
GetDlgItem( IDC_OPTIONS )->GetWindowRect( r );

if ( r.PtInRect( point ) ) // this condition can be replaced by next line:
// if ( ::GetWindowLong( pWnd->GetSafeHwnd(), GWL_ID ) == IDC_OPTIONS )
{
CMenu menu;
menu.LoadMenu( IDR_CONTEXT_MENU );
menu.GetSubMenu(0)->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON,
point.x, point.y, this );
}
}





If any else, email me.
Please try to exclude the word "button" when you think "right-click or "left-click"
- it is hard to understand for me (click button control or mouse button ?).
If you are speak russian - simpify my life please :)

Best Regards, Eugene