Solved

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

Posted on 2009-05-15
5
2,187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 100 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Viewers will learn the different options available in the Backstage view in Excel 2013.
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…

739 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