[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-05-15
5
Medium Priority
?
2,446 Views
Last Modified: 2013-12-21
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
0
Comment
Question by:ssk0507
  • 3
  • 2
5 Comments
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 400 total points
ID: 24410073
One way is to Implement an overrride on
    PreTranslateMessage
    http://msdn.microsoft.com/en-us/library/kkbhxcs2(VS.80).aspx
in the target window(s) and look for and look for right-click (WM_RBUTTONDOWN)
Or just add an
   OnContextMenu
   http://msdn.microsoft.com/en-us/library/aa249869(VS.60).aspx
handler to the windows of interest.  You can always check the mouse location to see if the click in is a particular screen location.
0
 

Author Comment

by:ssk0507
ID: 24414849
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24416416
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)
0
 

Author Comment

by:ssk0507
ID: 24423997
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
0
 

Accepted Solution

by:
ssk0507 earned 0 total points
ID: 24427780
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
http://msdn.microsoft.com/en-us/magazine/cc164126.aspx
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;
	popupMenu.LoadMenu(IDR_MENU2);
	CMenu* subMenu = popupMenu.GetSubMenu(0);
	ClientToScreen(&point);
	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;
   GetClientRect(&rc);
   ClientToScreen(&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));
   else
      CFrameWnd::OnNcRButtonUp(nHitTest, pt);
}
 
 
void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint p)
{
   if (PtInClientRect(p)) {
      CMenu menu;
      VERIFY(menu.LoadMenu(IDR_MENU1));
      CMenu* pSubMenu = menu.GetSubMenu(0);
      pSubMenu->TrackPopupMenu(0, p.x, p.y, this);
      return;
   }
   CFrameWnd::OnContextMenu(pWnd, p);
}
and add the following variable in MainFrm.h file 
protected:
   // helper
   BOOL PtInClientRect(CPoint p);

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question