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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds