Simple self-plotting clistbox, overloaded virtual MeasureItem and DrawItem, two virtual functions

Source: Internet
Author: User

[CPP] view plain copy

  1. For example Cnewlistbox inherits from CListBox, overloads virtual MeasureItem and DrawItem two virtual functions, the code is as follows:
  2. void Cnewlistbox::D rawitem (lpdrawitemstruct lpdrawitemstruct)
  3. {
  4. //Todo:add your code to draw the specified item
  5. ASSERT (Lpdrawitemstruct->ctltype = = Odt_listbox);
  6. LPCTSTR LpszText = (lpctstr) lpdrawitemstruct->itemdata;
  7. ASSERT (LpszText! = NULL);
  8. CDC DC;
  9. dc. Attach (LPDRAWITEMSTRUCT->HDC);
  10. //Save These value to restore them when do drawing.
  11. colorref Croldtextcolor = DC.  GetTextColor ();
  12. colorref Croldbkcolor = DC.  GetBkColor ();
  13. //If This item is selected, set the background color
  14. //And the text color to appropriate values. Also, Erase
  15. //Rect by filling it with the background color.
  16. if (lpdrawitemstruct->itemaction | Oda_select) &&
  17. (Lpdrawitemstruct->itemstate & ods_selected))
  18. {
  19. dc. SetTextColor (:: GetSysColor (Color_highlighttext));
  20. dc. SetBkColor (:: GetSysColor (Color_highlight));
  21. dc. Fillsolidrect (&lpdrawitemstruct->rcitem,
  22. :: GetSysColor (Color_highlight));
  23. }
  24. Else
  25. {
  26. if (lpdrawitemstruct->itemid%2)
  27. dc. Fillsolidrect (&lpdrawitemstruct->rcitem, RGB (128,128,128));
  28. Else
  29. dc. Fillsolidrect (&lpdrawitemstruct->rcitem, RGB (255,128,255));
  30. }
  31. //If This item has the focus, draw a red frame around the
  32. //Item ' s rect.
  33. if (lpdrawitemstruct->itemaction | Oda_focus) &&
  34. (Lpdrawitemstruct->itemstate & Ods_focus))
  35. {
  36. CBrush BR (RGB (0, 0, 128));
  37. dc. Framerect (&lpdrawitemstruct->rcitem, &BR);
  38. }
  39. Lpdrawitemstruct->rcitem.left + = 5;
  40. //Draw the text.
  41. dc. DrawText (
  42. LpszText,
  43. Strlen (LpszText),
  44. &lpdrawitemstruct->rcitem,
  45. Dt_left| dt_singleline| Dt_vcenter);
  46. //Reset the background color and the text color back to their
  47. //Original values.
  48. dc. SetTextColor (Croldtextcolor);
  49. dc. SetBkColor (Croldbkcolor);
  50. dc. Detach ();
  51. }
  52. void Cnewlistbox::measureitem (Lpmeasureitemstruct lpmeasureitemstruct)
  53. {
  54. //Todo:add your code to determine the size of specified item
  55. ASSERT (Lpmeasureitemstruct->ctltype = = Odt_listbox);
  56. LPCTSTR LpszText = (lpctstr) lpmeasureitemstruct->itemdata;
  57. ASSERT (LpszText! = NULL);
  58. CSize sz;
  59. cdc* PDC = GetDC ();
  60. SZ = pdc->gettextextent (LpszText);
  61. ReleaseDC (PDC);
  62. Lpmeasureitemstruct->itemheight = 2*sz.cy;
  63. }
  64. Where M_listbox is an object of type Cnewlistbox
  65. #define Idc_listbox 0x1101
  66. M_listbox.create (ws_child| ws_visible| ws_border| ws_vscroll| ws_hscroll|
  67. lbs_ownerdrawvariable, CRect (0, 0, 380, 280), this , Idc_listbox);

As shown below:

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

Simple self-plotting clistbox, overloaded virtual MeasureItem and DrawItem, two virtual functions

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.