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

📄 imagelistbox2.h

📁 WTL框架实现的ImageListBox
💻 H
📖 第 1 页 / 共 2 页
字号:
#if !defined(AFX_IMAGELISTBOX_H__20040606_AF04_B15C_D1F1_0080AD509054__INCLUDED_)
#define AFX_IMAGELISTBOX_H__20040606_AF04_B15C_D1F1_0080AD509054__INCLUDED_

#pragma once

/////////////////////////////////////////////////////////////////////////////
// CImageListBox - A ListBox control with that extra touch
//
// For this control to work, you must add the
//   REFLECT_NOTIFICATIONS()
// macro to the parent's message map.
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2004 Bjarke Viksoe.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//

#ifndef __cplusplus
  #error WTL requires C++ compilation (use a .cpp suffix)
#endif

#ifndef __ATLAPP_H__
  #error ImageListBox.h requires atlapp.h to be included first
#endif

#ifndef __ATLCTRLS_H__
  #error ImageListBox.h requires atlctrls.h to be included first
#endif


// ImageListBox mask flags
#define ILBIF_TEXT               0x0001
#define ILBIF_SUBTEXT            0x0002
#define ILBIF_IMAGE              0x0004
#define ILBIF_SELIMAGE           0x0008
#define ILBIF_STYLE              0x0010
#define ILBIF_FORMAT             0x0020
#define ILBIF_PARAM              0x0040

// ImageListBox styles
#define ILBS_IMGLEFT             0x0000
#define ILBS_IMGRIGHT            0x0001
#define ILBS_IMGTOP              0x0002
#define ILBS_SELNORMAL           0x0000
#define ILBS_SELROUND            0x0010


// ImageListBox item structure
typedef struct
{
   int iItem;           // index
   UINT mask;           // mask (ILBIF_xxx flags)
   UINT style;          // item styles (ILBS_xxx flags)
   UINT format;         // text format (DT_xxx flags)
   LPTSTR pszText;      // title text
   int cchMaxText;
   LPTSTR pszSubText;   // subtext
   int cchMaxSubText;
   int iImage;          // image
   int iSelImage;       // selected image
   LPARAM lParam;       // user-defined parameter
} ILBITEM, *PILBITEM;

// ImageListBox ImageList constants
#define ILSIL_NORMAL    0
#define ILSIL_SELECTED  1

// ImageListBox settings
typedef struct
{
   COLORREF clrText;
   COLORREF clrBackground;
   COLORREF clrHighlite;
   COLORREF clrHighliteText;
   COLORREF clrHighliteBorder;
   SIZE sizeMargin;
   SIZE sizeIndent;
   SIZE sizeSubIndent;
   POINT ptArc;
} ILBSETTINGS, *PILBSETTINGS;


template< class T, class TBase = CListViewCtrl, class TWinTraits = CControlWinTraits >

