自绘CListCtrl类,重载虚函数DrawItem
- //自绘CListCtrl类,重载虚函数DrawItem
- void CNewListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- // TODO: Add your code to draw the specified item
- ASSERT(lpDrawItemStruct->CtlType == ODT_LISTVIEW);
- CDC dc;
- dc.Attach(lpDrawItemStruct->hDC);
- ASSERT(NULL != dc.GetSafeHdc());
- // Save these value to restore them when done drawing.
- COLORREF crOldTextColor = dc.GetTextColor();
- COLORREF crOldBkColor = dc.GetBkColor();
- // If this item is selected, set the background color
- // and the text color to appropriate values. Also, erase
- // rect by filling it with the background color.
- if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
- (lpDrawItemStruct->itemState & ODS_SELECTED))
- {
- dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
- dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
- dc.FillSolidRect(&lpDrawItemStruct->rcItem,
- ::GetSysColor(COLOR_HIGHLIGHT));
- }
- else
- {
- if(lpDrawItemStruct->itemID%2)
- dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128,128,128));
- else
- dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,128,255));
- }
- // If this item has the focus, draw a red frame around the
- // item's rect.
- if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
- (lpDrawItemStruct->itemState & ODS_FOCUS))
- {
- CBrush br(RGB(0, 0, 128));
- dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
- }
- // Draw the text.
- CString strText(_T(""));
- CRect rcItem;
- for(int i=0; i<GetHeaderCtrl()->GetItemCount(); i++)
- {
- strText = GetItemText(lpDrawItemStruct->itemID, i);
- GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem);
- rcItem.left += 5;
- dc.DrawText(
- strText,
- strText.GetLength(),
- &rcItem,
- DT_LEFT|DT_SINGLELINE|DT_VCENTER);
- }
- // Reset the background color and the text color back to their
- // original values.
- dc.SetTextColor(crOldTextColor);
- dc.SetBkColor(crOldBkColor);
- dc.Detach();
- }
- // 调用
- CNewListCtrl m_list; // 类的成员变量
- #define IDC_LIST 0x1101
- m_list.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|WS_HSCROLL|LVS_OWNERDRAWFIXED, CRect(0, 0, 280, 280), this, IDC_LIST);
- m_list.ModifyStyle(0, LVS_REPORT|LVS_SINGLESEL);
- m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
- m_list.InsertColumn(0, _T("AAA"), LVCFMT_LEFT, 100);
- m_list.InsertColumn(1, _T("BBB"), LVCFMT_LEFT, 100);
- CString strText(_T(""));
- for(int i=0; i<20; i++)
- {
- m_list.InsertItem(i, _T(""));
- strText.Format(_T("%d - Hello, World!"), i+1);
- m_list.SetItemText(i, 0, strText);
- strText.Format(_T("%d - ABCDEFG"), i+1);
- m_list.SetItemText(i, 1, strText);
- }
显示效果如下图所示:

http://blog.csdn.net/visualeleven/article/details/5948057
自绘CListCtrl类,重载虚函数DrawItem的更多相关文章
- C++ //多态 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 //动态多态:派生类和虚函数实现运行时多态
1 //多态 2 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 3 //动态多态:派生类和虚函数实现运行时多态 4 5 //静态多态和动态多态的区别 6 //静态多态的函数地址早 ...
- C++ 类中有虚函数(虚函数表)时 内存分布
虚函数表 对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的.简称为V-Table.在这个表中,主是要一个类的虚函数的地址表 ...
- C++基类、派生类、虚函数的几个知识点
1.尽管派生类中含有基类继承来的成员,但派生类初始化这部分变量需要调用基类的构造函数. class A { private: int x; virtual void f(){cout<<& ...
- Unity3D中可重载虚函数的总结
重载虚函数:Unity3D中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. A ...
- C++ - 类的虚函数\虚继承所占的空间
类的虚函数\虚继承所占的空间 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24236469 char占用一个字节, 但不满足4的 ...
- 简单的自绘CListBox,重载虚MeasureItem和DrawItem这两个虚函数
[cpp] view plain copy //例如CNewListBox继承自CListBox,重载虚MeasureItem和DrawItem这两个虚函数,代码如下: void CNewListBo ...
- C++ - 虚基类、虚函数与纯虚函数
虚基类 在说明其作用前先看一段代码 class A{public: int iValue;}; class B:public A{public: void bPrintf(){ ...
- C++学习笔记第三天:类、虚函数、双冒号
类 class Box { public: double length; // 盒子的长度 double breadth; // 盒子的宽度 double height; // 盒子的高度 }; 类成 ...
- 获取C++类成员虚函数地址
1.GCC平台 GCC平台获取C++成员虚函数地址可使用如下方法[1]: class Base{ int i; public: virtual void f1(){ cout<<" ...
随机推荐
- JS全局屏蔽回车事件
window.onload = function (){ document.body.onkeydown=function(event){ if(event.keyCode==13){ event.k ...
- 设计模式之Birdge(桥接)模式
1.出现原因 1.同一个类型,有两个变化的维度(两个维度的抽象:一个抽象部分的抽象,一个实现部分的抽象) 2.如何应对这种“多维度的变化”?如何利用面向对象技术来使得同一类型可以轻松地沿着两个方向变化 ...
- Windows8 各种版本区别对比详解
微软的 Windows8 操作系统提供了4个不同的版本,分别是 Windows RT.Windows 8 标准版.Windows 8 Pro 专业版 以及 Windows 8 Enterprise 企 ...
- LintCode-Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 五、案例-指令参考-freemarker指令、表达式
案例-指令参考描述:本人自己测试写了一遍,如有错的地方,懂freemarker的朋友望指点指点! 案例-指令参考 表达式 一. Assign 1.<#assign name1="北京& ...
- 怎么将java项目打包成双击就可以运行的jar包---fatjar
fatjar下载地址:http://pan.baidu.com/s/1cQ01o 下载fatJar插件,解压缩后是一个.../plugins/(net...)把plugins下面的(net..)文件夹 ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- 华为HG8240光猫-破解-联通-2016-telnet-http
序 我与大家想法基本一致,拿到联通的光猫后,心想它应该是个路由器吧,如果让它自己拨号上网就好了,即省一台路由器,又省电了.抱着这个想法,在2013年里,我搜罗了不少文章,经过Q群,搜索,询问,阅读,理 ...
- Java8 Lambda sample (iwantmoon.com出品)
转载请注明出处:http://iwantmoon.com/Post/83a4497a74674612834965857e7bc044 .Net转到Java来,不知不觉已经有两年了,之前已经用习惯的li ...
- TEST ON 平安夜
1.前言 = = 感觉自己其实没发过关于考试的博客过... 今天是一个平安的夜晚,漆黑的夜被霓虹划分成网络,很适合发题. 2.num9九数码问题 传统8数码改一下...只询问一个状态,所以很容易搞,正 ...