⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atloutbarctrl.h

📁 这是一本学习 window编程的很好的参考教材
💻 H
📖 第 1 页 / 共 5 页
字号:
// Copyright (c) Iuri Apollonio 1998
// Use & modify as you want & need, and leave those 3 lines.
// http://www.codeguru.com
//
// =============================================================================
//
// Port to ATL/WTL Rashid Thadha 01/03/2001
// Requires WTL v3.1
//
// 1.00   01/03/2001    Ported to WTL
// 1.01   05/04/2001    Fixed painting problem when resizing frame
// 
// =============================================================================

#if !defined(ATL_OUTBARCTRL_H__)
#define ATL_OUTBARCTRL_H__

#define WM_OUTBAR_NOTIFY		WM_USER + 1
#define NM_OB_ITEMCLICK			1
#define NM_OB_ONLABELENDEDIT	2
#define NM_OB_ONGROUPENDEDIT	3
#define NM_OB_DRAGITEM			4
#define NM_FOLDERCHANGE			5

#define ID_GFX_SMALLICON                50000
#define ID_GFX_LARGEICON                50001
#define ID_GFX_RENAMEITEM               50002
#define ID_GFX_REMOVEITEM               50003

#define _ID_GFX_SMALLICON	"Small Icons"
#define _ID_GFX_LARGEICON	"Large Icons"
//#define _ID_GFX_REMOVEITEM	"Remove Item"
//#define _ID_GFX_RENAMEITEM	"Rename Item"


#include "atlgroupedit.h"


#include <list>
using namespace std;

struct OUTBAR_INFO 
{
	int				index;
	const char *	cText;
	int				iDragFrom;
	int				iDragTo;
};

class CBarItem
{
public:
	CBarItem(const char * name, const int image, DWORD exData)
  {
	  cItem = NULL;
	  iImageIndex = image;
	  dwData = exData;
	  if (name)
	  {
		  cItem = new char[lstrlen(name)+1];
		  ATLASSERT(cItem);
		  lstrcpy(cItem, name);
	  }
  }
	virtual ~CBarItem()
  {
	  if (cItem) delete [] cItem;
  }

	int iImageIndex;
	char*	cItem;
	DWORD	dwData;
};
typedef list<CBarItem*, allocator<CBarItem*> > CBarItemList;

class CBarFolder
{
public:
	CBarFolder(const char * name, DWORD exData)
  {
    cName = NULL;
    dwData = exData;

    if (name)
    {
	    cName = new char[lstrlen(name)+1];
	    ATLASSERT(cName);
	    lstrcpy(cName, name);
    }
    pLargeImageList = NULL;
    pSmallImageList = NULL;
    pChild = NULL;
  }

	virtual  ~CBarFolder()
  {
    if (cName) delete [] cName;
	  CBarItemList::iterator ListIterator;	
	  for (ListIterator = arItems.begin(); ListIterator != arItems.end(); ++ListIterator)        
	  {		
		  CBarItem* pItem = *ListIterator;
		  if (pItem)
        delete pItem;
    }
  }

	int GetItemCount()
  {
	  return arItems.size();
  }
	int InsertItem(int index, const char * text, const int image, const DWORD exData)
  {
	  if (index < 0 || index > GetItemCount()) index = GetItemCount();

	  int c = GetItemCount();

	  CBarItem * pbf = new CBarItem(text, image, exData);
	  ATLASSERT(pbf);
	  if (index < GetItemCount()) 
    {
      int nIndex = 0;
      CBarItemList::iterator ListIterator;	
      for (ListIterator = arItems.begin(); ListIterator != arItems.end();++ListIterator)        
      {
        if (nIndex == index)
        {
          arItems.insert(ListIterator, pbf);
          break;
        }
        nIndex++;
      }
    }
	  else 
		  arItems.insert(arItems.end(), pbf);

	  c = GetItemCount();

	  return index;
  }

	char*	cName;
	DWORD	dwData;
	CImageList*	pLargeImageList;
	CImageList*	pSmallImageList;
	CBarItemList	arItems;
	CWindow*	pChild;
};

typedef list<CBarFolder*, allocator<CBarFolder*> > CBarFolderList;

class COutBarCtrl : public CWindowImpl<COutBarCtrl, CWindow>
{

// Construction
public:

  DECLARE_WND_CLASS_EX("MFXOutBarCtrl", CS_DBLCLKS, NULL);