class ATL_NO_VTABLE CImageListBoxImpl : 
   public CWindowImpl< T, TBase, TWinTraits >,
   public CCustomDraw< CImageListBoxImpl >
{
public:
   DECLARE_WND_SUPERCLASS(NULL, TBase::GetWndClassName())

   CImageListBoxImpl() : m_hMainFont(NULL), m_hSubFont(NULL)
   { 
      ::ZeroMemory(&m_st, sizeof(m_st));      
      m_st.ptArc.x = m_st.ptArc.y = 6;
   }

   ILBSETTINGS m_st;
   CImageList m_imgNormal;
   CImageList m_imgSelected;
   CFont m_fontScale;
   HFONT m_hMainFont;
   HFONT m_hSubFont;

   // Operations

   BOOL SubclassWindow(HWND hWnd)
   {
      ATLASSERT(m_hWnd==NULL);
      ATLASSERT(::IsWindow(hWnd));
#ifdef _DEBUG
      TCHAR szBuffer[20] = { 0 };
      if( ::GetClassName(hWnd, szBuffer, 19) ) {
         ATLASSERT(::lstrcmpi(szBuffer, TBase::GetWndClassName())==0);
      }
#endif
      BOOL bRet = CWindowImpl< T, TBase, TWinTraits >::SubclassWindow(hWnd);
      if( bRet ) _Init();
      return bRet;
   }

   void SetItemData(int /*nIndex*/, LPARAM /*lParam*/)
   {
      ATLASSERT(false);
   }

   int InsertItem(const ILBITEM* pItem)
   {
      ATLASSERT(::IsWindow(m_hWnd));
      // Create a copy of the ITEM structure
      PILBITEM pNewItem;      
      ATLTRY(pNewItem = new ILBITEM);
      ATLASSERT(pNewItem);
      ::ZeroMemory(pNewItem, sizeof(ILBITEM));
      UINT mask = pItem->mask;
      pNewItem->mask = mask;
      if( mask & ILBIF_TEXT ) {
         ATLTRY(pNewItem->pszText = new TCHAR[ lstrlen(pItem->pszText) + 1 ] );
         ::lstrcpy( pNewItem->pszText, pItem->pszText );
      }
      if( mask & ILBIF_SUBTEXT ) {
         ATLTRY(pNewItem->pszSubText = new TCHAR[ lstrlen(pItem->pszSubText ) + 1 ] );
         ::lstrcpy( pNewItem->pszSubText, pItem->pszSubText );
      }
      if( mask & ILBIF_STYLE ) pNewItem->style = pItem->style;
      if( mask & ILBIF_FORMAT ) pNewItem->format = pItem->format;
      if( mask & ILBIF_IMAGE ) pNewItem->iImage = pItem->iImage;
      if( mask & ILBIF_SELIMAGE ) pNewItem->iSelImage = pItem->iSelImage;
      if( mask & ILBIF_PARAM ) pNewItem->lParam = pItem->lParam;
      // Add item to listbox
      int iItem = TBase::InsertItem(pItem->iItem, pNewItem->pszText);
      if( iItem >= 0 ) {
         TBase::SetItemData(iItem, (LPARAM) pNewItem);
      }
      return iItem;
   }
   BOOL GetItem(ILBITEM *pItem) const
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(pItem);
      PILBITEM pOrgItem = (PILBITEM) TBase::GetItemData(pItem->iItem);
      if( pOrgItem == NULL ) return FALSE;
      // Copy attributes
      UINT mask = pItem->mask;
      if( mask & ILBIF_TEXT ) ::lstrcpyn( pItem->pszText, pOrgItem->pszText, pItem->cchMaxText );
      if( mask & ILBIF_SUBTEXT ) ::lstrcpyn( pItem->pszSubText, pOrgItem->pszSubText, pItem->cchMaxSubText );
      if( mask & ILBIF_STYLE ) pItem->style = pOrgItem->style;
      if( mask & ILBIF_FORMAT ) pItem->format = pOrgItem->format;
      if( mask & ILBIF_IMAGE ) pItem->iImage = pOrgItem->iImage;
      if( mask & ILBIF_SELIMAGE ) pItem->iSelImage = pOrgItem->iSelImage;
      if( mask & ILBIF_PARAM ) pItem->lParam = pOrgItem->lParam;
      return TRUE;
   }
   BOOL SetItem(const ILBITEM *pItem)
   {
      ATLASSERT(::IsWindow(m_hWnd));
      ATLASSERT(pItem);     
      // Get original item data and set attributes
      PILBITEM pOrgItem = (PILBITEM) TBase::GetItemData(pItem->iItem);
      if( pOrgItem == NULL ) return FALSE;
      UINT mask = pItem->mask;
      if( mask & ILBIF_TEXT ) {
         if( pOrgItem->mask & ILBIF_TEXT ) delete [] pOrgItem->pszText;
         ATLTRY(pOrgItem->pszText = new TCHAR[ lstrlen(pItem->pszText) + 1 ]);
         ::lstrcpy( pOrgItem->pszText, pItem->pszText );
      }
      if( mask & ILBIF_SUBTEXT ) {
         if( pOrgItem->mask & ILBIF_SUBTEXT ) delete [] pOrgItem->pszSubText;
         ATLTRY( pOrgItem->pszSubText = new TCHAR[ lstrlen(pItem->pszSubText) + 1] );
         ::lstrcpy( pOrgItem->pszSubText, pItem->pszSubText );
      }
      if( mask & ILBIF_STYLE ) pOrgItem->style = pItem->style;
      if( mask & ILBIF_FORMAT ) pOrgItem->format = pItem->format;
      if( mask & ILBIF_IMAGE ) pOrgItem->iImage = pItem->iImage;
      if( mask & ILBIF_SELIMAGE ) pOrgItem->iSelImage = pItem->iSelImage;
      if( mask & ILBIF_PARAM ) pOrgItem->lParam = pItem->lParam;
      pOrgItem->mask |= pItem->mask;
      // Repaint item
      RECT rc;
      GetItemRect(pItem->iItem, &rc, LVIR_BOUNDS);
      InvalidateRect(&rc, TRUE);
      return TRUE;
   }
   CImageList SetImageList(HIMAGELIST hImageList, int nImageList)
   {
      HIMAGELIST hOldList = NULL;
      switch( nImageList ) {
      case ILSIL_NORMAL:
         hOldList = m_imgNormal;
         m_imgNormal = hImageList;
         TBase::SetImageList(hImageList, LVSIL_SMALL);
         break;
      case ILSIL_SELECTED:
         hOldList = m_imgSelected;
         m_imgSelected = hImageList;
         break;
      default:
         ATLASSERT(false);
      }    
      return hOldList;
   }
   HFONT SetFont(HFONT hFont)

⌨️ 快捷键说明

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