Adding a Document Selector to a MDI Application

Document Selector is a Windows taskbar like control which enables user to quickly jump to a selected document easily in a MDI application. The idea came in my mind after using Editplus, a text editor from ES-Computing. I was seeking some help for the purpose as I was unable to write the whole thing from scratch. After searching a little, I found the article "A Switcher Control (like the Windows Task Bar)" at codeguru posted by Jimmy Brush. I modified the code as appropriate and got a working document selector which I am posting here.

In the beginning of writing the code, I was willing to make the Document selector as close as the taskbar in it's functionality. But because of the complexity the spin button control was adding, I dropped the idea and added a Menu button to drop down a menu for unshown buttons. But soon I will post a updated article for the document selector with a spin button support too.

The future version of the Document bar will have following additional features :-

  • Support for image lists.
  • Support for both the Spin button control and menu button.
  • Docking document bar.
  • Automatic removal of buttons when the view is destroyed.
  • Support for context menu.

It will be soon posted in a month or two. (If you think that "soon" is too late, please don't mind. I am just 19, and studying in a college. My exams are nearing over. That one month span is just for those exams.)

To add the document bar to your application, all you have to do is to take the five following steps :-

  • Include the following six files in your project and add the menubutton.bmp (or IDB_MENU_BUTTON if you downloaded the demo project) to your resource file.
    • DocSelect.cpp
    • DocSelect.h
    • MenuButton.cpp
    • MenuButton.h
    • SwitcherButton.cpp
    • SwitcherButton.h

  • Add a member of type CDocSelect to your CMainFrame class :-
     CDocSelector m_wndDocSelector;
  • Create the Bar Window in the CMainFrame OnCreate function:-
    m_wndDocSelector.Create(NULL, NULL, WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | 
                            CBRS_TOP, CRect(0,0,0,0), this, AFX_IDW_STATUS_BAR);
    m_wndDocSelector.SetBarStyle( CBRS_ALIGN_TOP );

  • Add a new button when it's view is created, by overriding the OnInitialUpdate function in your view class :-
    void CDocumentBarView::OnInitialUpdate() 
     // Call base class's OnIntialUpdate First...
     // Replace IDR_DOCUMETYPE with your Icon resource ID.
     ((CMainFrame*)AfxGetMainWnd())->m_wndDocSelector.AddButton( this, IDR_DOCUMETYPE );

  • Remove button of the destroying view, by overriding the OnDestroy function in your view class :-
    void CDocumentBarView::OnDestroy() 
     ((CMainFrame*)AfxGetMainWnd())->m_wndDocSelector.RemoveButton( this );
     // Call base class's OnDestroy after removing button...

All the selection and deselection process is handled by the document selector itself. You don't have to override OnSetFocus and OnKillFocus to manually select or deselect a view's button.

If you extend the document selector, please notify me of the changes and post me a copy. I will be thankful.


Download Demo Project - 33 Kb
Download Source - 15 Kb



  • Debug for CFormView in VS2003

    Posted by cnix on 03/20/2005 04:48am

    Changed the code in CDocSelector::OnTimer():
    if ( GetFocus() == wndButton->m_AttachedView ) 
    bool bFind = false;
    CWnd* pwnd = GetFocus();
    CWnd* pView = wndButton->m_AttachedView;
    while (pwnd != 0)
        if(pwnd == pView)
            bFind = true;
        pwnd = pwnd->GetParent();
    if ( bFind )
    //end. by Cnix.
    //This code shows selected button normally if the view is a CFormView.

  • Slight improvement

    Posted by Legacy on 02/05/2002 12:00am

    Originally posted by: John Dixson

    Changed the code in CDocSelector::OnSelChange() from;

    // Yogesh Jagota
    CMDIFrameWnd *pFrame = (CMDIFrameWnd *)AfxGetMainWnd();
    CWnd* Window = (CWnd*)newsel->m_FrameWnd;
    pFrame->MDIActivate( Window );
    // End Yogesh Jagota


    // Yogesh Jagota
    CMDIFrameWnd *pFrame = (CMDIFrameWnd *)AfxGetMainWnd();
    CWnd* Window = (CWnd*)newsel->m_FrameWnd;

    // *** NEW CODE
    Window->GetWindowPlacement( &wndpl);
    if(wndpl.showCmd == SW_SHOWMINIMIZED) Window->ShowWindow(SW_RESTORE);

    // *** END NEW CODE

    pFrame->MDIActivate( Window );
    // End Yogesh Jagota

    This added code restores a minimized window when selected.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

Most Popular Programming Stories

More for Developers

RSS Feeds

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