Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

Folder Tab Control for Windows MFC (like MS Excel)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
5 Oct 20031 min read 216.4K   4.9K   74   23
Folder Tab control for Windows MFC (like MS Excel).

Sample Image - foldertabcontrol.jpg

Introduction

This is a folder tab window control for MFC. It looks and acts very much like the folder tab control for MS Excel, except it does not allow re-ordering of the folder tabs. It has a first button for the first tab, a next button for the next tab, a last button for the last tab and a previous button for the previous tab. You can also rename the folder tabs and delete/add new tabs interactively.

Paul DiLascia at MSDN magazine originally wrote this code in 3 separate issues of MSDN ( the last article was in the October 2002 issue). I just added the first and last buttons and the tab renaming capability.

Using the code

Paul wrote a FolderFrame.cpp class for handling the view of this class automatically. However, my view is a diagramming toolkit with zoom and pan that requires a lot of view specific code, so I do not use his FolderFrame class.

To include this control in my app, I just added it to my View class:

#include "ftab.h"
CFolderTabCtrl FolderTabs;

Then I created it in my OnCreate method:

     // create the folder tabs window
CFolderTabCtrl& ftc = FolderTabs;
VERIFY (ftc.Create (WS_CHILD|WS_VISIBLE, rc, this, 1, FTS_BUTTONS));

Then I draw it in my OnDraw method along with my horizontal scrollbar:

   // get the sizes of the client area and redraw the folder

   // tabs and the scrollbars - must do this as we might be

   // redrawing because of a change in number of folder tabs

RECT rect;

GetClientRect (&rect);

int newHeight = rect.bottom;

int newWidth = rect.right;

   // reposition the folder tabs window (calc size and limit first)

int folderTabWidth = FolderTabs.GetDesiredWidth ();

int folderTabLimit = (newWidth * 4) / 5;

if (folderTabWidth > folderTabLimit) folderTabWidth = folderTabLimit;

FolderTabs.MoveWindow (0, newHeight - GetSystemMetrics (SM_CYHSCROLL),
          folderTabWidth, GetSystemMetrics (SM_CYHSCROLL), TRUE);

// reposition and redraw the horizontal scroll bar

HorzScrollBar.MoveWindow (folderTabWidth,
          newHeight - GetSystemMetrics (SM_CYHSCROLL),
          newWidth - folderTabWidth - GetSystemMetrics (SM_CXVSCROLL),
          GetSystemMetrics (SM_CYHSCROLL), TRUE);

SCROLLINFO si;

ZeroMemory (&si, sizeof (SCROLLINFO));

si.cbSize = sizeof (SCROLLINFO);

si.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;

si.nMin = 0;

si.nMax = giPaperWdDP;

si.nPos = gisFlsHorzOffset;

si.nPage = gisFlsHorzInc;

HorzScrollBar.SetScrollInfo (&si, TRUE);

I added several message map items in my View class for handling the folder messages:

ON_NOTIFY(FTN_TABCHANGED, 1, OnChangedTab)
ON_NOTIFY(FTN_EDITSHEETS, 1, OnEditSheets)
ON_NOTIFY(FTN_TABNAMECHANGED, 1, OnChangedTabName)
ON_NOTIFY(FTN_ADDSHEET, 1, OnAddSheet)
ON_NOTIFY(FTN_DELETESHEET, 1, OnDeleteSheet)

My View class also has the methods for handling messages from the folder tab control:

//////////////////
// Changed tab
//

void CFlsView::OnChangedTab(NMHDR* nmtab, LRESULT* pRes)
{
   NMFOLDERTAB& nmft = *(NMFOLDERTAB*)nmtab;
   giCurrentLayer = nmft.iItem;   //  your new tab number in the view
   Invalidate ();
}

//////////////////
// right mouse click on sheet tabs and select properties
//

void CFlsView::OnEditSheets(NMHDR* nmtab, LRESULT* pRes)
{
   //  put up your properties dialog now
}

//////////////////
// Changed tab name
//

void CFlsView::OnChangedTabName(NMHDR* nmtab, LRESULT* pRes)
{
   NMFOLDERTAB& nmft = *(NMFOLDERTAB*)nmtab;
   int sheet = nmft.iItem;
   giCurrentLayer = sheet;
   strncpy (LayerInfo [sheet].Name, nmft.lpText, LYR_NAMELEN);
}

//////////////////
// add page
//

