一个MFC类的子类,方便大家使用,画的不好,希望见量,LIST的样子和OD差不多,很多东西都可以实现了,比起原版的要强大些,实际一个不难的东西,google找到的X站,是需要会员可以下的,会员注册是需要RMB的,无法偷懒,只有自己动手。
头文件:
#if !defined(AFX_MGLISTCTRLBASE_H__5688F862_FD2E_42A5_9999_9369060BD7AE__INCLUDED_)
#define AFX_MGLISTCTRLBASE_H__5688F862_FD2E_42A5_9999_9369060BD7AE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MgListCtrlBase.h : header file
//
//--------------------------------------------------------------------------------------------------------------------------
//关键重画部分已经使用类虚拟函数,如果需要那部分重画,直接子类化重载,将绘画部分和重载分开,方便做默认处理^^.
//LVS_OWNERDATA属性似乎无法用代码开启,似乎是在控件创建时设定,但是当在对话框初始化函数中时,控件已经创建完成,
//所以,在使用的时候需要手动开启CListCtrl控件的该属性.
//如果是VC6,需要开启:
#pragma warning(disable:4786) //需要注释掉warning,std::map 模板版本兼容问题,存在警告~!
#include <map>
#include <vector>
#include <algorithm>
//--------------------------------------------------------------------------------------------------------------------------
//常量声明:
#define _MGLCB_FONTNML_HEIGHT_ 12
//--------------------------------------------------------------------------------------------------------------------------
//类声明:
class CMgListCtrlBaseInfo
{
public:
CMgListCtrlBaseInfo();
virtual ~CMgListCtrlBaseInfo();
public:
typedef struct _tagItemInfo_
{
int nIndex; //列号
int nTextLen; //字符串长度
CString strText; //字符串
}_T_ITEMINFO_;
protected:
//由原来的map改为vector,提高速度:
//读取临时使用at,设置为清除插入方法.
std::vector< _T_ITEMINFO_ > m_vtrItemInfo;
private:
int m_nIndex;
public:
int ClearItemInfo();
//
int GetIndex();
int SetIndex( int nIndex );
//
int SetItemText( int nColumn , LPCTSTR pszText );
int GetItemText( int nColumn , LPTSTR pszText , int nTextLen );
CString GetItemText( int nColumn );
};
//--------------------------------------------------------------------------------------------------------------------------
//类声明:
/////////////////////////////////////////////////////////////////////////////
// CMgListCtrlBase window
class CMgListCtrlBase : public CListCtrl
{
// Construction
public:
CMgListCtrlBase();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMgListCtrlBase)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMgListCtrlBase();
// Generated message map functions
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//{{AFX_MSG(CMgListCtrlBase)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
virtual void DrawItemFocus( int nType ,CDC * pDC , UINT nItemID );
virtual void DrawItemSelect( CDC * pDC , UINT nItemID );
virtual void DrawCellText( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect );
virtual void DrawCellState( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect );
virtual void DrawCustomInfo( CDC * pDC , UINT nItemID );
protected:
void DrawItemBase(LPDRAWITEMSTRUCT lpDrawItemStruct);
void DrawItemMainBase( CDC * pDC , UINT nItemID , int nType );
int DrawItemFocusBase( int nType , CDC * pDC , UINT nItemID );
int DrawItemSelectBase( CDC * pDC , UINT nItemID );
int DrawGridLineBase( CDC * pDC , UINT nItemID );
int DrawCellBase( CDC * pDC , UINT nItemID );
int DrawCellTextBase( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect );
int DrawCellStateBase( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect );
//-----------------------------------------------------------------------------
//重载CListCtrl类:枚举声明
public:
typedef enum _enum_ListCtrlStyle_
{
_E_LCS_SmallIcon_ = LVS_SMALLICON,
_E_LCS_Icon_ = LVS_ICON,
_E_LCS_List_ = LVS_LIST,
_E_LCS_Report_ = LVS_REPORT,
_E_LCS_SelAlways_ = LVS_SHOWSELALWAYS,
_E_LCS_ReportAndShowSelAlways_ = LVS_REPORT | LVS_SHOWSELALWAYS
}_enum_LCS_;
typedef enum _enum_ListItemState_
{
_E_LIS_Normal_ = 0,
_E_LIS_Default_ = 0,
_E_LIS_Select_ = 1,
_E_LIS_Focus_ = 2,
_E_LIS_SelAndFocus_ = _E_LIS_Select_ | _E_LIS_Focus_
}_enum_LIS_;
typedef enum _enum_ListGridLine_
{
_E_LGL_Normal_ = 0,
_E_LGL_Default_ = 0,
_E_LGL_VLine_ = 1,
_E_LGL_HLine_ = 2,
_E_LGL_HVLine_ = _E_LGL_VLine_ | _E_LGL_HLine_
}_enum_LGL_;
//-----------------------------------------------------------------------------
//重载CListCtrl类:结构声明
public:
typedef struct _tagListCellState_
{
int nItem;
int nSubItem;
COLORREF clrBk;
COLORREF clrTxt;
}_T_LCS_;
//-----------------------------------------------------------------------------
//重载CListCtrl类:变量声明
public:
//默认颜色:
COLORREF m_clrNmlBackGround;
COLORREF m_clrNmlTxtBackGround;
COLORREF m_clrNmlTxt;
//焦点和网格颜色:
//焦点:0不选择,1选择
//网格:0是纵向,1是横向
COLORREF m_clrFocusLine[2];
COLORREF m_clrGridLine[2];
//选择颜色
COLORREF m_clrSelBackGround;
COLORREF m_clrSelTxt;
//单元格颜色:
std::map<int,_T_LCS_> m_mapListCellState;
std::map<int,_T_LCS_> m_mapListFullCellState;
//多数据量处理信息:
std::map<int,CMgListCtrlBaseInfo> m_mapListInfo;
public:
//项目高度:默认为字体高度 * 78 / 72
int m_nItemHeight;
//列表框全部字体:
CFont m_fontAllItemFont;
//是否重画焦点线框:
BOOL m_bDrawFocusLine;
//网格线状态:
int m_nGridLineState;
//是否显示项目:
BOOL m_bYesNoShowItem;
//是否按大项目来处理处理
BOOL m_bYesNoOwnerData;
//-----------------------------------------------------------------------------
//重载CListCtrl类:函数声明
public:
//-----------------------------------------------
//GetFullCellRect : 列表控件整行矩形
//-----------------------------------------------
int GetFullCellRect( int nItem , LPRECT lpRect );
//-----------------------------------------------
//GetCellRect : 列表控件单元格矩形
//-----------------------------------------------
int GetCellRect( int nItem , int nSubItem , LPRECT lpRect );
//-----------------------------------------------
//SetSelItem: 设置列表控件选择项目
//-----------------------------------------------
int SetSelItem( int nIndex );
int SetSelItemAndFocus( int nIndex );
//-----------------------------------------------
//SetTxtFont : 设置列表控件纯文本字体
//-----------------------------------------------
BOOL SetAllTxtFont( int nFontSize , char * szFontName = _T("宋体"), BOOL bRedraw = TRUE );
CFont * GetAllTxtFont( );
//-----------------------------------------------
//ClearAllItems : 清空所有项目
//-----------------------------------------------
int ClearAllItems( );
public:
//-----------------------------------------------
//SetStyle : 设置列表控件风格
//-----------------------------------------------
int SetStyle( _enum_LCS_ lcs /*= _ENUM_LCS_Report_*/ );
int SetStyle( DWORD dwStyle = LVS_REPORT );
//-----------------------------------------------
//SetExStyle : 设置列表控件扩展风格
//-----------------------------------------------
int SetExStyle( DWORD dwStyle = LVS_EX_FULLROWSELECT );
//-----------------------------------------------
//SetShowItem : 是否显示项目(可以选择否,等添加完成项目,再显示,提高速率.)
//-----------------------------------------------
BOOL SetShowItem( BOOL bShow = TRUE );
//-----------------------------------------------
//SetOwnerDraw : 设置列表控件是否重画
//-----------------------------------------------
int SetOwnerDraw( BOOL bDraw = FALSE );
//-----------------------------------------------
//SetOwnerData : 设置列表控件是否按大数据量来处理项目,默认否.
//-----------------------------------------------
int SetOwnerData( BOOL bOwnerData = FALSE );
//-----------------------------------------------
//SetShowFocus : 是否显示焦点线框
//-----------------------------------------------
BOOL SetShowFocus( BOOL bShow = TRUE , COLORREF clrNml = RGB(0,0,0) , COLORREF clrSel = RGB(255, 255, 255) );
//-----------------------------------------------
//SetGridLineState : 设置网格线
//-----------------------------------------------
void SetGridLineState( int nMgState = -1 , int nNmlState = -1 ,
COLORREF clrV = RGB( 150, 150, 150 ) , COLORREF clrH = RGB(150, 150, 150 ) );
//-----------------------------------------------
//SetItemHeight : 设置列表控件项目高度
//-----------------------------------------------
void SetItemHeight( int nHeight );
public:
//-----------------------------------------------
//SetNmlColor : 设置列表控件默认背景
//-----------------------------------------------
BOOL SetNmlBkColor( COLORREF color );
COLORREF GetNmlBkColor( );
//-----------------------------------------------
//SetTxtBkColor : 设置列表控件纯文本背景
//-----------------------------------------------
BOOL SetNmlTxtBkColor( COLORREF color );
COLORREF GetNmlTxtBkColor( );
//-----------------------------------------------
//SetTxtColor : 设置列表控件纯文本颜色
//-----------------------------------------------
BOOL SetNmlTxtColor( COLORREF color );
COLORREF GetNmlTxtColor( );
//-----------------------------------------------
//SetSelBkColor : 设置列表控件选择时背景
//-----------------------------------------------
BOOL SetSelBkColor( COLORREF color );
COLORREF GetSelBkColor( );
//-----------------------------------------------
//SetSelTxtColor : 设置列表控件选择时文本颜色
//-----------------------------------------------
BOOL SetSelTxtColor( COLORREF color );
COLORREF GetSelTxtColor( );
//-----------------------------------------------
//SetCellColor : 设置单元格颜色
//-----------------------------------------------
void SetCellColor( int nItem , int nColumn , COLORREF clrBackground = (COLORREF)-1, COLORREF clrTxt =(COLORREF)-1 );
void DelCellColor( int nItem , int nColumn );
//-----------------------------------------------
//SetFullCellColor : 设置整行颜色
//-----------------------------------------------
void SetFullCellColor( int nItem , COLORREF clrBackground = (COLORREF)-1, COLORREF clrTxt =(COLORREF)-1 );
void DelFullCellColor( int nItem );
//-----------------------------------------------------------------------------
//重载CListCtrl类:大数据项目相关函数声明:
protected:
virtual void vl_DrawCellText( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect );
protected:
int vl_DrawCellTextBase( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect );
public:
//-----------------------------------------------
//vl_InsertItem : 向列表控件插入项目
//-----------------------------------------------
int vl_InsertItem( int nItem , LPCTSTR strItem , BOOL bUpdate = TRUE );
//-----------------------------------------------
//vl_DeleteItem : 删除列表控件指定项目
//-----------------------------------------------
int vl_DeleteItem( int nItem );
int vl_DeleteAllItems( );
//-----------------------------------------------
//vl_SetItemText : 设置列表控件指定项目文本
//-----------------------------------------------
int vl_SetItemText( int nItem, int nColumn , LPCTSTR pszItem );
//-----------------------------------------------
//vl_GetItemText : 获取列表控件指定项目文本
//-----------------------------------------------
int vl_GetItemText( int nItem , int nColumn , LPTSTR pszItem , int nItemLen );
CString vl_GetItemText( int nItem , int nColumn );
};
//--------------------------------------------------------------------------------------------------------------------------
//
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MGLISTCTRLBASE_H__5688F862_FD2E_42A5_9999_9369060BD7AE__INCLUDED_)
代码文件
// MgListCtrlBase.cpp : implementation file
//
#include "stdafx.h"
#include "MgListCtrlBase.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//---------------------------------------------------------------------------------------------------------------------------
// ----- 构造函数 ------
//---------------------------------------------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CMgListCtrlBaseInfo
CMgListCtrlBaseInfo::CMgListCtrlBaseInfo()
{
ClearItemInfo();
}
CMgListCtrlBaseInfo::~CMgListCtrlBaseInfo()
{
//
}
//-----------------------------------------------------------------------
//清除处理:
//
//-----------------------------------------------------------------------
int CMgListCtrlBaseInfo::ClearItemInfo()
{
//
m_nIndex = -1;
//
m_vtrItemInfo.clear();
return 0;
}
//-----------------------------------------------------------------------
//索引处理:
//
//-----------------------------------------------------------------------
int CMgListCtrlBaseInfo::GetIndex()
{
return m_nIndex;
}
int CMgListCtrlBaseInfo::SetIndex( int nIndex )
{
int nResult = m_nIndex ;
m_nIndex = nIndex ;
return nResult;
}
//-----------------------------------------------------------------------
//设置项目文本处理:
//
//-----------------------------------------------------------------------
int CMgListCtrlBaseInfo::SetItemText( int nColumn , LPCTSTR pszText )
{
if( nColumn < 0 || pszText == NULL ){ return -1; }
_T_ITEMINFO_ info;
info.nIndex = nColumn;
info.nTextLen= strlen(pszText);
info.strText = pszText;
//先清除元素,再插入:
//这个方法不大妥当:m_vtrItemInfo[nColumn] = info;
if( m_vtrItemInfo.end() > m_vtrItemInfo.begin() + nColumn )
{
m_vtrItemInfo.erase( m_vtrItemInfo.begin() + nColumn );
}
if( m_vtrItemInfo.size() < nColumn ){ return -1; }
m_vtrItemInfo.insert( m_vtrItemInfo.begin() + nColumn , info );
return nColumn;
}
//-----------------------------------------------------------------------
//获取项目文本处理:
//
//-----------------------------------------------------------------------
int CMgListCtrlBaseInfo::GetItemText( int nColumn , LPTSTR pszText , int nTextLen )
{
if( nColumn < 0 || pszText == NULL ){ return 0; }
if( m_vtrItemInfo.size() <= nColumn ){ pszText[0] = '\0'; return 0; }
//
int nLen = m_vtrItemInfo.at( nColumn ).nTextLen;
if( nLen >= nTextLen )
{
memcpy( pszText , m_vtrItemInfo.at( nColumn ).strText , nTextLen -1 );
pszText[nTextLen - 1] = '\0';
}else{
strcpy( pszText , m_vtrItemInfo.at( nColumn ).strText );
}
return nLen;
}
CString CMgListCtrlBaseInfo::GetItemText( int nColumn )
{
if( nColumn < 0 ){ return _T(""); }
if( m_vtrItemInfo.size() <= nColumn ){ return _T(""); }
CString str = m_vtrItemInfo.at( nColumn ).strText ;
return str;
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 构造函数 ------
//---------------------------------------------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CMgListCtrlBase
CMgListCtrlBase::CMgListCtrlBase()
{
//
m_clrNmlBackGround = ::GetSysColor(COLOR_BACKGROUND) ; //获取背景
m_clrNmlTxtBackGround = m_clrNmlBackGround;
m_clrNmlTxt = RGB( 0, 0, 0 ); //黑色
m_nItemHeight = _MGLCB_FONTNML_HEIGHT_ ;
m_bDrawFocusLine = TRUE;
m_clrFocusLine[0] = RGB( 0 , 0 , 0 );
m_clrFocusLine[1] = RGB( 255 , 255 , 255 );
//
m_clrSelBackGround = RGB( 200, 200, 200 ); //选择时背景为灰色
m_clrSelTxt = RGB( 255, 255, 255 ); //文字为白色
m_nGridLineState = 0;
//
m_bYesNoShowItem = TRUE;
m_bYesNoOwnerData = FALSE; //默认不按多数据量来处理数据
//
m_mapListCellState.clear();
m_mapListFullCellState.clear();
m_mapListInfo.clear();
}
CMgListCtrlBase::~CMgListCtrlBase()
{
}
BEGIN_MESSAGE_MAP(CMgListCtrlBase, CListCtrl)
//{{AFX_MSG_MAP(CMgListCtrlBase)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_MOUSEMOVE()
ON_WM_MEASUREITEM_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//---------------------------------------------------------------------------------------------------------------------------
// ----- 消息函数 ------
//---------------------------------------------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CMgListCtrlBase message handlers
//--------------------------------------------------------
// WM_MEASUREITEM_REFLECT :
//--------------------------------------------------------
void CMgListCtrlBase::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if( m_nItemHeight > 0 )
{
lpMeasureItemStruct->itemHeight = m_nItemHeight;
}
}
//--------------------------------------------------------
// WM_MOUSEMOVE :
//--------------------------------------------------------
void CMgListCtrlBase::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( GetAsyncKeyState(VK_LBUTTON) )
{
//判断选择多行
int nMark = this->GetSelectionMark();
int nCount = this->GetSelectedCount();
//
RECT rtItem;
GetItemRect( nMark , &rtItem , LVIR_BOUNDS );
//
if( rtItem.top > point.y )
{
int nSelCount = (rtItem.top - point.y) / m_nItemHeight ;
if( (( point.y - rtItem.top ) % m_nItemHeight) > 0 )
{
nSelCount += 1;
}
for( int m = 0 ; m <= nSelCount ; m ++ )
{
if( nMark < 0 )
{
break;
}
SetSelItem( nMark );
nMark -- ;
}
}else if( rtItem.bottom < point.y )
{
int nSelCount = ( point.y - rtItem.bottom ) / m_nItemHeight ;
if( (( point.y - rtItem.bottom ) % m_nItemHeight) > 0 )
{
nSelCount += 1;
}
for( int m = 0 ; m <= nSelCount ; m ++ )
{
if( nMark < 0 )
{
break;
}
SetSelItem( nMark );
nMark ++ ;
}
}
}
CListCtrl::OnMouseMove(nFlags, point);
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 常规函数 ------
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------
//GetFullCellRect : 列表控件整行矩形
//-----------------------------------------------
int CMgListCtrlBase::GetFullCellRect( int nItem , LPRECT lpRect )
{
this->GetItemRect( nItem, lpRect , LVIR_BOUNDS );
return 0;
}
//-----------------------------------------------
//GetCellRect : 列表控件单元格矩形
//-----------------------------------------------
int CMgListCtrlBase::GetCellRect( int nItem , int nSubItem , LPRECT lpRect )
{
CRect rect;
this->GetSubItemRect( nItem , nSubItem , LVIR_BOUNDS ,rect );
lpRect->left = rect.left;
lpRect->right= rect.right;
lpRect->top = rect.top;
lpRect->bottom= rect.bottom;
//
if( nSubItem == 0 )
{
int nW = this->GetColumnWidth( 0 );
lpRect->right= rect.left + nW;
}
return 0;
}
//-----------------------------------------------
//SetSelItem: 设置列表控件选择项目
//-----------------------------------------------
int CMgListCtrlBase::SetSelItem( int nIndex )
{
if( nIndex >= 0 )
{
//
this->SetSelectionMark( nIndex );
this->SetItemState( nIndex , LVNI_SELECTED , LVNI_SELECTED );
return nIndex;
}
return -1;
}
//-----------------------------------------------
//SetSelItemAndFocus: 设置列表控件选择并焦点项目
//-----------------------------------------------
int CMgListCtrlBase::SetSelItemAndFocus( int nIndex )
{
if( nIndex >= 0 )
{
//
this->SetSelectionMark( nIndex );
this->SetItemState( nIndex , LVNI_SELECTED | LVNI_FOCUSED , LVNI_SELECTED | LVNI_FOCUSED );
return nIndex;
}
return -1;
}
//-----------------------------------------------
//SetAllTxtFont : 设置列表控件纯文本字体
//-----------------------------------------------
BOOL CMgListCtrlBase::SetAllTxtFont( int nFontSize , char * szFontName , BOOL bRedraw )
{
if( nFontSize <= 0 ){ return FALSE ; }
if( szFontName == NULL ){ return FALSE ; }
m_fontAllItemFont.DeleteObject();
//
SetItemHeight( nFontSize * 78 / 72 ); //GetDeviceCaps( GetDC()->m_hDC ,LOGPIXELSY) / 72 );
//
BOOL bResult = m_fontAllItemFont.CreateFont( nFontSize, //这个数字就是字体大小了
0,0,0,FW_NORMAL,FALSE,FALSE,0,
GB2312_CHARSET,OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,
DRAFT_QUALITY,VARIABLE_PITCH,szFontName );
this->SetFont( &m_fontAllItemFont , bRedraw );
return bResult;
}
//-----------------------------------------------
//GetAllTxtFont : 获取列表控件纯文本字体
//-----------------------------------------------
CFont * CMgListCtrlBase::GetAllTxtFont( )
{
return &m_fontAllItemFont;
}
//-----------------------------------------------
//ClearAllItems : 清空所有项目
//-----------------------------------------------
int CMgListCtrlBase::ClearAllItems( )
{
//
m_mapListCellState.clear();
m_mapListFullCellState.clear();
//
this->DeleteAllItems();
return 0;
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 重载函数 ------
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------
//DrawItem : 重画列表控件
//-----------------------------------------------
void CMgListCtrlBase::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
DrawItemBase( lpDrawItemStruct );
}
//-----------------------------------------------
//DrawItemFocus : 重画列表控件焦点
//-----------------------------------------------
void CMgListCtrlBase::DrawItemFocus( int nType , CDC * pDC , UINT nItemID )
{
DrawItemFocusBase( nType , pDC , nItemID );
}
//-----------------------------------------------
//DrawItemSelect : 重画列表控件选择
//-----------------------------------------------
void CMgListCtrlBase::DrawItemSelect( CDC * pDC , UINT nItemID )
{
DrawItemSelectBase( pDC , nItemID );
}
//-----------------------------------------------
//DrawCellText : 重画列表控件单元格文本
//-----------------------------------------------
void CMgListCtrlBase::DrawCellText( CDC * pDC , UINT nItemID , int nColumn ,LPRECT pRect )
{
DrawCellTextBase( pDC , nItemID , nColumn ,pRect );
}
//-----------------------------------------------
//DrawCellState : 重画列表控件单元格状态
//-----------------------------------------------
void CMgListCtrlBase::DrawCellState( CDC * pDC , UINT nItemID , int nColumn ,LPRECT pRect )
{
DrawCellStateBase( pDC , nItemID , nColumn , pRect );
}
//-----------------------------------------------
//DrawCustomInfo : 重画列表控件自定义
//-----------------------------------------------
void CMgListCtrlBase::DrawCustomInfo( CDC * pDC , UINT nItemID )
{
//该函数为虚函数,是用来绘画自定义数据,改函数需要子类化重载:
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 重绘基础函数 ------
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------
//DrawItemBase : 重画列表控件基本函数
//-----------------------------------------------
void CMgListCtrlBase::DrawItemBase(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if( !lpDrawItemStruct ){ return; }
if( !m_bYesNoShowItem ){ return; }
//
CDC * pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
int nSaveDC = pDC->SaveDC();
//
if( m_nItemHeight <= (lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top) )
{
m_nItemHeight = lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top;
}
//
LV_ITEM lvi;
lvi.mask = LVIF_IMAGE | LVIF_STATE;
lvi.iItem = lpDrawItemStruct->itemID;
lvi.iSubItem = 0;
lvi.stateMask = 0xFFFF; // get all state flags
GetItem(&lvi);
//
_enum_LIS_ lis = _E_LIS_Normal_;
//
switch( lvi.state )
{
case LVIS_FOCUSED : //焦点
{
lis = _E_LIS_Focus_;
break;
}
case LVIS_SELECTED: //选择
{
lis = _E_LIS_Select_;
break;
}
case LVIS_FOCUSED | LVIS_SELECTED : //焦点并选择
{
lis = _E_LIS_SelAndFocus_;
break;
}
default:
{
lis = _E_LIS_Default_;
break;
}
}
//
DrawItemMainBase( pDC , lpDrawItemStruct->itemID , lis );
//
pDC->RestoreDC( nSaveDC );
}
//-----------------------------------------------
//DrawItemMainBase : 重画列表控件主要基本函数
//-----------------------------------------------
void CMgListCtrlBase::DrawItemMainBase( CDC * pDC , UINT nItemID , int nType )
{
//重画背景覆盖残留:
RECT rtItem;
GetFullCellRect( nItemID , &rtItem );
pDC->FillSolidRect( &rtItem , m_clrNmlBackGround );
//画当前状态:
if( nType == _E_LIS_Focus_ )
{
DrawItemFocus( 0 , pDC , nItemID );
}
if( nType == _E_LIS_Select_ )
{
DrawItemSelect( pDC , nItemID );
}
if( nType == _E_LIS_SelAndFocus_ )
{
DrawItemSelect( pDC , nItemID );
DrawItemFocus( 1 , pDC , nItemID );
}
//画网格线:
DrawGridLineBase( pDC , nItemID );
//画单元格:
DrawCellBase( pDC , nItemID );
//自定义绘画:
DrawCustomInfo( pDC , nItemID );
}
//-----------------------------------------------
//DrawItemFocusBase : 重画列表控件焦点基本函数,
//分选择和非选择焦点
//-----------------------------------------------
int CMgListCtrlBase::DrawItemFocusBase( int nType , CDC * pDC , UINT nItemID )
{
if( pDC == NULL ){ return -1; }
if( m_bDrawFocusLine == FALSE ){ return 0 ; }
//
RECT rtItem;
GetFullCellRect( nItemID , &rtItem );
//画焦点框
CPen penNew;
CPen *penOld = NULL;
CGdiObject *pOldBrush = NULL;
if( nType == 1 )
{
//
penNew.CreatePen( PS_DOT, 0 , m_clrFocusLine[1] ); //创建白色的....线:
penOld = pDC->SelectObject( &penNew );
pOldBrush=pDC->SelectStockObject( NULL_BRUSH );
//
pDC->Rectangle( &rtItem );
//
pDC->SelectObject( penOld );
pDC->SelectObject( pOldBrush );
penNew.DeleteObject();
}else{
penNew.CreatePen( PS_DOT, 0 , m_clrFocusLine[0] ); //创建白色的....线:
penOld = pDC->SelectObject( &penNew );
pOldBrush=pDC->SelectStockObject( NULL_BRUSH );
//
pDC->Rectangle( &rtItem );
//
pDC->SelectObject( penOld );
pDC->SelectObject( pOldBrush );
penNew.DeleteObject();
}
return 1;
}
//-----------------------------------------------
//DrawItemSelectBase : 重画列表控件选择基本函数
//-----------------------------------------------
int CMgListCtrlBase::DrawItemSelectBase( CDC * pDC , UINT nItemID )
{
if( pDC == NULL ){ return -1; }
RECT rtItem;
this->GetFullCellRect( nItemID , &rtItem );
pDC->FillSolidRect( &rtItem , m_clrSelBackGround );
return 1;
}
//-----------------------------------------------
//DrawGridLineBase : 重画列表控件网格线基本函数
//-----------------------------------------------
int CMgListCtrlBase::DrawGridLineBase( CDC * pDC , UINT nItemID )
{
if( m_nGridLineState == _E_LGL_VLine_ || m_nGridLineState == _E_LGL_HVLine_ )
{
RECT rtClient;
this->GetClientRect( &rtClient );
CHeaderCtrl *pHeader = this->GetHeaderCtrl();
int nCount = pHeader->GetItemCount();
CPen penNew;
CPen *penOld = NULL;
CGdiObject *pOldBrush = NULL;
penNew.CreatePen( PS_SOLID, 1 , m_clrGridLine[0] );
penOld = pDC->SelectObject( &penNew );
pOldBrush=pDC->SelectStockObject( NULL_BRUSH );
for( int n = 0 ; n < ( nCount - 1 ); n ++ )
{
RECT rect;
this->GetCellRect( nItemID , n , &rect );
pDC->MoveTo( rect.right - 1, rtClient.top );
pDC->LineTo( rect.right - 1, rtClient.bottom );
}
//
pDC->SelectObject( penOld );
pDC->SelectObject( pOldBrush );
penNew.DeleteObject();
}
if( m_nGridLineState == _E_LGL_HLine_ || m_nGridLineState == _E_LGL_HVLine_ )
{
CPen penNew;
CPen *penOld = NULL;
CGdiObject *pOldBrush = NULL;
penNew.CreatePen( PS_SOLID, 1 , m_clrGridLine[1] );
penOld = pDC->SelectObject( &penNew );
pOldBrush=pDC->SelectStockObject( NULL_BRUSH );
RECT rect;
this->GetFullCellRect( nItemID , &rect );
pDC->MoveTo( rect.left, rect.bottom - 1 );
pDC->LineTo( rect.right,rect.bottom - 1 );
//
pDC->SelectObject( penOld );
pDC->SelectObject( pOldBrush );
penNew.DeleteObject();
}
return 1;
}
//-----------------------------------------------
//DrawCellBase : 重画列表控件单元格基本函数
//-----------------------------------------------
int CMgListCtrlBase::DrawCellBase( CDC * pDC , UINT nItemID )
{
CHeaderCtrl *pheader = this->GetHeaderCtrl();
int nCount = pheader->GetItemCount();
COLORREF clrOldTxt = pDC->GetTextColor();
//画整行:
std::map<int,_T_LCS_>::iterator itr1 = m_mapListFullCellState.begin();
std::map<int,_T_LCS_>::iterator itr2 = m_mapListFullCellState.end();
for( ; itr1 != itr2 ; itr1 ++ )
{
if( itr1->second.nItem == nItemID )
{
RECT rect;
this->GetFullCellRect( nItemID , &rect );
pDC->FillSolidRect( &rect , itr1->second.clrBk );
pDC->SetTextColor( itr1->second.clrTxt );
}
}
//
for( int n = 0 ; n < nCount ; n ++ )
{
COLORREF clrOld = pDC->GetTextColor( );
RECT rect;
this->GetCellRect( nItemID , n , &rect );
DrawCellState( pDC , nItemID , n , &rect );
if( m_bYesNoOwnerData ){ vl_DrawCellText( pDC , nItemID , n , &rect ); }
else{ DrawCellText( pDC , nItemID , n , &rect ); }
//覆盖一下:
pDC->SetTextColor( clrOld );
}
//
pDC->SetTextColor( clrOldTxt );
return 1;
}
//-----------------------------------------------
//DrawCellTextBase : 重画列表控件单元格文本基本函数
//-----------------------------------------------
int CMgListCtrlBase::DrawCellTextBase( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect )
{
RECT rtItem;
rtItem.left = pRect->left;
rtItem.right= pRect->right;
rtItem.top = pRect->top;
rtItem.bottom=pRect->bottom;
LV_COLUMN _col;
_col.mask = LVCF_FMT;
this->GetColumn( nColumn , &_col );
COLORREF clrOldBk = pDC->GetBkColor();
COLORREF clrOldTxt= pDC->GetTextColor();
//默认处理
switch( _col.fmt )
{
case LVCFMT_LEFT | 0x4000 :
{
rtItem.left += 5;
pDC->DrawText( this->GetItemText(nItemID,nColumn) , &rtItem , DT_LEFT|DT_SINGLELINE );
break;
}
case LVCFMT_CENTER | 0x4000 :
{
pDC->DrawText( this->GetItemText(nItemID,nColumn) , &rtItem , DT_CENTER|DT_SINGLELINE );
break;
}
case LVCFMT_RIGHT | 0x4000 :
{
rtItem.right -= 5;
pDC->DrawText( this->GetItemText(nItemID,nColumn) , &rtItem , DT_RIGHT|DT_SINGLELINE );
break;
}
default:
{
rtItem.left += 5;
pDC->DrawText( this->GetItemText(nItemID,nColumn) , &rtItem , DT_LEFT|DT_SINGLELINE );
break;
}
}
//
pDC->SetBkColor( clrOldBk );
pDC->SetTextColor( clrOldTxt );
return 1;
}
//-----------------------------------------------
//DrawCellStateBase : 重画列表控件单元格状态基本函数
//-----------------------------------------------
int CMgListCtrlBase::DrawCellStateBase( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect )
{
std::map<int,_T_LCS_>::iterator itr1 = m_mapListCellState.begin();
std::map<int,_T_LCS_>::iterator itr2 = m_mapListCellState.end();
for( ; itr1 != itr2 ; itr1 ++ )
{
if( itr1->second.nItem == nItemID && itr1->second.nSubItem == nColumn )
{
pDC->FillSolidRect( pRect , itr1->second.clrBk );
pDC->SetTextColor( itr1->second.clrTxt );
}
}
return 1;
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 基本设置类函数 ------
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------
//SetStyle : 设置列表控件风格
//-----------------------------------------------
int CMgListCtrlBase::SetStyle( _enum_LCS_ lcs )
{
this->SetStyle( (DWORD)lcs );
return 0;
}
int CMgListCtrlBase::SetStyle( DWORD dwStyle )
{
this->ModifyStyle( NULL , dwStyle );
return 0;
}
//-----------------------------------------------
//SetExStyle : 设置列表控件扩展风格
//-----------------------------------------------
int CMgListCtrlBase::SetExStyle( DWORD dwStyle )
{
return this->SetExtendedStyle( this->GetExtendedStyle() | dwStyle );
}
//-----------------------------------------------
//SetShowItem : 是否显示项目(可以选择否,等添加完成项目,再显示,提高速率.)
//-----------------------------------------------
BOOL CMgListCtrlBase::SetShowItem( BOOL bShow )
{
BOOL bResult = m_bYesNoShowItem;
m_bYesNoShowItem = bShow;
return bResult;
}
//-----------------------------------------------
//SetOwnerDraw : 设置列表控件是否重画
//-----------------------------------------------
int CMgListCtrlBase::SetOwnerDraw( BOOL bDraw )
{
if( bDraw == TRUE )
{
this->ModifyStyle( NULL , LVS_OWNERDRAWFIXED );
}
return 0;
}
//-----------------------------------------------
//SetOwnerData : 设置列表控件是否按大数据量处理数据:
//-----------------------------------------------
int CMgListCtrlBase::SetOwnerData( BOOL bOwnerData )
{
if( bOwnerData == TRUE )
{
//无法开启该属性.
this->ModifyStyle( NULL , LVS_OWNERDATA );
m_bYesNoOwnerData = TRUE;
}
return 0;
}
//-----------------------------------------------
//SetShowFocus : 是否显示焦点线框
//-----------------------------------------------
BOOL CMgListCtrlBase::SetShowFocus( BOOL bShow , COLORREF clrNml , COLORREF clrSel )
{
//
m_clrFocusLine[0] = clrNml;
m_clrFocusLine[1] = clrSel;
BOOL bResult = m_bDrawFocusLine ;
m_bDrawFocusLine = bShow;
return bResult;
}
//-----------------------------------------------
//SetGridLineState : 设置网格线
//-----------------------------------------------
void CMgListCtrlBase::SetGridLineState( int nMgState , int nNmlState , COLORREF clrV , COLORREF clrH )
{
m_clrGridLine[0] = clrV;
m_clrGridLine[1] = clrH;
if( nMgState >= 0 ){ nNmlState = -1; m_nGridLineState = nMgState ; }
if( nNmlState >= 0 )
{
nMgState = -1;
m_nGridLineState = 0;
SetExStyle( this->GetExtendedStyle() | LVS_EX_GRIDLINES );
}
}
//-----------------------------------------------
//SetItemHeight : 设置列表控件项目高度
//-----------------------------------------------
void CMgListCtrlBase::SetItemHeight( int nHeight )
{
m_nItemHeight = nHeight;
CRect rcWin;
GetWindowRect(&rcWin);
WINDOWPOS wp;
wp.hwnd = m_hWnd;
wp.cx = rcWin.Width();
wp.cy = rcWin.Height();
wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 颜色设置类函数 ------
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------
//SetNmlColor : 设置列表控件默认背景
//-----------------------------------------------
BOOL CMgListCtrlBase::SetNmlBkColor( COLORREF color )
{
m_clrNmlBackGround = color;
return this->SetBkColor( m_clrNmlBackGround );
}
COLORREF CMgListCtrlBase::GetNmlBkColor( )
{
m_clrNmlBackGround = this->GetBkColor();
return m_clrNmlBackGround;
}
//-----------------------------------------------
//SetTxtBkColor : 设置列表控件纯文本背景
//-----------------------------------------------
BOOL CMgListCtrlBase::SetNmlTxtBkColor( COLORREF color )
{
m_clrNmlTxtBackGround = color;
return this->SetTextBkColor( m_clrNmlTxtBackGround );
}
COLORREF CMgListCtrlBase::GetNmlTxtBkColor( )
{
m_clrNmlTxtBackGround = this->GetTextBkColor();
return m_clrNmlTxtBackGround;
}
//-----------------------------------------------
//SetTxtColor : 设置列表控件纯文本颜色
//-----------------------------------------------
BOOL CMgListCtrlBase::SetNmlTxtColor( COLORREF color )
{
m_clrNmlTxt = color;
return this->SetTextColor( m_clrNmlTxt );
}
COLORREF CMgListCtrlBase::GetNmlTxtColor( )
{
m_clrNmlTxt = this->GetTextColor();
return m_clrNmlTxt;
}
//-----------------------------------------------
//SetSelBkColor : 设置列表控件选择时背景
//-----------------------------------------------
BOOL CMgListCtrlBase::SetSelBkColor( COLORREF color )
{
m_clrSelBackGround = color;
return TRUE;
}
COLORREF CMgListCtrlBase::GetSelBkColor( )
{
return m_clrSelBackGround;
}
//-----------------------------------------------
//SetSelTxtColor : 设置列表控件选择时文本颜色
//-----------------------------------------------
BOOL CMgListCtrlBase::SetSelTxtColor( COLORREF color )
{
m_clrSelTxt = color ;
return TRUE;
}
COLORREF CMgListCtrlBase::GetSelTxtColor( )
{
return m_clrSelTxt;
}
//---------------------------------------------------------------------------------------------------------------------------
// ----- 大数据项目类函数 ------
//---------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------
//vl_DrawCellText : 重画列表控件单元格文本
//-----------------------------------------------
void CMgListCtrlBase::vl_DrawCellText( CDC * pDC , UINT nItemID , int nColumn ,LPRECT pRect )
{
vl_DrawCellTextBase( pDC , nItemID , nColumn ,pRect );
}
//-----------------------------------------------
//DrawCellTextBase : 重画列表控件单元格文本基本函数
//-----------------------------------------------
int CMgListCtrlBase::vl_DrawCellTextBase( CDC * pDC , UINT nItemID , int nColumn , LPRECT pRect )
{
RECT rtItem;
rtItem.left = pRect->left;
rtItem.right= pRect->right;
rtItem.top = pRect->top;
rtItem.bottom=pRect->bottom;
LV_COLUMN _col;
_col.mask = LVCF_FMT;
this->GetColumn( nColumn , &_col );
COLORREF clrOldBk = pDC->GetBkColor();
COLORREF clrOldTxt= pDC->GetTextColor();
//默认处理
switch( _col.fmt )
{
case LVCFMT_LEFT | 0x4000 :
{
rtItem.left += 5;
pDC->DrawText( m_mapListInfo[nItemID].GetItemText(nColumn) , &rtItem , DT_LEFT|DT_SINGLELINE );
break;
}
case LVCFMT_CENTER | 0x4000 :
{
pDC->DrawText( m_mapListInfo[nItemID].GetItemText(nColumn) , &rtItem , DT_CENTER|DT_SINGLELINE );
break;
}
case LVCFMT_RIGHT | 0x4000 :
{
rtItem.right -= 5;
pDC->DrawText( m_mapListInfo[nItemID].GetItemText(nColumn) , &rtItem , DT_RIGHT|DT_SINGLELINE );
break;
}
default:
{
rtItem.left += 5;
pDC->DrawText( m_mapListInfo[nItemID].GetItemText(nColumn) , &rtItem , DT_LEFT|DT_SINGLELINE );
break;
}
}
//
pDC->SetBkColor( clrOldBk );
pDC->SetTextColor( clrOldTxt );
return 1;
}
//-----------------------------------------------
//vl_InsertItem : 向列表控件插入项目
//-----------------------------------------------
int CMgListCtrlBase::vl_InsertItem( int nItem , LPCTSTR strItem , BOOL bUpdate )
{
CMgListCtrlBaseInfo info;
info.SetIndex( nItem );
info.SetItemText( 0 , strItem );
int nSize = m_mapListInfo.size();
for( int n = nSize - 1 ; n >= nItem ; n -- )
{
m_mapListInfo[n].SetIndex( m_mapListInfo[n].GetIndex() + 1 );
m_mapListInfo[n+1] = m_mapListInfo[n];
}
m_mapListInfo.erase( nItem );
m_mapListInfo.insert( std::pair<int,CMgListCtrlBaseInfo>( nItem , info ) );
/*
SCROLLINFO si;
this->GetScrollInfo( SB_VERT , &si );
si.nMax = nItem;
this->SetScrollInfo( SB_VERT , &si );
*/
if( bUpdate == TRUE ){ this->SetItemCount( m_mapListInfo.size() ); }
//this->Invalidate();
return m_mapListInfo.size() ;
}
//-----------------------------------------------
//vl_DeleteItem : 删除列表控件指定项目
//-----------------------------------------------
int CMgListCtrlBase::vl_DeleteItem( int nItem )
{
for( int n = nItem ; n < m_mapListInfo.size() - 1 ; n ++ )
{
m_mapListInfo[n] = m_mapListInfo[n+1];
}
m_mapListInfo.erase( m_mapListInfo.size() - 1 );
this->SetItemCount( m_mapListInfo.size() - 1 );
return this->GetItemCount() - 1;
}
int CMgListCtrlBase::vl_DeleteAllItems( )
{
m_mapListCellState.clear();
m_mapListFullCellState.clear();
m_mapListInfo.clear();
this->SetItemCount( 0 );
return 0;
}
//-----------------------------------------------
//vl_SetItemText : 设置列表控件指定项目文本
//-----------------------------------------------
int CMgListCtrlBase::vl_SetItemText( int nItem, int nColumn , LPCTSTR pszItem )
{
return m_mapListInfo[nItem].SetItemText( nColumn , pszItem );
}
//-----------------------------------------------
//vl_GetItemText : 获取列表控件指定项目文本
//-----------------------------------------------
int CMgListCtrlBase::vl_GetItemText( int nItem , int nColumn , LPTSTR pszItem , int nItemLen )
{
return m_mapListInfo[nItem].GetItemText( nColumn , pszItem , nItemLen );
}
CString CMgListCtrlBase::vl_GetItemText( int nItem , int nColumn )
{
return m_mapListInfo[nItem].GetItemText( nColumn );
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!