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 :-
    public:
     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...
     CEditView::OnInitialUpdate();
    
     // 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...
     CEditView::OnDestroy(); 
    }
    

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.

Downloads

Download Demo Project - 33 Kb
Download Source - 15 Kb

History



Comments

  • 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 ) 
     
    to:
     
    //begin
    bool bFind = false;
    CWnd* pwnd = GetFocus();
    CWnd* pView = wndButton->m_AttachedView;
    while (pwnd != 0)
    {
        if(pwnd == pView)
        {
            bFind = true;
            break;
        }
        pwnd = pwnd->GetParent();
    }		
    if ( bFind )
    //end. by Cnix.
    //This code shows selected button normally if the view is a CFormView.

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


    to;

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

    // *** NEW CODE
    WINDOWPLACEMENT wndpl;
    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.

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

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds