?
Solved

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

Posted on 2009-05-15
5
Medium Priority
?
2,321 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 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

777 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