Command routing beyond a split frame

Abstract: The article present a simple method of routing WM_COMMAND messages through a number of views in a split frame window. This simplifies dealing with command routing and UI updates for inactive views.

The standard framework route does not include inactive views, which causes toolbar buttons and menus to gray when their mother view is deactivated. Users are confused. To bring back their happiness, I have overridden the CCmdTarget::OnCmdMsg function in the main frame (obviously derived from CFrameWnd):

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
    CDocument *pDoc = GetActiveDocument();
	POSITION pos = pDoc->GetFirstViewPosition();
	CView *pView = NULL;
	while(pView = pDoc->GetNextView(pos))
	    if(pView != GetActiveView()
		&& pView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

    return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

I used a list of views contained in the active document (if any). The command message is passed to all views but the active one, which had a chance to handle it before it was routed to the frame window. If the message is handled by one of the views (OnCmdMsg indicates it by returning TRUE), no further processing is needed and we can return. Otherwise, the base class member is called to restore the conventional framework route.


  • Thanks

    Posted by Legacy on 07/05/2003 12:00am

    Originally posted by: Hans Wedemeyer

    Great tip.... thanks.

  • Great

    Posted by Legacy on 05/12/2003 12:00am

    Originally posted by: Vilmos

    Thanks. It is simple and works fine. Vilmos

  • Useful Technique !

    Posted by Legacy on 04/29/2002 12:00am

    Originally posted by: Mahesh Shimpi

    Thanks a lot. That saved lot of my efforts in handling these mesg handlers in different splitter views.

  • Thanks

    Posted by Legacy on 01/06/2002 12:00am

    Originally posted by: Vlad

    This code gave me a free hour of guitar shredding
    instead of looking into MFC manuals.

  • Conditional routing ("Ctrl" pressesd) - in view or document ???

    Posted by Legacy on 12/14/2001 12:00am

    Originally posted by: Amit Gefen

    I made a conditional routing to one on my doc kinds. If the "Ctrl" button is pressed the command will be distributed to all document belongs to this template. If not only the active document will respond.
    Also it works fine, My difficulty is to accept that CDucument take care for "Ctrl" state that is part of GUI and belongs more to View and not Document. On the other hand it seems that it is the most suitable place.

    Someone have an opinion??? or suggestion???

    BOOL CLomahDoc::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
    // TODO: Add your specialized code here and/or call the base class

    BOOL specified_command, bHandled = CDocument::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

    //Specify to "Target" selecrion & "Reset"
    specified_command = ( ((nID>=ID_TARGET_F)&&(nID<=ID_TARGET_L))||

    if (bHandled && specified_command)
    //AG__ (On "Ctrl" pressed: update also inactive similars)

    static BOOL in_process;

    if (!in_process)//eliminate infinite regresion
    if (::GetKeyState(VK_CONTROL)<0)//if "Ctrl" down
    in_process = TRUE;//start

    POSITION DocPos = m_pDocTemplate->GetFirstDocPosition();

    while (DocPos)//iterate template members
    CLomahDoc* pDoc =
    (CLomahDoc*) m_pDocTemplate->GetNextDoc(DocPos);

    if (pDoc!=this)//ignore self
    pDoc->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

    in_process = FALSE;//end


    return bHandled;

  • Very useful.

    Posted by Legacy on 11/15/2001 12:00am

    Originally posted by: Ritesh Kale

    I would like to thank the author for this immensely useful contribution.

    -Ritesh Kale

  • Wow, I can keep my job.

    Posted by Legacy on 10/17/2001 12:00am

    Originally posted by: Dan

    I had a feeling, in an abstract sense, that
    the problem was command routing. To come up
    with what you did would have taken me a
    long time. Thanks for your contribution.

  • Gracias

    Posted by Legacy on 09/24/2001 12:00am

    Originally posted by: Morgan

    I owe you a cerveza for this one!

  • thanks for this hint

    Posted by Legacy on 11/16/2000 12:00am

    Originally posted by: Hemmi

    After searching 2 weeks for a solution to handle menu commands in docking windows I found this article. And after 10 minutes my application works ....

    thousend thanks to the author!

  • Brilliant

    Posted by Legacy on 11/01/2000 12:00am

    Originally posted by: Dennis

    It's just easy code that does great things...

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date