Click here to Skip to main content
11,644,731 members (66,782 online)
Click here to Skip to main content

Folder Tab Control for Windows MFC (like MS Excel)

, 5 Oct 2003 175.3K 4.3K 68
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Lynn McGuire
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalerror C4430: missing type specifier - int assumed. Note: C++ does not support default-int Pin
Koundinya5-Nov-07 18:18
memberKoundinya5-Nov-07 18:18 
GeneralVisual Studio 2005 issues (analyser) Pin
C++ Hacker23-Mar-07 1:26
memberC++ Hacker23-Mar-07 1:26 
GeneralVisual Studio 2005 errors/warnings Pin
C++ Hacker15-Mar-07 5:47
memberC++ Hacker15-Mar-07 5:47 
GeneralRe: Visual Studio 2005 errors/warnings Pin
Koundinya5-Nov-07 18:17
memberKoundinya5-Nov-07 18:17 
QuestionWhy can't I use it under vc++6.0??? Pin
wangpai12-Oct-06 18:35
memberwangpai12-Oct-06 18:35 
Questionhow to use the class? the CTabCtrl cant get the click message Pin
hamcn2-Jun-06 3:17
memberhamcn2-Jun-06 3:17 
Generaltab control Pin
plapla13-Jul-05 18:29
memberplapla13-Jul-05 18:29 
QuestionAnybody got it to work? Pin
poiut8-Mar-05 9:50
memberpoiut8-Mar-05 9:50 
GeneralExample Pin
rfhn8-Sep-04 16:59
memberrfhn8-Sep-04 16:59 
QuestionCan you give me an example? Pin
deer@webmail.hebut.edu.cn21-Jul-04 22:25
memberdeer@webmail.hebut.edu.cn21-Jul-04 22:25 
GeneralWhich view class are you using Pin
Marcus Carey10-Mar-04 19:51
memberMarcus Carey10-Mar-04 19:51 
GeneralRe: Which view class are you using Pin
Yacine950004-Jun-04 0:52
memberYacine950004-Jun-04 0:52 
GeneralSample Would Be Great Pin
YoSilver16-Jan-04 17:50
memberYoSilver16-Jan-04 17:50 
GeneralRe: Sample Would Be Great Pin
Bin Xiong28-Jul-05 21:48
memberBin Xiong28-Jul-05 21:48 
GeneralWhen the leftmost tab is not the very first beginning Pin
cgtext16-Dec-03 15:46
membercgtext16-Dec-03 15:46 
Questionexample? Pin
Cui Sheng29-Oct-03 16:16
memberCui Sheng29-Oct-03 16:16 
AnswerRe: example? Pin
Lynn McGuire30-Oct-03 6:03
memberLynn McGuire30-Oct-03 6:03 
GeneralHow do you create the rectangle rc ? Pin
Yacine950003-Jun-04 5:46
memberYacine950003-Jun-04 5:46 
GeneralRe: How do you create the rectangle rc ? Pin
Lynn McGuire1-Jul-04 7:31
memberLynn McGuire1-Jul-04 7:31 
GeneralRe: How do you create the rectangle rc ? Pin
Yacine950001-Jul-04 20:48
memberYacine950001-Jul-04 20:48 
GeneralRe: How do you create the rectangle rc ? Pin
Cui Sheng5-Oct-09 20:57
memberCui Sheng5-Oct-09 20:57 
GeneralVery nice! Pin
Jeroen Walter14-Oct-03 22:24
memberJeroen Walter14-Oct-03 22:24 
GeneralRe: Very nice! Pin
Yacine950004-Jun-04 2:30
memberYacine950004-Jun-04 2:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 6 Oct 2003
Article Copyright 2003 by Lynn McGuire
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid