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

  • Given today's threat environment and the increasing connectivity of digital infrastructures, security teams now realize that they must assume their IT environments are subject to periodic compromise. Gone are the days when preventive measures to secure the perimeter or trying to detect malware problems using signature-match technologies were enough. New practices based on an understanding of the phases of an attack, continuous threat monitoring, and rapid attack detection and remediation are necessary. This …

  • A help desk is critical to the operations of an IT services business. As a centralized intake location for technical issues, it allows for a responsive and timely solution to get clients and their staff back to business as usual. In addition to handling immediate IT issues, a help desk performs several proactive tasks to ensure clients' IT systems remain operational and downtime is minimized. Thus, utilizing a help desk and following best practices can improve the productivity, efficiency and satisfaction of …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds