Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to show popup menu on right click anywhere in the dialog

I created one menu in resource

while using the TrackPopupMenu with menu I'm getting thin window

and while using with submenu I'm getting that error

What I have tried:

void CMenuPraticeDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
	
	GetCursorPos(&point);
	CMenu menu;
    VERIFY(menu.LoadMenu(IDR_MENU_POPUP));
    CMenu *pSub = menu.GetSubMenu(1);
    // Modify menu items here if necessary (e.g. gray out items)
//Here I'm getting error
    int nCmd = pSub->TrackPopupMenuEx(
        TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_RETURNCMD | TPM_NONOTIFY,
        point.x, point.y, AfxGetMainWnd(), NULL);
    if (nCmd)
        SendMessage(WM_COMMAND, nCmd);

}



POINT pt;
//getting the current cursor point
GetCursorPos(&pt);
//Here I'm getting thin window
menu.TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x,pt.y,this );
Posted
Updated 22-Feb-17 1:39am

1 solution

The error is probably in this line
CMenu *pSub = menu.GetSubMenu(1);
When the menu loaded from the resources contains only one popup menu (which is a common case) GetSubMenu() returns NULL.

So pass the position 0 instead.

[EDIT]
When using TrackPopupMenu[Ex], the menu must be a popup menu retrieved by calling CMenu::GetSubMenu() or having been created using CMenu::CreatePopupMenu. Using a normal menu will not work.
[/EDIT]
 
Share this answer
 
v2
Comments
Premnath Mali 22-Feb-17 22:56pm    
If I passed 0 still getting same error
Jochen Arndt 23-Feb-17 2:44am    
Then you probably did not have a popup menu in your IDR_MENU_POPUP resource.

When opening the RC file with a text editor it should look like:

IDR_MENU_POPUP MENU
BEGIN
POPUP "menu_name"
BEGIN
MENUITEM "item text" ITEM_CMD_ID
// ...
END
END

If it does not look like the above use the resource editor to create a popup menu.
Premnath Mali 23-Feb-17 4:37am    
Sir I just want popup menu which will popup on right click this seems we are creating menu and then inside that popop menu

I Created a menu with only three options Maximize Minimize and exit that's it and I want when user right click anywhere on the dialog i want to show those menus How do I do that?
Jochen Arndt 23-Feb-17 4:53am    
You probably created a "normal" menu. But you have to create a popup menu.
It is Windows specific that you have to create a menu resource (with an empty "normal" menu by not specifying a caption) which contains a popup menu (or even more). See also https://msdn.microsoft.com/en-us/library/k5az22fk.aspx.

If you don't want to do that you can use CMenu::CreatePopupMenu() and add the menu items within your program. But using a resource is the common and more portable version.
Premnath Mali 23-Feb-17 5:15am    
Thanks Now Got it Exactly What I Want

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900