c++ - How can I have a multi-line item in a ListControl MFC? -


i have mfc list control in visual studio 2013 (c++) list of items (report view)

   lvcolumn lvcolumn;          lvcolumn.mask = lvcf_fmt | lvcf_text | lvcf_width;         lvcolumn.fmt = lvcfmt_left;         lvcolumn.cx = 120;         lvcolumn.psztext = "full name";         ((clistctrl*)getdlgitem(idc_list1))->insertcolumn(0, &lvcolumn);          lvcolumn.mask = lvcf_fmt | lvcf_text | lvcf_width;         lvcolumn.fmt = lvcfmt_left;         lvcolumn.cx = 75;         lvcolumn.psztext = "profession";         ((clistctrl*)getdlgitem(idc_list1))->insertcolumn(1, &lvcolumn);          lvcolumn.mask = lvcf_fmt | lvcf_text | lvcf_width;         lvcolumn.fmt = lvcfmt_left;         lvcolumn.cx = 80;         lvcolumn.psztext = "fav sport";         ((clistctrl*)getdlgitem(idc_list1))->insertcolumn(2, &lvcolumn);          lvcolumn.mask = lvcf_fmt | lvcf_text | lvcf_width;         lvcolumn.fmt = lvcfmt_left;         lvcolumn.cx = 75;         lvcolumn.psztext = "hobby";         ((clistctrl*)getdlgitem(idc_list1))->insertcolumn(3, &lvcolumn);          lvitem lvitem;         int nitem;          lvitem.mask = lvif_text;         lvitem.iitem = 0;         lvitem.isubitem = 0;         lvitem.psztext = "sandra c. anschwitz";         nitem = ((clistctrl*)getdlgitem(idc_list1))->insertitem(&lvitem);          ((clistctrl*)getdlgitem(idc_list1))->setitemtext(nitem, 1, "singer");         ((clistctrl*)getdlgitem(idc_list1))->setitemtext(nitem, 2, "handball");         ((clistctrl*)getdlgitem(idc_list1))->setitemtext(nitem, 3, "beach"); 

how can have multiline items full name, profession, sport , hobby?

surprisingly, not possible default clistctrl. but, little custom coding (and trickery), can effect want.

first, you’ll need derive own class clistctrl , set owner draw bit (owner draw fixed = true) control style. in parent dialog class, create image list (here’s trickery). image list used specify height of each row of list control. in example below, used:

m_imagelist.create(48, 48, ilc_color4, 10, 10); m_listctrl.setimagelist(&m_imagelist, lvsil_small); 

you’ll need play around cx , cy values image list fit needs. control use image list size each row because it’s anticipating displaying icons. next, add handler drawitem this:

void myclistctrl::drawitem(lpdrawitemstruct lpdrawitemstruct)     {     cdc* pdc = cdc::fromhandle(lpdrawitemstruct->hdc);      cstring text = _t("now time \nfor men\nto come aid");     pdc->drawtext(text , &lpdrawitemstruct->rcitem, dt_top);     // todo:  add code draw specified item     } 

in example, results in…

enter image description here

it may not elegant solution, but, works. note: approach, every row have same height.

edit: there few ways obtain row text. easiest use getitemtext so:

cstring txt = getitemtext(lpdrawitemstruct->itemid, 0); pdc->drawtext(txt, &lpdrawitemstruct->rcitem, dt_top); 

the above assumes set text each row using 1 of clistctrl methods set text.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

oracle - Changing start date for system jobs related to automatic statistics collections in 11g -