Self-painted CListCtrl class, overloaded virtual function DrawItem

Source: Internet
Author: User

[CPP]View PlainCopy
  1. Self-painted CListCtrl class, overloaded virtual function DrawItem
  2. void Cnewlistctrl::D rawitem (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 do 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. Call
  59. Cnewlistctrl m_list; //Member Variable of class
  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. }

The display effect looks like this:

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

Self-painted CListCtrl class, overloaded virtual function DrawItem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.