Right Click detection(Context menu) on CMainFrame Wnd in SDI MFC application

Hello experts, I am working on SDI MFC application in which I need to handle right clicks. I was able to handle right click(context menu) on View window. so when user clicks on view window I am displaying a new menu.
After I need to do same thing in toolbar and menu bar place holders in CMainFrame wnd. So I tried to add oncontextmenu() on CMainFrame wnd and it was showing Menu when user clicks toolbar place holder. My problems are :
1) How to detect right click on Menubar place holder
2) context menu on CMainFrameWnd replacing the default right click menu and CMainFrameWnd . Means when I click and CMainFrame wnd edges also it showing menu created by me .

Can Any suggest me how to handle these problems
Who is Participating?
ssk0507Author Commented:
Hi Experts... after so much trail and error I finally able to get the context menu for toolbar,client area and caption window as I desired for both SDI and MDI MFC applications.
For future reference I am posting the link here which helped me to solve my issue
In this link he is talking about Dialog based application but we can use the same thing for SDI/MDI MFC appliacations also. For SDI application we need to remove "CView::OnRButtonUp(nFlags, point);"  statment at the end of "OnRButtonUp" to stop displaying multiple menus.

For the context menus on client area add the following code to xxxxview.cpp file 
void CTest3View::OnRButtonUp(UINT nFlags, CPoint point) 
	// TODO: Add your message handler code here and/or call default
	CMenu popupMenu;
	CMenu* subMenu = popupMenu.GetSubMenu(0);
	subMenu->TrackPopupMenu(0, point.x, point.y,
	   AfxGetMainWnd(), NULL);
To get context menus on Toolbar and caption windows add the following code to MainFrm.cpp file 
BOOL CMainFrame::PtInClientRect(CPoint p)
   CRect rc;
   return rc.PtInRect(p);
UINT CMainFrame::OnNcHitTest(CPoint pt)
   return PtInClientRect(pt) ? HTCAPTION : CFrameWnd::OnNcHitTest(pt);
void CMainFrame::OnNcRButtonUp(UINT nHitTest, CPoint pt)
   if (nHitTest==HTCAPTION && PtInClientRect(pt)) 
      SendMessage(WM_CONTEXTMENU, (WPARAM)m_hWnd, MAKELPARAM(pt.x,pt.y));
      CFrameWnd::OnNcRButtonUp(nHitTest, pt);
void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint p)
   if (PtInClientRect(p)) {
      CMenu menu;
      CMenu* pSubMenu = menu.GetSubMenu(0);
      pSubMenu->TrackPopupMenu(0, p.x, p.y, this);
   CFrameWnd::OnContextMenu(pWnd, p);
and add the following variable in MainFrm.h file 
   // helper
   BOOL PtInClientRect(CPoint p);

Open in new window

One way is to Implement an overrride on
in the target window(s) and look for and look for right-click (WM_RBUTTONDOWN)
Or just add an
handler to the windows of interest.  You can always check the mouse location to see if the click in is a particular screen location.
ssk0507Author Commented:
Hi DanRollins, Thank you for your response. Yeah I adding Context menu for the MainFrame window and adding some popmenu code in it . But I am unable to get the particular screen location on that window. After adding the context menu on main frame wnd it is replacing the existing default right click menu of main frame window.

If I put this in other words for example if we right click on any of the microsoft windows applications( for example internet explorer) at Mainframe window blue color top bar we will get a menu with minimize,maxmize,size and move etc .. etc .. right. If we right click any point below address bar we will get a different menu with all options of showing/hiding bars . right ..

so I want to implement this kind of functinality. please suggest me if you have anu idea .. Let me know if this doesn't make any sense
The blue area on top of the window is called the "non-client" part of a window and a click on it is reported to the application differently from clicks within the the other parts.  Clicks in the commandbar part of the window are reported to the commandbar window.
All you need to do is write an OnContextMenu handler for the window.  If you don't know what window will get the messages, use the program named Spy++ (available under the Tools menu in the VC++ IDE)
ssk0507Author Commented:
Hi DanRollins, I check the toolbar window name using Spy++ (available under the Tools menu in the VC++ IDE), and the window name is ToolbarWindow32". But I am not able to find a way to add oncontextmenu hanlder for this window. I working on SDI MFC application with all default toolbar,menu and status bar options. Please let me know if u have any thoughts .. on this
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.