Oct 232013
 

Why do you need to Select an MDI tab programmatically?

A customer recently asked this question on how to select an MDI tab programmatically, thought I’ll share the solution out here. When creating an MFC MDI application we have an option to enable tabbed view of MDI documents. Once this is done here’s how the resultant application will look like…

MFC MDI TabsMFC MDI Tab Groups

MFC MDI Tab Groups

Note that except for the first sample image you have multiple tab groups in the rest of the screenshots , this is a facility provided by the tabbing framework of MFC. This is important when you select an MDI tab programmatically: You’ll also have to decide which MDI tab from which group you would like to select.

Code to Select an MDI tab programmatically

In MFC framework tab groups are represented by CMDIClientAreaWnd::GetMDITabGroups() which returns a member variable of type CObList. This is how the code looks like…

const CObList& GetMDITabGroups() const { return m_lstTabbedGroups; }

This is a list of tab controls. Each tab control in this group denotes a tab group. So when activating a tab we’ll have to decide which tab in a tab group should be activated.

Following sample code activates the first tab in all the tab groups. Please note tab index starts at zero.

void CMainFrame::OnViewActivatetab()
{
    const CObList& TabGrps = m_wndClientArea.GetMDITabGroups();
    for (POSITION pos = TabGrps.GetHeadPosition(); pos != 0;)
    {
        CMFCTabCtrl* pNextWnd = DYNAMIC_DOWNCAST(CMFCTabCtrl, TabGrps.GetNext(pos));
        pNextWnd->ActivateMDITab(1);
    }
}
May 052009
 

People are having trouble finding feature pack samples installed in the samples directory. I too had a similar problem. So what you need to do is to uninstall the previous sample exe that you’ve installed. Then install the samples from here. Good thing about this is that you’ll have an updated samples package.

If you’re still having problems, I’ve uploaded them for you to my blog, please rename this file to zip after downloading or save as zip.

Apr 152009
 

What’s CMFCEditBrowseCtrl ?

CMFCEditBrowseCtrl is a specialized edit control (MFC Feature Pack VS2008) with a browse button attached to it’s right side, when we click on this button we get an open file dialog or an open folder dialog. See this sample screenshot.

Browse edit control sample

Browse edit control sample

Also this control allows us to implement our own event handling by overriding OnBrowse function of this class. This a cool control which I like a bit too much since I know how painful it is to implement one.

Some tips on how to make such a control…

  1. Handle nc calcsize event, so that you can specify size of the area that the browse button will take which in turn results in edit control resizing it’s client area to adjust the button.
  2. Handle nc paint to draw the button, also you have to force generate an nc calcsize message for once in the beginning.
  3. Handle mouse up and mouse down event.

Usage of CMFCEditBrowseCtrl

  1. You should be working in VS2008 SP1 or with Feature pack installation
  2. Add an edit control to a dialog
  3. Open .h file of this dialog’s class and add a member variable – CMFCEditBrowseCtrl m_EditBrowse;
  4. Open .cpp file and add a call to sub class this item in DoDataExchange
    DDX_Control( pDX, IDC_EDIT_FILEBROWSE, m_EditBrowse);
  5. Then in OnInitDialog
    // Note: Only one of these calls will work at a time!
    m_EditBrowse.EnableFileBrowseButton(); // To show file open dialog
    m_EditBrowse.EnableFolderBrowseButton(); // To show folder browse dialog
    m_EditBrowse.EnableBrowseButton(); // To do custom event handling
  6. That’s it, now you’ve got the browse edit working. 🙂
  7. Note that above calls take some parameters (which has default values) look up in MSDN.

Custom event handling for CMFCEditBrowseCtrl browse button

Here is a small sample on how to handle custom browse button event handling, taken from MSDN samples…

class CMyBrowseEdit : public CMFCEditBrowseCtrl
{
    virtual void OnBrowse()
    {
        MessageBox(_T("Browse item..."));
        SetWindowText(_T("New value!"));
    }
};

More on CMFCEditBrowseCtrl…

Use SetBrowseButtonImage to change browse button image, also has an option to maintain such a bitmap or an icon. There are some more virtual functions which could be useful…

  1. OnDrawBrowseButton – Override to for some additional painting from your side
  2. OnChangeLayout – Called when browse button mode changes, i.e. from folder to file mode etc (I guess so). 🙂