Self-painted Listctrl--Set row height

Source: Internet
Author: User

The following is the use of the overloaded Dramitem () method to achieve self-painting,

Therefore, you need to set the Listctrl control property "owner Draw Fixed" to True, "owner Data" is False (default is flase);


1. Preparatory work
(1). Create a new MFC class Cmylistctrl, whose base class is CListCtrl,
(2). Set the Listctrl control property "owner Draw Fixed" to True, "owner Data" is set to False (default is flase);
(3). In the dialog class declaration where the Listctrl control is located, modify the control variable declaration to Cmylistctrl m_list;
(4). In the MyListCtrl.h file, add the variable int m_nrowheight;

2. Manually add the message macro On_wm_measureitem_reflect (), and add the following function to implement modify row height

void Cmylistctrl::measureitem (Lpmeasureitemstruct lpmeasureitemstruct) {if (m_nrowheight>0) { Lpmeasureitemstruct->itemheight = M_nrowheight;}}

3. Manually add message macro On_wm_measureitem, Response message processing OnMeasureItem ()

void Cmylistctrl::onmeasureitem (int nidctl, lpmeasureitemstruct lpmeasureitemstruct) {//TODO: Add Message Handler code here and/ or call the default value Clistctrl::onmeasureitem (Nidctl, lpmeasureitemstruct);}

4. Add member variable int m_nrowheight, and add external modify interface

void cmylistctrl::setrowheigt (int nheight) {m_nrowheight = nheight; CRect Rcwin; GetWindowRect (&rcwin); Windowpos Wp;wp.hwnd = m_hwnd;wp.cx = Rcwin.width (); wp.cy = Rcwin.height (); wp.flags = Swp_noactivate | Swp_nomove | Swp_noownerzorder | Swp_nozorder; SendMessage (wm_windowposchanged, 0, (LPARAM) &wp);}

5. Manually add message Macro On_wm_drawitem (), Reload DRAWITEM () redraw list control

void Cmylistctrl::D rawitem (lpdrawitemstruct lpdrawitemstruct) {cdc* PDC = Cdc::fromhandle (LPDRAWITEMSTRUCT-&GT;HDC); Lvitem LVI = {0}; Lvi.mask = lvif_state;//| Lvif_image; Lvi.statemask = lvis_focused | lvis_selected; Lvi.iitem = lpdrawitemstruct->itemid; BOOL bget = GetItem (&lvi); highlighting bool Bhighlight = ((Lvi.state & lvis_drophilited) | | ((Lvi.state & lvis_selected) && ((getfocus () = = This) | | (GetStyle () & Lvs_showselalways))); /Draw Text background CRect Rcback = lpdrawitemstruct->rcitem; Pdc->setbkmode (TRANSPARENT); if (bhighlight)//If selected {Pdc->settextcolor (RGB (255,255,255));//Text color Pdc->fillrect (rcback, &cbrush (RGB ( 90,162,100)));       Row background Color} else {Pdc->settextcolor (RGB (0,0,0));//Text color Pdc->fillrect (rcback, &cbrush (RGB (255,255,255))); Row background Color}//Draw text if (Lpdrawitemstruct->itemaction & Oda_drawentire) {//Get column number int ncollumn = GetHeaderCtrl () GetItemCount ();//cyclic processing CString sztext; for (int i = 0; i < GetHeaderCtrl ()->getitemcount (); i++) {CRect rcItem; Getsubitemrect (Lpdrawitemstruct->itemid, I, Lvir_label, RcItem)) {continue;} Sztext = GetItemText (Lpdrawitemstruct->itemid, i); Rcitem.left + = 5; Rcitem.right-= 1; Pdc->drawtext (Sztext, Lstrlen (Sztext), &rcitem, Dt_left | Dt_vcenter | Dt_noprefix | Dt_singleline);} } }


Method of changing row Height II:

You can also change the row height by inserting an icon in each row, and the icon can be actually inserted as needed

Set the Listctrl property first

Load Listctrllong Lstyle;lstyle = GetWindowLong (M_tasklist.m_hwnd, Gwl_style); Gets the current window Stylelstyle &= ~lvs_typemask; Clear display mode Lstyle |= Lvs_report | Lvsil_small | Lvs_aligntop | Lvs_showselalways; Set Stylesetwindowlong (M_tasklist.m_hwnd, Gwl_style, lstyle);D word dwstyle = M_tasklist.getextendedstyle ();// Dwstyle |= Lvs_ex_fullrowselect; Select an exercise line highlighting (only applicable with the report style Listctrl) dwstyle |= lvs_ex_subitemimages; Subitem add icon dwstyle |= lvs_ex_doublebuffer;m_tasklist.setextendedstyle (dwstyle);//::sendmessage (m_TaskList.m_ HWnd, Lvm_setextendedlistviewstyle, Lvs_ex_fullrowselect, Lvs_ex_fullrowselect);

Set icon again (row height)

Set the Listctrl line height (only control the row height through cy=44, but there is no need to load the icon, you need to download the app dynamically depending on the app to load the specific icon) m_imagelist.create (*, Ilc_color32 | Ilc_mask, 0, 10); If the use of 32*32 will change the row height. Ilc_mask set the background of the icon to be transparent, otherwise the blank place is black m_imagelist.setbkcolor (RGB (255, 255, 255)); You need to set the background color to white, otherwise the icon cutout place is black, and the icon has a black border M_ninstall = M_imagelist.add (AfxGetApp ()->loadicon (Idi_icon1));   only. ico pictures,. bmp format not shown, ADD returns index 0//m_ninstall = M_imagelist.add (LoadIcon (AfxGetResourceHandle (), Makeintresource (IDI (_icon1)));  Or this m_tasklist.setimagelist (&m_imagelist, Lvsil_small); Lvsil_small for Small Icons (16*16), Lvsil_normal for large icons (32*32), be sure to use the matching, otherwise do not display




Self-painted Listctrl--Set row height

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.