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

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds