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();
    if(pDoc)
    {
	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.



Comments

  • Thanks

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

    Originally posted by: Hans Wedemeyer

    Great tip.... thanks.

    Reply
  • Great

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

    Originally posted by: Vilmos

    Thanks. It is simple and works fine. Vilmos

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • 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))||
    ID_SESSION_RESET );

    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
    }

    //__AG
    }

    return bHandled;
    }

    Reply
  • 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

    Reply
  • 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.

    Reply
  • Gracias

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

    Originally posted by: Morgan

    I owe you a cerveza for this one!

    Reply
  • 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!

    Reply
  • Brilliant

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

    Originally posted by: Dennis

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

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds