Question

Command routing problem with CMFCMenuBar

Asked by: Zoppo

Hi everybody,

when I create a new MDI application using extended features (especially 'menu bar') from MFC Feature Pack for VS 2008 I encounter following problem: When the view has set the mouse capture (SetCapture) commands from menues with shortcut are executed allthough they shouldn't be available.

For testing this I just created a new MDI project using a menu bar and implemented WM_LBUTTONUP/DOWN handlers to set capture like this:

void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
      CView::OnLButtonDown(nFlags, point);
      ::SetCapture( m_hWnd );
}

void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
      CView::OnLButtonUp(nFlags, point);
      ::SetCapture( NULL );
}

Now, while mouse button is pressed the view captures the mouse input, but I'm able to i.e. open the 'Open File' dialog by pressing the shortcut 'CTRL-O'. If I do the same in an application without CMFCMenuBar this doesn't happen (I can simulate this by simply commenting out all lines in my CMainFrame class which handle 'm_wndMenuBar' creation/docking).

I debugged the problem and could see that in both cases a call to '::TranslateAccelerator' is done in 'CMDIFrameWnd::PreTranslateMessage', in case I use CMFCMenuBar this causes the 'ID_FILE_OPEN' handler is called from here, if I don't use CMFCMenuBar this doesn't happen.

Does anybody know about a possibility to get that 'old-style' kind of command routing with use of CMFCMenuBar?

Thanks in advance,

ZOPPO

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-10-27 at 06:34:43ID23850306
Tags

Microsoft

,

MFC Feature Pack

,

VS 2008

Topics

Windows MFC Programming

,

Microsoft Visual C++

