[CPP] view plain copy
- For example Cnewlistbox inherits from CListBox, overloads virtual MeasureItem and DrawItem two virtual functions, the code is as follows:
- void Cnewlistbox::D rawitem (lpdrawitemstruct lpdrawitemstruct)
- {
- //Todo:add your code to draw the specified item
- ASSERT (Lpdrawitemstruct->ctltype = = Odt_listbox);
- LPCTSTR LpszText = (lpctstr) lpdrawitemstruct->itemdata;
- ASSERT (LpszText! = NULL);
- CDC DC;
- dc. Attach (LPDRAWITEMSTRUCT->HDC);
- //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);
- }
- Lpdrawitemstruct->rcitem.left + = 5;
- //Draw the text.
- dc. DrawText (
- LpszText,
- Strlen (LpszText),
- &lpdrawitemstruct->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 ();
- }
- void Cnewlistbox::measureitem (Lpmeasureitemstruct lpmeasureitemstruct)
- {
- //Todo:add your code to determine the size of specified item
- ASSERT (Lpmeasureitemstruct->ctltype = = Odt_listbox);
- LPCTSTR LpszText = (lpctstr) lpmeasureitemstruct->itemdata;
- ASSERT (LpszText! = NULL);
- CSize sz;
- cdc* PDC = GetDC ();
- SZ = pdc->gettextextent (LpszText);
- ReleaseDC (PDC);
- Lpmeasureitemstruct->itemheight = 2*sz.cy;
- }
- Where M_listbox is an object of type Cnewlistbox
- #define Idc_listbox 0x1101
- M_listbox.create (ws_child| ws_visible| ws_border| ws_vscroll| ws_hscroll|
- 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