  BEGIN_MSG_MAP(COutBarCtrl)
    MESSAGE_HANDLER(WM_CREATE, OnCreate)
    MESSAGE_HANDLER(WM_TIMER, OnTimer)
    MESSAGE_HANDLER(WM_PAINT, OnPaint)
    MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
    MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
    MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
    MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
    MESSAGE_HANDLER(WM_SIZE, OnSize)
    MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
		COMMAND_ID_HANDLER(ID_GFX_LARGEICON, OnGfxLargeicon)
		COMMAND_ID_HANDLER(ID_GFX_SMALLICON, OnGfxSmallicon)
		COMMAND_ID_HANDLER(ID_GFX_REMOVEITEM, OnGfxRemoveitem)
		COMMAND_ID_HANDLER(ID_GFX_RENAMEITEM, OnGfxRenameitem)
    REFLECT_NOTIFICATIONS()
	  MESSAGE_HANDLER(WM_OUTBAR_NOTIFY, OnEndLabelEdit)
	  REFLECT_NOTIFICATIONS()
	  //@@ON_UPDATE_COMMAND_UI(ID_GFX_LARGEICON, OnUpdateGfxLargeicon)
	  //@@ON_UPDATE_COMMAND_UI(ID_GFX_SMALLICON, OnUpdateGfxSmallicon)
	  //@@ON_UPDATE_COMMAND_UI(ID_GFX_REMOVEITEM, OnUpdateGfxRemoveitem)
	  //@@ON_UPDATE_COMMAND_UI(ID_GFX_RENAMEITEM, OnUpdateGfxRenameitem)
	END_MSG_MAP()

	BOOL PreTranslateMessage(MSG* pMsg)
	{
		pMsg;
		return FALSE;
	}

  COutBarCtrl() : m_pGroupEdit(NULL)
  {
    crBackGroundColor = RGB(58,110,165);//GetSysColor(COLOR_3DSHADOW);//
	  crBackGroundColor1 = RGB(58,110,165);//GetSysColor(COLOR_3DSHADOW);
	  crTextColor = GetSysColor(COLOR_WINDOW);
	  cr3dFace = GetSysColor(COLOR_3DFACE);
	  crLightBorder = GetSysColor(COLOR_3DLIGHT);
	  crHilightBorder = GetSysColor(COLOR_3DHILIGHT);
	  crShadowBorder = GetSysColor(COLOR_3DSHADOW);
	  crDkShadowBorder = GetSysColor(COLOR_3DDKSHADOW);

	  pBlackPen = new CPen();
    pBlackPen->CreatePen(PS_SOLID, 1, RGB(0,0,0));

	  iFolderHeight = 22;

	  dwFlags = fDragItems|fEditGroups|fEditItems|fRemoveGroups|fRemoveItems|fAddGroups|fAnimation|fSelHighlight;

	  //iSelFolder = 1;
    iSelFolder = 0;

	  iLastFolderHighlighted = -1;
	  iLastSelectedFolder = -1;
	  
	  pLargeImageList = NULL;
	  pSmallImageList = NULL;

	  iFirstItem = 0;

	  xSmallIconLabelOffset = 4;
	  yLargeIconLabelOffset = 3;

	  ySmallIconSpacing = 10;
	  yLargeIconSpacing = 8;

	  xLeftMargin = 5;
	  yTopMargin = 5;

	  bUpArrow = bDownArrow = FALSE;

	  hHandCursor =::LoadCursor(_Module.GetResourceInstance(),
		  MAKEINTRESOURCE(IDC_HANDCUR));
	  hNormalCursor =::LoadCursor(NULL,
		  MAKEINTRESOURCE(IDC_ARROW));

	  rcUpArrow = rcDownArrow = CRect(0,0,0,0);

	  bUpPressed = bDownPressed = FALSE;

	  bLooping = FALSE;

	  iLastItemHighlighted = -1;
	  bPressedHighlight = FALSE;

	  iLastDragItemDraw = -1;

	  lAnimationTickCount = 10;

	  hDragCursor =::LoadCursor(_Module.GetResourceInstance(),
		  MAKEINTRESOURCE(IDC_DRAGGING)); //AtlLoadCursor(IDC_DRAGGING);
	  hNoDragCursor =::LoadCursor(_Module.GetResourceInstance(),
		  MAKEINTRESOURCE(IDC_NODRAGGING)); //AtlLoadCursor(IDC_NODRAGGING);

	  iLastSel = -1;
	  iSelAnimCount = 0;

	  iSelAnimTiming = 0;
    m_hOwnerWindow = NULL;
  }