Participating Experts
2
Points
0
Comments
11

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. HWND of a CDialog...
    Hey, Continuing with my problem for having a CDialog automatically closed after a while, I need to pass the handle window parameter of the CDialog I want to send the message WM_CLOSE. I have spend plenty of hours searching for a way to have the HWND of that Dialog unsucessfu...
  2. how to Case a UINT
    A windoze "C" question If two different applications register the same message string, the applications return the same message value. The message remains registered until the Windows session ends. like this ........ UINT NEW_MSG = RegisterWindowMessage("NE...
  3. Problem with WM_CTLCOLORBTN
    I have make this HBRUSH g_hBrush = CreateSolidBrush(RGB(255,255,0)); BOOL CALLBACK DialogWndProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_CTLCOLORBTN : SetBkMode((HDC)wParam, TRANSPARENT); SetTextColor((HDC)wParam, R...
  4. Using default functionality for ON_WM_COMMAND
    Hi, I have a program that need to decide at run time whether to process certain messages or to let the framework do it. I have therefore written a ON_WM_COMMAND_RANGE handler that captures all command messages. I do some testing on it and in some cases I would like not to p...
  5. hWnd not attatched to CWnd??
    I'm new to MFC but for the last 3 months or so I've been creating a windows application that is a script interpretter. Basically it looks like a normal window but functions kind of like a command line interface that takes in scripts to be executed as well as general commands...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: itsmeandnobodyelsePosted on 2008-10-27 at 07:46:14ID: 22813136

>>>> When the view has set the mouse capture (SetCapture) commands from menues with shortcut are executed allthough they shouldn't be available.

The menu updates (reflecting both on toolbar buttons and menu items) were made in idle times only. In case of mouse capture there might be many many mouse move messages which might prevent from ever entering the idle message handling.

You should capture the mouse only when it is above a control of interest and free it when ever it is outside of the controls. Especially, when clicking at a toolbar button and/or opening a menu, the mouse should not be captured (and so the menu items were up-to-date)

 

by: ZoppoPosted on 2008-10-27 at 08:06:10ID: 22813316

Hi itsmeandnobodyelse,

thanks for the reply - but, my problem ist not the update-mechanism, my problem is that menu-commands can be called via shortcut while the view has the capture.

My application is a CAD like application with a complex GUI within a CScrollView. There are about 15 or more dialogs which can be opened via shortcuts while no drag operation in the view is active. As soon as the user starts a drag-operation the capture is set to the view, it's released when the drag operation finished.

With 'old' MFC there was no problem since menu commands weren't executed with their shortcuts as long as capture was set. With 'new' MFC (or better said as soon as a CMFCMenuBar is involved) this doesn't work anymore.

Of course now I coul implement a ON_UPDATE_COMMAND_UI handler for each of these menue commands, but I'm searching for a more generic way to get this working as it did in 'old' MFC.

In MSDN about 'SetCapture' there's this statement:
> When the mouse is captured, menu hotkeys and other keyboard accelerators do not work.

This in fact is not the case anymore when using the CMFCMenuBar.


Regards,

ZOPPO

 

by: itsmeandnobodyelsePosted on 2008-10-27 at 08:43:04ID: 22813679

>>>> can be called via shortcut while the view has the capture.
Your accelerators (shortcuts) wouldn't be able to invoke menu functions (or toolbar buttons) when the items or buttons were disabled. Enabling/disabling of menu functions happens in the UpdateUI handler functions which were called at idle times, i. e. when the message pump is not busy.

>>>> With 'old' MFC there was no problem since menu commands weren't executed with their shortcuts as long as capture was set.
That seems to have been a bug rather than a feature. I doubt that it makes much sense to ignore shortcuts while the mouse is captured - as a general restriction (though of course it is bad if you have written code which was based on that). Normally the user must not be aware of mouse capturing (though you might have changed cursor shape) and then might be confused if short-cuts were not working.  

>>>> I could implement a ON_UPDATE_COMMAND_UI handler for each of these menue commands, but I'm searching for a more generic way to get this working
Hmm. The UI handlers is the MFC way to do this. You hardly will find a better solution which has not some drawbacks. BTW, you could create the UPDATE_COMMAND_UI handlers  using the wizard and replace then all handler functions in the message map by one single handler. That way the efforts seem to manageable. You even could call the one and only UI handler function when capturing/releasing the mouse, thus regaining the *lost* feature from old MFC.

 

by: ZoppoPosted on 2008-10-27 at 09:05:48ID: 22813931

Sorry, I don't agree.

>> That seems to have been a bug rather than a feature.
First as told in MSDN exactly this behavior is stated: 'When the mouse is captured, menu hotkeys and other keyboard accelerators do not work'. We started developing that mentioned application with MSVC++ 4.0 and since then that behavior wasn't changed (even with VS 2008 it's the same as long 'normal' menues are used). There we implemented the shortcuts which can be used while the mouse is captured via handling WM_KEYDOWN/WM_KEYUP messages. This worked fine the last 10 years.

IMO this modified behavior of 'new' MFC is a bug and I'd like to find a workaround to get it working as it did before.

Of course I know about the UpdateUI-handler functionality. As told I would like to get the 'new' MFC work as the 'old' one did to avoid the need of changing/adding UPDATE_COMMAND_UI handlers for all possible shortcuts since there are a lot of these which handles the states in different situations and I fear it would take me (and our testers) a lot of time to find all commands which handlers need to be changed.

BTW: Did you already do something with MFC FeaturePack's new classes? I did and found that there are some bugs inside and even some things which IMO are not designed very well - I implemented lot of 'new' GUI elements in our application and had to implement quite a lot workarounds to get it running as expected ...

Best regards,

ZOPPO

 

by: itsmeandnobodyelsePosted on 2008-10-27 at 10:36:38ID: 22814784

The CWnd::PreTranslateMessage runs before accelerators were handled - or better said the CDialog::PreTranslateMessage or CFormView::PreTranslateMessage actually were handling the accelerators. So, you could overload these functions, check for the mouse being captured and call the baseclass function only if you want the accelerators to be handled.

>>>> Did you already do something with MFC FeaturePack's new classes?
No, my current project is a huge MFC based framework which recently was ported from VC6 to VC7. It uses only basic MFC controls (plus list control and tree control) as they have been since the beginning of MFC. So, I know the new features from EE questions only.

 

by: ZoppoPosted on 2008-10-28 at 06:40:42ID: 22821618

Hi,

I think I found a workaround which works. I found no way to handle these in PreTranslateMessage, but I found I can skip those shortcuts from working by overriding CMainFrame::OnCommand like this:

BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
      if ( HIWORD( wParam ) == 1 )
      {
            if ( NULL != GetCapture() )
            {
                  HMENU hMenu = m_wndMenuBar.GetHMenu();

                  if ( NULL != hMenu )
                  {
                        MENUITEMINFO mi = { 0 };
                        mi.cbSize = sizeof( MENUITEMINFO );
                        mi.fMask = MIIM_FTYPE;

                        if ( FALSE != GetMenuItemInfo( hMenu, LOWORD( wParam ), FALSE, &mi ) )
                        {
                              return TRUE;
                        }
                  }
            }
      }

      return CMDIFrameWndEx::OnCommand(wParam, lParam);
}

