65.9K
CodeProject 正在变化。 阅读更多。
Home

Windows MFC 的文件夹选项卡控件(类似 MS Excel)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (10投票s)

2003年10月6日

1分钟阅读

viewsIcon

220679

downloadIcon

4944

Windows MFC 文件夹选项卡控件(类似于 MS Excel)。

Sample Image - foldertabcontrol.jpg

引言

这是一个用于 MFC 的文件夹选项卡窗口控件。它的外观和行为与 MS Excel 的文件夹选项卡控件非常相似,但它不允许重新排序文件夹选项卡。它具有第一个选项卡的首个按钮、下一个选项卡的下一个按钮、最后一个选项卡的最后一个按钮和上一个选项卡的上一个按钮。您还可以交互式地重命名文件夹选项卡并删除/添加新选项卡。

Paul DiLascia 在 MSDN 杂志上最初在 3 个独立的 MSDN 期刊中编写了这段代码(最后一篇文章发表在 2002 年 10 月号)。我只是添加了首个和最后一个按钮以及选项卡重命名功能。

使用代码

Paul 编写了一个 FolderFrame.cpp 类,用于自动处理该类的视图。但是,我的视图是一个绘图工具包,具有缩放和平移功能,需要大量的特定于视图的代码,因此我没有使用他的 FolderFrame 类。

为了将此控件包含在我的应用程序中,我将其添加到我的 View 类中

#include "ftab.h"
CFolderTabCtrl FolderTabs;

然后在我的 OnCreate 方法中创建它

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

然后在我的 OnDraw 方法中与我的水平滚动条一起绘制它

      // 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);

我在我的 View 类中添加了几个消息映射项,用于处理文件夹消息

   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)

我的 View 类还具有处理来自文件夹选项卡控件的消息的方法

//////////////////
// 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 ();
   }
}
© . All rights reserved.