VC self-Painted CListCtrl class

Source: Internet
Author: User

Http://www.codeproject.com/listctrl/quicklist.asp

Http://www.codeproject.com/listctrl/ctooltiplistctrl.asp


Implementation function: When the mouse slide on the Listctrl, swipe to which line which line is highlighted, there is only one is highlighted.

How to implement the following two features:
1. When the mouse leaves Listctrl, certain cancel highlighting.
2. Change the background color of the certain, the default is blue, the customer wants other colors.


    Self-painting 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-&GT;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 was 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, &AMP;BR);       }//Draw the text.       CString StrText (_t (""));             CRect RcItem; for (int i=0; I<getheaderctrl ()->getitemcount (); i++) {strText = GetItemText (Lpdrawitemstruct->itemi        D, 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; The member variable of the 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:


VC self-Painted CListCtrl class

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.