大家好,又见面了,我是你们的朋友全栈君。
pragma once
// CListCtrlCl
class CListCtrlCl : public CListCtrl
{
DECLARE_DYNAMIC(CListCtrlCl)
public:
CListCtrlCl();
virtual ~CListCtrlCl();
protected:
DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
public:
afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
public:
CPtrList m_ptrListCol; //保存列颜色
CPtrList m_ptrListItem; //保存Item颜色表
CPtrList m_colTextColor; //保存列字体颜色
CPtrList m_ItemTextColor; //保存单元格字体颜色
bool FindColColor(int col ,COLORREF &color); //查找列颜色
bool FindItemColor(int col,int row,COLORREF &color);
void SetColColor(int col,COLORREF color); //设置列颜色
void SetItemColor(int col,int row,COLORREF color); //设置Item颜色
void SetColTextColor(int col,COLORREF color); //设置列文本颜色
void SetItemTextColor(int col,int row,COLORREF color);
COLORREF m_color;
BOOL SetTextColor(COLORREF cr);
public:
// 字体高度
int m_fontHeight;
// 字体宽度
int m_fontWith;
};
// ListCtrlCl.cpp : 实现文件
//
#include "stdafx.h"
#include "TestCtrlList.h"
#include "ListCtrlCl.h"
struct stColor
{
int nRow;
int nCol;
COLORREF rgb;
};
// CListCtrlCl
IMPLEMENT_DYNAMIC(CListCtrlCl, CListCtrl)
CListCtrlCl::CListCtrlCl()
:m_fontHeight(12)
, m_fontWith(0)
{
m_color = RGB(0,0,0);
}
CListCtrlCl::~CListCtrlCl()
{
}
BEGIN_MESSAGE_MAP(CListCtrlCl, CListCtrl)
ON_WM_MEASUREITEM()
ON_WM_MEASUREITEM_REFLECT()
END_MESSAGE_MAP()
// CListCtrlCl 消息处理程序
void CListCtrlCl::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
ModifyStyle(0,LVS_OWNERDRAWFIXED);
CListCtrl::PreSubclassWindow();
}
void CListCtrlCl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
TCHAR lpBuffer[256];
LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM ;
lvi.iItem = lpDrawItemStruct->itemID ;
lvi.iSubItem = 0;
lvi.pszText = lpBuffer ;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rtClient;
GetClientRect(&rtClient);
for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
{
if ( nCol > 0 )
{
// Get Previous Column Width in order to move the next display item
GetColumn(nCol-1, &lvcprev) ;
lpDrawItemStruct->rcItem.left += lvcprev.cx ;
lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left;
}
CRect rcItem;
if (!GetSubItemRect(lpDrawItemStruct->itemID,nCol,LVIR_LABEL,rcItem))
continue;
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
CRect rcTemp;
rcTemp = rcItem;
if (nCol==0)
{
rcTemp.left -=2;
}
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
pDC->FillSolidRect(&rcTemp, GetSysColor(COLOR_HIGHLIGHT)) ;
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
}
else
{
COLORREF color;
color = GetBkColor();
pDC->FillSolidRect(rcTemp,color);
if (FindColColor(nCol,color))
{
pDC->FillSolidRect(rcTemp,color);
}
if (FindItemColor(nCol,lpDrawItemStruct->itemID,color))
{
pDC->FillSolidRect(rcTemp,color);
}
}
pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
UINT uFormat = DT_LEFT ;
TEXTMETRIC metric;
pDC->GetTextMetrics(&metric);
int ofst;
ofst = rcItem.Height() - metric.tmHeight;
rcItem.OffsetRect(0,ofst/2);
pDC->SetTextColor(m_color);
CFont nFont ,* nOldFont;
nFont.CreateFont(m_fontHeight,m_fontWith,0,0,0,FALSE,FALSE,0,0,0,0,0,0,_TEXT("宋体"));//创建字体
nOldFont = pDC->SelectObject(&nFont);
DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer), &rcItem, uFormat) ;
pDC->SelectStockObject(SYSTEM_FONT) ;
}
}
void CListCtrlCl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
void CListCtrlCl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
}
bool CListCtrlCl::FindColColor(int col,COLORREF &color) //查找列颜色
{
int flag = 0;
for (POSITION pos = m_ptrListCol.GetHeadPosition();pos!=NULL;)
{
stColor *pColor = (stColor*)m_ptrListCol.GetNext(pos);
if (pColor->nCol==col)
{
flag = 1;
color = pColor->rgb;
break;
}
}
if (1==flag)
{
return true;
}
return false;
}
bool CListCtrlCl::FindItemColor(int col,int row,COLORREF &color) //查找颜色
{
int flag = 0;
for (POSITION pos = m_ptrListItem.GetHeadPosition();pos!=NULL;)
{
stColor *pColor = (stColor*)m_ptrListItem.GetNext(pos);
if (pColor->nCol==col&&pColor->nRow==row)
{
flag = 1;
color = pColor->rgb;
break;
}
}
if (1==flag)
{
return true;
}
return false;
}
void CListCtrlCl::SetColColor(int col,COLORREF color) //设置列颜色
{
stColor *pColor = new stColor;
pColor->nCol = col;
pColor->rgb = color;
m_ptrListCol.AddTail(pColor);
}
void CListCtrlCl::SetItemColor(int col,int row,COLORREF color) //设置格子颜色
{
stColor *pColor = new stColor;
pColor->nCol = col;
pColor->nRow = row;
pColor->rgb = color;
m_ptrListItem.AddTail(pColor);
}
BOOL CListCtrlCl::SetTextColor(COLORREF cr) //设置字体颜色
{
m_color = cr;
return TRUE;
}
void CListCtrlCl::SetColTextColor(int col,COLORREF color)
{
stColor *pColor = new stColor;
pColor->nCol = col;
pColor->rgb = color;
m_colTextColor.AddTail(pColor);
}
void CListCtrlCl::SetItemTextColor(int col,int row,COLORREF color)
{
stColor *pColor = new stColor;
pColor->nCol = col;
pColor->nRow = row;
pColor->rgb = color;
m_ItemTextColor.AddTail(pColor);
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/152481.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...