[CPP]View PlainCopy
- Self-painted CListCtrl class, overloaded virtual function DrawItem
- void Cnewlistctrl::D rawitem (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 do 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 ();
- }
- Call
- Cnewlistctrl m_list; //Member Variable of class
- #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);
- }
The display effect looks like this:
http://blog.csdn.net/visualeleven/article/details/5948057
Self-painted CListCtrl class, overloaded virtual function DrawItem