Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is it possible to get the rectangle of a contextual menu?

I'd like to be able to have the contextual menu always be inside the Window (CWnd) it is called from.

For example, if the X position is "near" the right size of the CWnd, then the contextual menu should should be called with the flag TPM_RIGHTALIGN.

For example, if the Y position is "near" the bottom size of the CWnd, then the contextual menu should should be called with the flag TPM_BOTTOMALIGN.

I don't think I can compute the size (height and height) of the menu before it is created (with TrackPopupMenu).

for example :

C++
//show the context menu
UINT menuFlags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON;

if ( point.x < menuWidth)
{
    menuFlags |= TPM_LEFTALIGN;
}
else if ( point.x > clientRect.right - menuWidth)
{
    menuFlags |= TPM_RIGHTALIGN;
}
else
{
    menuFlags |= TPM_LEFTALIGN;
}

if ( point.y < menuHeight)
{
    menuFlags |= TPM_TOPALIGN;
}
else if ( point.y > clientRect.bottom - menuHeight ) 
{
    menuFlags |= TPM_BOTTOMALIGN;
}
else
{
    menuFlags |= TPM_TOPALIGN;
}

::ClientToScreen( currentView->GetSafeHwnd(), &point );
pContextMenu->TrackPopupMenu( menuFlags,  point.x , point.y , AfxGetMainWnd()); 



Addendum:

Seems CContextMenuManager::TrackPopupMenu does something near what I want, but only for the screen borders, not a predefined Cwnd Rect.



Thanks.

Max.
Posted
Updated 16-Oct-13 8:49am
v2
Comments
Captain Price 16-Oct-13 12:05pm    
What happens if you resize the window to be smaller than the context menu ?
Maximilien 16-Oct-13 12:14pm    
Then the whole world implodes.
Sergey Alexandrovich Kryukov 16-Oct-13 13:33pm    
:-)!!!
Sergey Alexandrovich Kryukov 16-Oct-13 13:33pm    
It should be possible, just because this rectangle is also a window with HWND (yes, I tested it), so you can use raw Windows API. The question is: why?
—SA
Maximilien 16-Oct-13 14:46pm    
Why? because higher authorities asked to looked into it. another why, is that when using (newly discovered) CContextMenuManager::TrackPopupMenu it seems to work in the Y direction, but not in the X direction (i.e. if the context menu is too low in the screen, it will be pop up above the cursor instead of below.

Anyway, this is minor stuff... Thanks.

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