void CFlsView::OnAddSheet(NMHDR* nmtab, LRESULT* pRes)
{

   NMFOLDERTAB& nmft = *(NMFOLDERTAB*)nmtab;
   int sheet = nmft.iItem;

   // add new sheet to the view
   FolderTabs.AddItem (LayerInfo [newSheet].Name);

   giCurrentLayer = newSheet;
   FolderTabs.SelectItem (giCurrentLayer);
   Invalidate ();

}

//////////////////
// Delete page
//

void CFlsView::OnDeleteSheet(NMHDR* nmtab, LRESULT* pRes)
{

   NMFOLDERTAB& nmft = *(NMFOLDERTAB*)nmtab;

   int sheet = nmft.iItem;

   if (0 == sheet)
   {
      CString text = "You cannot delete ";
      text += LayerInfo [sheet].Name;
      text += ".";
      int result = MessageBox (text, "Your App name", MB_OK);
   }
   else
   {

      DeleteLayer (sheet);
      // fix the view
      FolderTabs.RemoveItem (sheet);
      // if deleting the current sheet then reset the current sheet to 0
      if (sheet == giCurrentLayer)
      giCurrentLayer = 0;
      Invalidate ();
   }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalerror C4430: missing type specifier - int assumed. Note: C++ does not support default-int Pin
Koundinya5-Nov-07 18:18
Koundinya5-Nov-07 18:18 
GeneralVisual Studio 2005 issues (analyser) Pin
User 2694223-Mar-07 1:26
professionalUser 2694223-Mar-07 1:26 
GeneralVisual Studio 2005 errors/warnings Pin
User 2694215-Mar-07 5:47
professionalUser 2694215-Mar-07 5:47 
GeneralRe: Visual Studio 2005 errors/warnings Pin
Koundinya5-Nov-07 18:17
Koundinya5-Nov-07 18:17 
QuestionWhy can't I use it under vc++6.0??? Pin
wangpai12-Oct-06 18:35
wangpai12-Oct-06 18:35 
Questionhow to use the class? the CTabCtrl cant get the click message Pin
hamcn2-Jun-06 3:17
hamcn2-Jun-06 3:17 
Generaltab control Pin
Member 66357813-Jul-05 18:29
Member 66357813-Jul-05 18:29 
QuestionAnybody got it to work? Pin
poiut8-Mar-05 9:50
poiut8-Mar-05 9:50 
GeneralExample Pin
rfhn8-Sep-04 16:59
rfhn8-Sep-04 16:59 
QuestionCan you give me an example? Pin
zhang lu21-Jul-04 22:25
zhang lu21-Jul-04 22:25 
GeneralWhich view class are you using Pin
Marcus Carey10-Mar-04 19:51
Marcus Carey10-Mar-04 19:51 
GeneralRe: Which view class are you using Pin
Yacine950004-Jun-04 0:52
Yacine950004-Jun-04 0:52 
GeneralSample Would Be Great Pin
YoSilver16-Jan-04 17:50
YoSilver16-Jan-04 17:50 
GeneralRe: Sample Would Be Great Pin
teli198228-Jul-05 21:48
teli198228-Jul-05 21:48 
i think so
GeneralWhen the leftmost tab is not the very first beginning Pin
16-Dec-03 15:46
suss16-Dec-03 15:46 
Questionexample? Pin
Cui Sheng29-Oct-03 16:16
Cui Sheng29-Oct-03 16:16 
AnswerRe: example? Pin
Lynn McGuire30-Oct-03 6:03
Lynn McGuire30-Oct-03 6:03 
GeneralHow do you create the rectangle rc ? Pin
Yacine950003-Jun-04 5:46
Yacine950003-Jun-04 5:46 
GeneralRe: How do you create the rectangle rc ? Pin
Lynn McGuire1-Jul-04 7:31
Lynn McGuire1-Jul-04 7:31 
GeneralRe: How do you create the rectangle rc ? Pin
Yacine950001-Jul-04 20:48
Yacine950001-Jul-04 20:48 
GeneralRe: How do you create the rectangle rc ? Pin
Cui Sheng5-Oct-09 20:57
Cui Sheng5-Oct-09 20:57 
GeneralVery nice! Pin
Hans Kester14-Oct-03 22:24
Hans Kester14-Oct-03 22:24 
GeneralRe: Very nice! Pin
Yacine950004-Jun-04 2:30
Yacine950004-Jun-04 2:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.