Reference: clistboxst source code demo source code: ciconlistbox_demodemo program image: Example: Manually changing the properties of The ListBox control is as follows, because the following features cannot be dynamically modified through code.m_listbox.AddString(TEXT("123"), IDI_ICON_1);m_listbox.AddString(TEXT("456"), IDI_ICON_2);m_listbox.AddString(TEXT("789"), IDI_ICON_1);
The Code is as follows -------------------------------------------------------------------------------------------------------
Ciconlistbox class:Class ciconlistbox: Public clistbox {declare_dynamic (ciconlistbox) struct lbdata {hicon; int niconheight; int niconwidth;}; public: ciconlistbox (); Virtual ~ Ciconlistbox (); protected: declare_message_map () Public: Virtual void drawitem (lpdrawitemstruct/* blank */); Virtual void measureitem (lpmeasureitemstruct/* measlpureitemstruct */); // interfaceint addstring (lpctstr lpszitem, int nicon); int deletestring (uint nindex); int insertstring (INT nindex, lpctstr lpszitem, int nicon); void resetcontent (); Private: void deleteitemdata (uint nindex); int m_niconheight; // The height of the icon. In the measureitem function, int m_noffset is used to determine the height of each item. // when the icon and text are drawn, offset from the border };
1 override measureitem//// Determine the height to be drawn for each item in ListBox // void ciconlistbox: measureitem (lpmeasureitemstruct) {// determine the height of the text assert (lpmeasureitemstruct-> ctltype = odt_listbox); lpctstr lpsztext = (lpctstr) lpmeasureitemstruct-> itemdata; Assert (lpsztext! = NULL); csize SZ; CDC * PDC = getdc (); SZ = PDC-> gettextextent (lpsztext); releasedc (PDC); // compare the height of text and icon, int nitemheight = 2 * Sz. cy> m_niconheight + m_noffset * 2? 2 * Sz. Cy: m_niconheight + m_noffset * 2; lpmeasureitemstruct-> itemheight = nitemheight ;}
2 override drawitemVoid ciconlistbox: drawitem (lpdrawitemstruct) {// prepare the cdc dc; DC. attach (lpdrawitemstruct-> HDC); DC. setbkmode (transparent); Assert (lpdrawitemstruct-> ctltype = odt_listbox); cstring cstrtext; clistbox: gettext (lpdrawitemstruct-> Itemid, cstrtext); Assert (! Cstrtext. isempty (); // monitoring status bool bisselected = (lpdrawitemstruct-> itemaction | oda_select) & (lpdrawitemstruct-> itemstate & ods_selected ); bool bisfocused = (lpdrawitemstruct-> itemaction | oda_focus) & (lpdrawitemstruct-> itemstate & ods_focus); // draw background if (bisselected) {cbrush brushbk (:: getsyscolor (color_highlight); DC. fillrect (& lpdrawitemstruct-> rcitem, & brushbk);} else {cbrush brushbk (DC. getbkcolor (); DC. fillrect (& region-> rcitem, & brushbk);} // draw iconlbdata * plbdata = (lbdata *) clistbox: getitemdataptr (lpdrawitemstruct-> Itemid); If (null! = Plbdata & (lbdata *)-1! = Plbdata & null! = Plbdata-> hicon) {uint flag = dst_icon;: drawstate (lpdrawitemstruct-> HDC, null, null, (lparam) plbdata-> hicon, null, lpdrawitemstruct-> rcitem. left + m_noffset, lpdrawitemstruct-> rcitem. top + m_noffset, plbdata-> niconwidth, plbdata-> niconheight, flag);} // draw textif (bisselected) {DC. settextcolor (: getsyscolor (color_highlighttext);} else {DC. settextcolor (RGB (255, 0, 0);} crect rctext; rctext. left = lpdrawitemstruct-> rcitem. left + m_noffset + plbdata-> niconwidth + m_noffset; rctext. top = lpdrawitemstruct-> rcitem. top + m_noffset; rctext. right = lpdrawitemstruct-> rcitem. right-m_noffset; rctext. bottom = lpdrawitemstruct-> rcitem. bottom-m_noffset; DC. drawtext (cstrtext, & rctext, dt_left | dt_singleline | dt_vcenter); // closes the work DC. detach ();}
3. Other functions/// The ID of the icon in nicon. // note: In clistbox: addstring, measureitem is called to determine the height of the added item. Therefore, the first LoadImage is used to determine the height of the icon, and assign the value to the class's private variable m_niconheight. // in this way, you can use m_niconheight in measureitem to determine the icon height. // int ciconlistbox: addstring (lpctstr lpszitem, int nicon) {lbdata * plbdata = NULL; m_niconheight = 0; // load iconhicon hicon = (hicon): LoadImage (: getmodulehandle (null), makeintresource (nicon), image_icon, 0, 0, 0); If (hicon! = NULL) {plbdata = new lbdata (); iconinfo ICI;: geticoninfo (hicon, & ICI); bitmap bm;: GetObject (ici. hbmcolor, sizeof (Bitmap), & BM);: deleteobject (ici. hbmcolor);: deleteobject (ici. hbmmask); plbdata-> hicon = hicon; plbdata-> niconheight = BM. bmheight; plbdata-> niconwidth = BM. bmwidth; m_niconheight = BM. bmheight;} // Add string and lbdataint nindex = clistbox: addstring (lpszitem); If (lb_err = nindex | L B _errspace = nindex) {If (null! = Plbdata) {Delete plbdata;: destroyicon (hicon) ;}} else {clistbox: setitemdataptr (nindex, plbdata);} return nindex;} int ciconlistbox :: insertstring (INT nindex, lpctstr lpszitem, int nicon) {lbdata * plbdata = NULL; m_niconheight = 0; // load iconhicon hicon = (hicon): LoadImage (:: getmodulehandle (null), makeintresource (nicon), image_icon, 0, 0, 0); If (hicon! = NULL) {plbdata = new lbdata (); iconinfo ICI;: geticoninfo (hicon, & ICI); bitmap bm;: GetObject (ici. hbmcolor, sizeof (Bitmap), & BM);: deleteobject (ici. hbmcolor);: deleteobject (ici. hbmmask); plbdata-> hicon = hicon; plbdata-> niconheight = BM. bmheight; plbdata-> niconwidth = BM. bmwidth; m_niconheight = BM. bmheight;} // insert string and lbdatanindex = clistbox: insertstring (nindex, lpszitem); If (lb_err = N Index | lb_errspace = nindex) {If (null! = Plbdata) {: destroyicon (hicon); Delete plbdata ;}} else {clistbox: setitemdataptr (nindex, plbdata);} return nindex;} int ciconlistbox :: deletestring (uint nindex) {deleteitemdata (nindex); Return clistbox: deletestring (nindex);} void ciconlistbox: deleteitemdata (uint nindex) {lbdata * plbdata = (lbdata *) clistbox: getitemdataptr (nindex); If (lbdata *)-1! = Plbdata & null! = Plbdata) {If (plbdata-> hicon): destroyicon (plbdata-> hicon); Delete plbdata ;}} void ciconlistbox: resetcontent () {int ncount = getcount (); For (INT I = 0; I <ncount; ++ I) {deleteitemdata (I) ;} clistbox: resetcontent ();}