Then I had two more problems:
- i.e. pressing ALT-F opened the File menue
- pressing and releasing ALT set focus to the menu bar

To solve these I derived a class from CMFCMenuBar and added these functions:

// message handler for WM_SETFOCUS
LRESULT CMyMenuBar::OnMessageSetFocus( WPARAM, LPARAM )
{
      if ( NULL == ::GetCapture() )
      {
            CWnd* pWnd = NULL == wParam ? NULL : CWnd::FromHandle( (HWND)wParam );

            CMFCMenuBar::OnSetFocus( pWnd );
      }

      return 0;
}

// overridden from CMFCMenuBar
BOOL CMyMenuBar::TranslateChar( UINT nChar )
{
      if ( NULL != ::GetCapture() )
      {
            return FALSE;
      }

      return CMFCMenuBar::TranslateChar( nChar );
}

 

by: DanRollinsPosted on 2008-10-31 at 13:31:28ID: 22853866

Have you installed Vs2008 Service Pack 1 (released in August)?  A number of bugs were fixed.

 

by: itsmeandnobodyelsePosted on 2008-11-01 at 01:41:18ID: 22856066

>>>>> I found no way to handle these in PreTranslateMessage
Did you overload CMainFrame::PreTranslateMessage as well?

The accelerators (e. g. Alt-F) were all handled in the TranslateMessage of your message loop (in CWinThread::Run) what leads to (virtual) calls of the PreTranslateMessage of the targeted CWnd derived class.

 

by: ZoppoPosted on 2008-11-02 at 02:45:30ID: 22860836

Hi,

@DanRollins: Yes, I have installed it ...

@itsmeandnobodyelse: I tried PreTranslateMessage in MainFrame, ChildFrame, App and View classes. As written above I can debug it down to the call of '::TranslateAccelerator' in 'CMDIFrameWnd::PreTranslateMessage'. If this is called the menue pops up without a further call to any of the above mentioned classes PreTranslateMessage.

Regards,

ZOPPO

 

by: itsmeandnobodyelsePosted on 2008-11-02 at 05:30:09ID: 22861281

That is strange. I seriously had the same case where I finally found  the translation in CDialog::PreTranslateMessage.

You might set a breakpoint into CWnd::PreTranslateMessage in order to find the window which translates the shortcuts ...

 

by: ZoppoPosted on 2008-11-05 at 08:45:27ID: 22887197

Hi again,

I tested around with this a lot now and found no possibility to catch it directly in PreTranslateMessage - as told before as soon as the '::TranslateAccelerator' gets called the WM_COMMAND message is sent directly to my CView derived class, no matter if capture ist set or not.

So I found no other way to solve this as I wrote above. Up to now it seems to work fine ...


Now, I don't really know what to do with this question now - any suggestions?

Regards,

ZOPPO

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...