Solved

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

Posted on 2009-05-15
5
1,936 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now