VC自绘ListCtrl(III) -- Alternate Row Colors for the CListCtrl

问题是这样的: 一般情况下,如果ListCtrl 的某个 Item 被选中的时候,它的背景会显示为蓝色,这时候如果我们点击一下其它的Button (也就是输入焦点转移到其它控件上去时),刚才这个被选中的item的背景会变成浅灰色,在这种情况下我要让它的背景仍然是蓝色,应该怎么实现?谢谢!

http://www.codeproject.com/listctrl/coloredlistctrl.asp

Alternate Row Colors for the CListCtrl

Introduction

I have seen a lot of requests asking how to implement a list control with different colors in each row. After a long time, taking a lot of useful information from this site, it‘s time to give back something. So, this MFC wrapper code just replaces the
CListCtrl control with one that alternates the row color of the control.

Using the code

This class uses the OnCustomDraw and OnEraseBkgnd to accomplish the alternate color effect. The big job is done in the
OnEraseBkgnd function, since this part of the code is responsible to "paint" our object. We have to find how many rows are shown as well as the height of each row. We receive these information from the
::GetCountPerPage and ::GetItemPosition (you can refer to MSDN for details) respectively. From this point and after, things are easy.

Collapse |
Copy Code

BOOL CColoredListCtrl::OnEraseBkgnd(CDC* pDC)
{
  // TODO: Add your message handler code here
  //       and/or call default

  CRect rect;
  CColoredListCtrl::GetClientRect(rect);

  POINT mypoint;  

  CBrush brush0(m_colRow1);
  CBrush brush1(m_colRow2);

  int chunk_height=GetCountPerPage();
  pDC->FillRect(&rect,&brush1);

  for (int i=0;i<=chunk_height;i++)
  {
    GetItemPosition(i,&mypoint);
    rect.top=mypoint.y ;
    GetItemPosition(i+1,&mypoint);
    rect.bottom =mypoint.y;
    pDC->FillRect(&rect,i %2 ? &brush1 : &brush0);
  }

  brush0.DeleteObject();
  brush1.DeleteObject();

  return FALSE;
}

To use this code, add the CColoredListCtrl class to your project and replace any
CListCtrl with this one.

If you want to change the default color of the rows, just replace the m_colRow1 and
m_colRow2 variables in the CColoredListCtrl constructor with the colors you prefer.

The default text color is black RGB(0,0,0). If you also want to change the item‘s text color then replace the code
lplvcd->clrText = RGB(0,0,0); that you can find in the
CColoredListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) function.

时间: 2024-08-02 17:10:26

VC自绘ListCtrl(III) -- Alternate Row Colors for the CListCtrl的相关文章

VC/MFC之ListCtrl控件使用经验总结

以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtrl类 SDK:以 "ListView_"开头的一些宏.如 ListView_InsertColumn -------------------------------------------------------------------------------- 1. CListCtrl 风格      LVS_ICON: 为每个item显示大图标      LVS_SMALLIC

自绘ListCtrl -- 设置行高

以下是通过重载DramItem()方法来实现自绘, 故需要设置ListCtrl控件属性"Owner Draw Fixed"为TRUE,"Owner Data"为FALSE(默认为FLASE); 1.  准备工作 (1).新建一个MFC类CMyListCtrl,其基类为CListCtrl, (2).将ListCtrl控件属性"Owner Draw Fixed"设置为TRUE,"Owner Data"设置为FALSE(默认为FLA

VC 自绘CListCtrl类

http://www.codeproject.com/listctrl/quicklist.asp http://www.codeproject.com/listctrl/ctooltiplistctrl.asp 实现功能:鼠标在ListCtrl上滑动的时候,滑动到哪一行哪一行就高亮,有且仅有一条是高亮的. 如何实现下面两个功能: 1.当鼠标离开ListCtrl的时候,高亮行取消高亮. 2.更改高亮行的背景色,默认是蓝色的,客户想要其它的颜色. //自绘CListCtrl类,重载虚函数DrawI

VC自绘按钮

看了吕鑫老师的视频,自己试着编写了一个自绘按钮控件.YuButton.h头文件如下: #pragma once   #include "afxwin.h"   class CYuButton :public CWnd   {       private:           BOOL  m_bIsDown;           BOOL  m_bIsMove;           BOOL  _bMouseTrack;//鼠标追踪状态              CString  m_s

共享MFC自绘Listctrl代码

在别人代码基础上修改的ListCtrl,支持设置行高,header高度,header背景图,奇偶行不同背景色, 支持设置某列为Checkbox,Edit,Combobox, 支持自定义排序. 效果图如下: VS2012代码下载:https://github.com/yinkaisheng/MFCListCtrlEx

VC自绘控件框架

这是一个按钮自绘的框架,其他控件也类似 //.h头文件 #pragma once #include "afxwin.h" #include "MemDC.h"//封装内存绘图类 class CYuButton :public CWnd {     private: BOOL  m_bIsDown; BOOL  m_bIsMove; BOOL  _bMouseTrack; CString  m_sCaption; CFont    *m_pFont;         

自绘listCtrl控件选中该行高亮(模拟windows)

CListCtrl的派生类CMyListCtrl的DrawItem()函数里添加代码 CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (lpDrawItemStruct->itemState && LVIS_SELECTED) { CPen PenLine(PS_SOLID, 1, RGB(0, 0, 0)); CPen *OldPenLine = pDC->SelectObject(&PenLine);

VC自绘 史上最强大且精简的记分点名软件

创意.诚意.潜力之作 史上最强大且精简的记分点名软件. 教育专家推荐的课堂软件,当老师的错过了,错过了肯定是您的损失. [一]个性开头白 唐诗.名言.英文.自定义 并可语音朗读出 [二]个性小组名 小组口号 并醒目显示 [三] 完善的积分机制 [四]  QQ管理界面 www.gudianxiaoshuo.com                

CListCtrl的Report风格自绘

原文链接: http://jingyan.baidu.com/article/5bbb5a1b38af1113eaa17910.html CListCtrl是MFC中运用最广泛的控件之一,很多软件都有CListCtrl的身影,但是对于CListCtrl的自绘,很多朋友都犯了难,网上虽然有很多人讲解怎么自绘,但是实现出的效果都不是太友好,本篇讲解CListCtrl的Report自绘,对于LVS_ICON风格,我们采用窗口模拟进行绘制.先说一下Report重绘的注意事项. 方法/步骤 1 CList