1. //自绘CListCtrl类,重载虚函数DrawItem
  2. void CNewListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  3. {
  4. // TODO: Add your code to draw the specified item
  5. ASSERT(lpDrawItemStruct->CtlType == ODT_LISTVIEW);
  6. CDC dc;
  7. dc.Attach(lpDrawItemStruct->hDC);
  8. ASSERT(NULL != dc.GetSafeHdc());
  9. // Save these value to restore them when done drawing.
  10. COLORREF crOldTextColor = dc.GetTextColor();
  11. COLORREF crOldBkColor = dc.GetBkColor();
  12. // If this item is selected, set the background color
  13. // and the text color to appropriate values. Also, erase
  14. // rect by filling it with the background color.
  15. if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
  16. (lpDrawItemStruct->itemState & ODS_SELECTED))
  17. {
  18. dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  19. dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  20. dc.FillSolidRect(&lpDrawItemStruct->rcItem,
  21. ::GetSysColor(COLOR_HIGHLIGHT));
  22. }
  23. else
  24. {
  25. if(lpDrawItemStruct->itemID%2)
  26. dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128,128,128));
  27. else
  28. dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,128,255));
  29. }
  30. // If this item has the focus, draw a red frame around the
  31. // item's rect.
  32. if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
  33. (lpDrawItemStruct->itemState & ODS_FOCUS))
  34. {
  35. CBrush br(RGB(0, 0, 128));
  36. dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
  37. }
  38. // Draw the text.
  39. CString strText(_T(""));
  40. CRect rcItem;
  41. for(int i=0; i<GetHeaderCtrl()->GetItemCount(); i++)
  42. {
  43. strText = GetItemText(lpDrawItemStruct->itemID, i);
  44. GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem);
  45. rcItem.left += 5;
  46. dc.DrawText(
  47. strText,
  48. strText.GetLength(),
  49. &rcItem,
  50. DT_LEFT|DT_SINGLELINE|DT_VCENTER);
  51. }
  52. // Reset the background color and the text color back to their
  53. // original values.
  54. dc.SetTextColor(crOldTextColor);
  55. dc.SetBkColor(crOldBkColor);
  56. dc.Detach();
  57. }
  58. // 调用
  59. CNewListCtrl m_list; // 类的成员变量
  60. #define IDC_LIST 0x1101
  61. m_list.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|WS_HSCROLL|LVS_OWNERDRAWFIXED, CRect(0, 0, 280, 280), this, IDC_LIST);
  62. m_list.ModifyStyle(0, LVS_REPORT|LVS_SINGLESEL);
  63. m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
  64. m_list.InsertColumn(0, _T("AAA"), LVCFMT_LEFT, 100);
  65. m_list.InsertColumn(1, _T("BBB"), LVCFMT_LEFT, 100);
  66. CString strText(_T(""));
  67. for(int i=0; i<20; i++)
  68. {
  69. m_list.InsertItem(i, _T(""));
  70. strText.Format(_T("%d - Hello, World!"), i+1);
  71. m_list.SetItemText(i, 0, strText);
  72. strText.Format(_T("%d - ABCDEFG"), i+1);
  73. m_list.SetItemText(i, 1, strText);
  74. }

显示效果如下图所示:

http://blog.csdn.net/visualeleven/article/details/5948057

自绘CListCtrl类,重载虚函数DrawItem的更多相关文章

  1. C++ //多态 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 //动态多态:派生类和虚函数实现运行时多态

    1 //多态 2 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 3 //动态多态:派生类和虚函数实现运行时多态 4 5 //静态多态和动态多态的区别 6 //静态多态的函数地址早 ...

  2. C++ 类中有虚函数(虚函数表)时 内存分布

    虚函数表 对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的.简称为V-Table.在这个表中,主是要一个类的虚函数的地址表 ...

  3. C++基类、派生类、虚函数的几个知识点

    1.尽管派生类中含有基类继承来的成员,但派生类初始化这部分变量需要调用基类的构造函数. class A { private: int x; virtual void f(){cout<<& ...

  4. Unity3D中可重载虚函数的总结

    重载虚函数:Unity3D中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. A ...

  5. C++ - 类的虚函数\虚继承所占的空间

    类的虚函数\虚继承所占的空间 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24236469 char占用一个字节, 但不满足4的 ...

  6. 简单的自绘CListBox,重载虚MeasureItem和DrawItem这两个虚函数

    [cpp] view plain copy //例如CNewListBox继承自CListBox,重载虚MeasureItem和DrawItem这两个虚函数,代码如下: void CNewListBo ...

  7. C++ - 虚基类、虚函数与纯虚函数

    虚基类       在说明其作用前先看一段代码 class A{public:    int iValue;}; class B:public A{public:    void bPrintf(){ ...

  8. C++学习笔记第三天:类、虚函数、双冒号

    类 class Box { public: double length; // 盒子的长度 double breadth; // 盒子的宽度 double height; // 盒子的高度 }; 类成 ...

  9. 获取C++类成员虚函数地址

    1.GCC平台 GCC平台获取C++成员虚函数地址可使用如下方法[1]: class Base{ int i; public: virtual void f1(){ cout<<" ...

随机推荐

  1. SqlServer 全局变量

    1.@@ERROR 返与@@ERROR 近语句错误码局限于DML语句select语句执行现错误则返等于0错误码没错则返0通使用判断语句没执行功 -- Create Schema if not one ...

  2. OrzFAng系列–树 解题报告

    题目描述 方方方种下了三棵树,两年后,第二棵树长出了n个节点,其中1号节点是根节点. 给定一个n个点的树 支持两种操作 方方方进行m次操作,每个操作为: (1)给出两个数i,x,将第i个节点的子树中, ...

  3. web项目首页提示404

    我也是服了,估计是项目有问题,只能找jsp,找不到html和htm.把list中多余的都删掉,只保留index.jsp <welcome-file-list> <welcome-fi ...

  4. Coding4Fun.Phone.Controls的使用

    Coding4Fun.Phone.Controls的使用: windows phone的应用一直有一个特色,那就是方块(磁贴).之前的应用中,我一直都XXXX 来实现,原来其实一直有一个更加好的方法, ...

  5. Sqlite基础及其与SQLServer语法差异

    1 TOP 这是一个大家经常问到的问题,例如在SQLSERVER中可以使用如下语句来取得记录集中的前十条记录: SELECT TOP 10 * FROM [index] ORDER BY indexi ...

  6. 定位position详解:relative与absolute

    定位标签:position 包含属性:relative(相对) absolute(绝对) 1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在的位置上.然后通过设 ...

  7. Sqli-labs less 17

    Less-17 本关我们可以看到是一个修改密码的过程,利用的是update语句,与在用select时是一样的,我们仅需要将原先的闭合,构造自己的payload. 尝试报错 Username:admin ...

  8. NWR协议

    NWR是一种在分布式存储系统中用于控制一致性级别的一种策略.在Amazon的Dynamo云存储系统中,就应用NWR来控制一致性. 让我们先来看看这三个字母的含义:N:在分布式存储系统中,有多少份备份数 ...

  9. http status 汇总

    http status 汇总 常见HTTP状态码 200 OK 301 Moved Permanently 302 Found 304 Not Modified 307 Temporary Redir ...

  10. LA 2038

    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...