	virtual ~COutBarCtrl()
  {
	  if (m_pGroupEdit)
      delete m_pGroupEdit;
    
    CBarFolderList::iterator ListIterator;	
	  for (ListIterator = arFolder.begin(); ListIterator != arFolder.end(); ++ListIterator)        
	  {		
		  CBarFolder* pItem = *ListIterator;
		  if (pItem)
        delete pItem;
    }

    delete pBlackPen;
  }

  HWND m_hOwnerWindow;
  void SetOwner(HWND hWnd) {m_hOwnerWindow = hWnd;}

	enum {  fSmallIcon = 1 << 0, fLargeIcon = 1 << 1, fEditGroups = 1 << 2, fEditItems = 1 << 3,
			fRemoveGroups = 1 << 4, fRemoveItems = 1 << 5, fAddGroups = 1 << 6, 
			fDragItems = 1 << 7, fAnimation = 1 << 8, fSelHighlight = 1 << 9 };

	enum { ircIcon = 1, ircLabel = 2, ircAll = 3 };

  enum { htNothing = -1, htFolder, htItem, htUpScroll, htDownScroll};
  
  int GetFolderCount() const
  {
	  return arFolder.size();
  }

	int GetSelectedFolder()
  {
    return iSelFolder;
  }

  LRESULT OnEndLabelEdit(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  {
	  if (wParam == NM_OB_ONGROUPENDEDIT)
	  {
		  CGroupEdit * pEdit = (CGroupEdit *) lParam;
		  OUTBAR_INFO ob;
		  ob.index = pEdit->iIndex;
		  ob.cText = pEdit->text;

      CWindow wnd(m_hOwnerWindow);
		  if (wnd.SendMessage(WM_OUTBAR_NOTIFY, NM_OB_ONGROUPENDEDIT, (LPARAM) &ob))
		  {
			  SetFolderText(pEdit->iIndex, pEdit->text);
			  CRect rc;
			  GetFolderRect(pEdit->iIndex, rc);
			  InvalidateRect(rc, FALSE);
		  }
	  }
	  if (wParam == NM_OB_ONLABELENDEDIT)
	  {
		  CGroupEdit * pEdit = (CGroupEdit *) lParam;
		  OUTBAR_INFO ob;
		  ob.index = pEdit->iIndex;
		  ob.cText = pEdit->text;

      CWindow wnd(m_hOwnerWindow);
		  if (wnd.SendMessage(WM_OUTBAR_NOTIFY, NM_OB_ONLABELENDEDIT, (LPARAM) &ob))
		  {
			  SetItemText(pEdit->iIndex, pEdit->text);
			  CRect rc;
			  GetInsideRect(rc);
			  InvalidateRect(rc, TRUE);
		  }
	  }
	  return 0;
  }

  CBarFolder* GetBarFolderListItem(int nItem)
  {
	  int nIndex = 0;
    CBarFolderList::iterator ListIterator;	
	  for (ListIterator = arFolder.begin(); ListIterator != arFolder.end(); ++ListIterator)        
    {
      if (nIndex == nItem)
      {
        return *ListIterator;
      }
      nIndex++;
    }

    return NULL;
  }

  CBarItem* GetBarItemListItem(CBarFolder* pbf, int nItem)
  {
	  ATLASSERT(pbf);

    int nIndex = 0;
    CBarItemList::iterator ListIterator;	
	  for (ListIterator = pbf->arItems.begin(); ListIterator != pbf->arItems.end(); ++ListIterator)        
    {
      if (nIndex == nItem)
      {
        return *ListIterator;
      }
      nIndex++;
    }

    return NULL;
  }

  CImageList* GetFolderImageList(const int index, const BOOL bSmall)
  {
	  ATLASSERT(index >= 0 && index < GetFolderCount());
	  CBarFolder* pbf = GetBarFolderListItem(index);
	  if (bSmall)
	  {
		  if (pbf->pSmallImageList) return pbf->pSmallImageList; else return pSmallImageList;
	  }
	  if (pbf->pLargeImageList) return pbf->pLargeImageList; else return pLargeImageList;
  }

  BOOL IsSmallIconView() const
  {
	  return dwFlags&fSmallIcon;
  }

  void GetInsideRect(CRect& rect) const
  {
	  GetClientRect(rect);
	  if (arFolder.size() > 0)
	  {
		  int max = arFolder.size();
		  rect.top += iFolderHeight * (iSelFolder + 1) - 1;//+ 2;
		  rect.bottom -= (max - iSelFolder - 1)*iFolderHeight;
		  return;
	  }
  }

  CSize GetItemSize(const int iFolder, const int index, const int type)
  {
	  ATLASSERT(iFolder >= 0 && iFolder < GetFolderCount());
	  CBarFolder* pbf = GetBarFolderListItem(iFolder);

	  //ATLASSERT(index >= 0 && index < pbf->GetItemCount());
    if (!(index >= 0 && index < pbf->GetItemCount()))
      return CSize(0, 0);

	  CBarItem* pi = GetBarItemListItem(pbf, index);

	  CSize szImage(0,0);
	  CSize szLabel(0,0);
	  CSize szAll(0,0);

	  if (pi->iImageIndex >= 0)
	  {
		  if (type != ircLabel)
		  {
			  CImageList * il = GetFolderImageList(iFolder, IsSmallIconView());
			  ATLASSERT(il);
			  if (il)
			  {
				  IMAGEINFO ii;
				  il->GetImageInfo(pi->iImageIndex, &ii);
				  szImage = CRect(ii.rcImage).Size();
			  }
		  }
	  }
	  if (pi->cItem)
	  {
		  if (type != ircIcon)
		  {
			  CClientDC dc(m_hWnd);
			  HFONT oft = dc.SelectFont((HFONT)GetStockObject(DEFAULT_GUI_FONT));
			  if (IsSmallIconView())
			  {
	        CSize size;
				  szLabel = dc.GetTextExtent(pi->cItem, lstrlen(pi->cItem), &size);
			  }
			  else
			  {
				  CRect rc;
				  GetInsideRect(rc);
				  rc.bottom = rc.top;
				  dc.DrawText(pi->cItem, lstrlen(pi->cItem), rc, DT_CALCRECT|DT_CENTER|DT_WORDBREAK);
				  szLabel = rc.Size();
			  }
			  dc.SelectFont(oft);
		  }
	  }
	  if (IsSmallIconView())
	  {
		  if (type == ircIcon) szAll = szImage;
		  else if (type == ircLabel) szAll = szLabel;
		  else if (type == ircAll) szAll = CSize(szImage.cx + szLabel.cx + xSmallIconLabelOffset, szImage.cy > szLabel.cy ? szImage.cy : szLabel.cy);
	  }
	  else
	  {
		  if (type == ircIcon) szAll = szImage;
		  else if (type == ircLabel) szAll = szLabel;
		  else if (type == ircAll) 
		  {
			  szAll = CSize(szImage.cx > szLabel.cx ? szImage.cx : szLabel.cx, szLabel.cy + szImage.cy + yLargeIconLabelOffset + yLargeIconSpacing);
		  }
	  }
	  return szAll;
  }

  void GetItemRect(const int iFolder, const int iIndex, CRect & rect)
  {
	  CRect rc;
	  GetInsideRect(rc);
	  int top = rc.top;
	  CSize sz(0,0);
	  int y = 0;
	  for (int t = 0; t < iIndex; t++)
	  {
		  sz = GetItemSize(iFolder, t, ircAll);
		  top += sz.cy;

		  if (IsSmallIconView()) 
		  {
			  top += ySmallIconSpacing; 
		  }
		  else 
		  {
			  top += yLargeIconSpacing;
		  }
		  if (t == iFirstItem - 1) y = top - rc.top;
	  }
	  sz = GetItemSize(iFolder, iIndex, ircAll);
	  rect.SetRect(rc.left, top, rc.left + sz.cx, top + sz.cy);

	  rect.top -= y;
	  rect.bottom -= y;

	  rect.left += xLeftMargin;
	  rect.top  += yTopMargin;
	  rect.bottom += yTopMargin;

	  if (!IsSmallIconView()) 
	  {
		  rect.left = rc.left;
		  rect.right = rc.right;
	  }
  }

  void DrawAnimItem(const int xoffset, const int yoffset, const int index)
  {
	  CImageList* ima = GetFolderImageList(iSelFolder, IsSmallIconView());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -