您的位置:程序门 -> vc/mfc -> 界面



如何在一個clistctrl控件中的一行,顯示兩行字符...(搞定就結貼)..謝謝.


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何在一個clistctrl控件中的一行,顯示兩行字符...(搞定就結貼)..謝謝.[已结贴,结贴人:kei_lin]
发表于:2007-09-04 10:32:50 楼主
如何在一個clistctrl控件中的一行,顯示兩行字符...
如:
        北京/bei   jing             鄭州/zheng   zhou                 12:40               4
專化成這樣:
              北京                         鄭州                                       12:40               4
            bei   jing               zheng   zhou                                                     4   number
請高手解答..(謝謝..)
发表于:2007-09-04 10:46:171楼 得分:8
custom   draw
发表于:2007-09-04 11:13:332楼 得分:0
能不能詳細點..
小弟有愚拙..
明顯吧..
发表于:2007-09-04 12:22:003楼 得分:2
如果是clistcontrl的话,有两种方式,一个是改变风格为report,然后设置表头,insertitem,setitemtext就可以了,或者用cstring::format,把两个字符格式化一下,然后insert进去也可以。
看看msdn
发表于:2007-09-04 13:07:014楼 得分:2
关注
发表于:2007-09-04 13:09:295楼 得分:2
用gridctrl,
www.codeproject.com搜索

发表于:2007-09-04 13:20:086楼 得分:2
...   看错了,原来是两行字符...
自绘吧
发表于:2007-09-04 14:31:397楼 得分:0
我也想到自繪..
但不知道從哪下著手..
能不能再詳細點..
发表于:2007-09-04 14:41:098楼 得分:2
mark
发表于:2007-09-04 22:05:319楼 得分:0
来个高手帮帮我别...
发表于:2007-09-14 15:41:2610楼 得分:2
继承clistctrl类,重载drawitem,自己绘就行了。。
发表于:2007-09-14 21:15:5611楼 得分:2
对。
发表于:2007-09-14 23:28:5712楼 得分:50
#pragma   once

//   cmultilinelistctrl

class   cmultilinelistctrl   :   public   clistctrl
{
declare_dynamic(cmultilinelistctrl)

public:
cmultilinelistctrl();
virtual   ~cmultilinelistctrl();
void   setgridcolor(colorref   color);  
void   showgrid(bool   show);
void   showminimized(bool   show);
void   closeall();
bool   setbkcolor(colorref   cr);
int   insertitem(const   lvitem*   pitem);
int   insertitem(int   nitem,lpctstr   lpszitem);
int   insertitem(int   nitem,lpctstr   lpszitem,int   nimage);
bool   setitemtext(int   nitem,int   nsubitem,lpctstr   lpsztext);
protected:
declare_message_map()

colorref   colorsymbol;
cpen   dot;
bool   m_repeatevent;
bool   m_skiptextcheck;
bool   m_grid;
bool   m_minimized;
colorref   m_gridcolor;
int   m_lastselected;
int   m_selected;

afx_msg   void   oncustomdraw(nmhdr*   pnmhdr,   lresult*   presult);
afx_msg   void   onactivate(nmhdr   *pnmhdr,   lresult   *presult);
afx_msg   void   ondblclick(nmhdr   *pnmhdr,   lresult   *presult);
void   calcnumberoflines(int   nitem,   lpctstr   lpsztext,   bool   insert   =   true);
void   opencloserow(int   row);
};
发表于:2007-09-14 23:30:0713楼 得分:20
/**********************************************************************************************/
/*function   name:   onactivate                                                                                                                                       */
/*function   definition:   expand   or   contract   the   row   when   clicked   twice                                                     */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
void   cmultilinelistctrl::onactivate(nmhdr   *pnmhdr,   lresult   *presult)
{
lpnmlistview   pnmlv   =   reinterpret_cast <lpnmlistview> (pnmhdr);
*presult   =   0;

if   (m_repeatevent)
return;

if   (getitemdata((int)pnmlv-> iitem)==1)
return;

m_repeatevent   =   true;
opencloserow((int)pnmlv-> iitem);
}

/**********************************************************************************************/
/*function   name:   ondblclick                                                                                                                                       */
/*function   definition:   avoid   to   repeat   onactivate   event                                                                               */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
void   cmultilinelistctrl::ondblclick(nmhdr   *pnmhdr,   lresult   *presult)
{
m_repeatevent   =   false;
*presult   =   0;
}

/**********************************************************************************************/
/*function   name:   insertitem   /   setitemtext                                                                                                           */
/*function   definition:   overwrite   functions   to   calculate   number   of   lines                                               */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
int   cmultilinelistctrl::insertitem(const   lvitem*   pitem)   {
int   result   =   clistctrl::insertitem(pitem);
if   (result   ==   -1)
return   -1;
calcnumberoflines(pitem-> iitem,pitem-> psztext);
return   result;
}
int   cmultilinelistctrl::insertitem(int   nitem,lpctstr   lpszitem)   {
int   result   =   clistctrl::insertitem(nitem,lpszitem);
if   (result   ==   -1)
return   -1;
calcnumberoflines(nitem,lpszitem);
return   result;
}
int   cmultilinelistctrl::insertitem(int   nitem,lpctstr   lpszitem,int   nimage)   {
int   result   =   clistctrl::insertitem(nitem,lpszitem,nimage);
if   (result   ==   -1)
return   -1;
calcnumberoflines(nitem,lpszitem);
return   result;
}
bool   cmultilinelistctrl::setitemtext(int   nitem,int   nsubitem,lpctstr   lpsztext)
{
calcnumberoflines(nitem,lpsztext,false);
return   clistctrl::setitemtext(nitem,nsubitem,lpsztext);
};

/**********************************************************************************************/
/*function   name:   calcnumberoflines                                                                                                                         */
/*function   definition:   count   number   of   lines   ( '\n ')                                                                                       */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
void   cmultilinelistctrl::calcnumberoflines(int   nitem,   lpctstr   lpsztext,   bool   insert)   {
if   (m_skiptextcheck)  
return;
        int   numlines   =   1;
for   (unsigned   int   i=0;   i <strlen(lpsztext);   i++)
if   (lpsztext[i]== '\n ')  
numlines++;
if   (insert   ¦ ¦   (int)getitemdata(nitem) <numlines)
setitemdata(nitem,   numlines);
}

/**********************************************************************************************/
/*function   name:   opencloserow                                                                                                                                   */
/*function   definition:   expand   or   contract   a   row                                                                                               */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
void   cmultilinelistctrl::opencloserow(int   row)   {
cstring   s,   str;
int   i,   j,   inserted   =   0;
m_skiptextcheck   =   true;
if   ((int)getitemdata(row)> =1   &&   (row==getitemcount()-1   ¦ ¦   (int)getitemdata(row+1)> =1))   {
//   open
for   (i=0;   i <getheaderctrl()-> getitemcount();   i++)   {
str   =   getitemtext(row,   i);
for   (j=1;   j <(int)getitemdata(row);   j++)   {
if   (str.find( '\n ')==-1)
break;
s   =   str   =   str.right(str.getlength()   -   str.find( '\n ')   -   1);  
if   (str.find( '\n ')> -1)
s   =   str.left(str.find( '\n '));  
if   (s.find( '\r ')> -1)
s   =   s.left(s.find( '\r '));
if   (j> inserted)   {
insertitem(row+j, " ");
inserted   =   j;
}
setitemtext(row+j,i,s);
setitemdata(row+j,-j);
}
}
}  
else   {
//   close
if   ((int)getitemdata(row)> 1)  
                for   (j=1;   j <(int)getitemdata(row);   j++)
deleteitem(row+1);
}
m_skiptextcheck   =   false;
invalidate();
}

/**********************************************************************************************/
/*function   name:   closeall                                                                                                                                           */
/*function   definition:   contract   all   rows                                                                                                             */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
void   cmultilinelistctrl::closeall()   {
int   i,j;
for   (i=0;   i <getitemcount();   i++)
if   ((int)getitemdata(i)> 1   &&   i <getitemcount()-1   &&   (int)getitemdata(i+1) <1)  
                for   (j=0;   j <(int)getitemdata(i);   j++)
deleteitem(i+1);
}
发表于:2007-09-14 23:30:2914楼 得分:0
/**********************************************************************************************/
/*function   name:   oncustomdraw                                                                                                                                   */
/*function   definition:   draw   list   items                                                                                                                 */
/*function   author:   carlos   andre   sanches   de   souza                                                                                             */
/*function   creation   date:   08/08/2007                                                                                                                     */
/**********************************************************************************************/
void   cmultilinelistctrl::oncustomdraw(nmhdr*   pnmhdr,   lresult*   presult)
{
*presult   =   cdrf_dodefault;
lpnmlvcustomdraw     lplvcd   =   (lpnmlvcustomdraw)pnmhdr;

switch(lplvcd-> nmcd.dwdrawstage)   {
case   cdds_prepaint   :
*presult   =   cdrf_notifyitemdraw;
return;

case   cdds_itemprepaint:
*presult   =   cdrf_notifysubitemdraw;
return;

case   cdds_subitem   ¦   cdds_prepaint   ¦   cdds_item:  
{
int   nitem   =   lplvcd-> nmcd.dwitemspec;
int   nsubitem   =   lplvcd-> isubitem;
int   nnumlines   =   (int)getitemdata(nitem);
int   ncurrentselection;
bool   changeselection   =   false;
position   posselection;
hdc   hdc   =   lplvcd-> nmcd.hdc;
cstring   str;
crect   boxrect,   bounds,   rect;
cdc*   pdc   =   cdc::fromhandle(hdc);

//   get   background   box
boxrect   =   lplvcd-> nmcd.rc;
getitemrect(   lplvcd-> nmcd.dwitemspec,   &bounds,   lvir_bounds   );
boxrect.top   =   bounds.top;
boxrect.bottom   =   bounds.bottom;
if(   nsubitem   ==   0   )
{
crect   lrect;
getitemrect(   lplvcd-> nmcd.dwitemspec,   &lrect,   lvir_label   );
boxrect.left   =   lrect.left;
boxrect.right   =   lrect.right;
}
else
{
boxrect.right   +=   bounds.left;
boxrect.left     +=   bounds.left;
}

//   get   selection
posselection   =   getfirstselecteditemposition();
if   (posselection)  
ncurrentselection   =   getnextselecteditem(posselection);
else
ncurrentselection   =   -1;
if   (ncurrentselection   !=   m_selected)   {
m_lastselected   =   m_selected;
m_selected   =   ncurrentselection;
changeselection   =   true;
}

//   fill   background   box
if   (   m_selected   ==   nitem   ¦ ¦
(nnumlines> 1   &&   nitem <getitemcount()-1   &&   (int)getitemdata(nitem+1) <1   &&   m_selected> nitem   &&   m_selected <nitem+nnumlines)   ¦ ¦
(nnumlines <1   &&   m_selected> =nitem+nnumlines   &&   m_selected <nitem+nnumlines+(int)getitemdata(nitem+nnumlines)))   {
pdc-> fillrect(boxrect,   &cbrush(::getsyscolor(color_highlight)));
pdc-> settextcolor(getsyscolor(color_highlighttext))   ;
}
else   {
pdc-> fillrect(boxrect,   &cbrush(::getsyscolor(color_window)));
pdc-> settextcolor(getsyscolor(color_windowtext))   ;
}

//   get   text   string
str   =   getitemtext(   nitem,   nsubitem   );

//   get   text   box
rect   =   boxrect;
rect.left   +=   nsubitem?6:2;

//   draw   symbol   [+]
if   (nnumlines> 1   &&   getitemtext(nitem,   nsubitem).find( '\n ')> -1   )
{
cpen   light(ps_solid,   1,   colorsymbol);
pdc-> selectobject(light);
pdc-> moveto(rect.left,rect.top+2);
pdc-> lineto(rect.left+8,rect.top+2);
pdc-> lineto(rect.left+8,rect.top+10);
pdc-> lineto(rect.left,rect.top+10);
pdc-> lineto(rect.left,rect.top+2);
pdc-> moveto(rect.left+2,rect.top+6);
pdc-> lineto(rect.left+7,rect.top+6);
if   (nitem <getitemcount()-1   &&   (int)getitemdata(nitem+1) <1)   {
pdc-> selectobject(dot);
pdc-> moveto(rect.left+4,rect.top+10);
pdc-> lineto(rect.left+4,rect.bottom);
}
else   {
pdc-> moveto(rect.left+4,rect.top+4);
pdc-> lineto(rect.left+4,rect.top+9);
}
if   (!m_minimized   ¦ ¦   (nitem <getitemcount()-1   &&   (int)getitemdata(nitem+1) <1))   {
if   (str.find( '\n ')> -1)
str   =   str.left(str.find( '\n '));
if   (str.find( '\r ')> -1)
str   =   str.left(str.find( '\r '));
}
else   {
                                str.replace( "\r\n ", "   ¦   ");
str.replace( "\n ", "   ¦   ");
}
rect.left   +=   12;
}
//   draw   dot   lines
if   (nnumlines <1   &&   getitemtext(nitem+nnumlines,   nsubitem).find( '\n ')> -1)   {
int   countlines   =   1;
cstring   text   =   getitemtext(nitem+nnumlines,   nsubitem);
for   (int   k=0;k <text.getlength();k++)
if   (text.getat(k)== '\n ')
countlines++;
if   (countlines> -nnumlines)   {
pdc-> selectobject(dot);
pdc-> moveto(rect.left+4,rect.top);
if   (countlines-1==-nnumlines)   {
pdc-> lineto(rect.left+4,rect.top+6);
pdc-> lineto(rect.left+8,rect.top+6);
}
else
pdc-> lineto(rect.left+4,rect.bottom);
}
rect.left   +=   12;
}

//   draw   text
pdc-> drawtext(   str,   rect,   dt_singleline ¦dt_noprefix ¦dt_left ¦dt_vcenter ¦dt_end_ellipsis);

//   draw   grid
if   (m_grid)   {
cpen   gridline(ps_solid,   1,   m_gridcolor);
pdc-> selectobject(gridline);
if   (nsubitem> 0)   {
pdc-> moveto(boxrect.left,   boxrect.top);
pdc-> lineto(boxrect.left,   boxrect.bottom);
}
if   (nnumlines==1   ¦ ¦   nitem==getitemcount()-1   ¦ ¦   (int)getitemdata(nitem+1)> =1)   {
pdc-> moveto(boxrect.left,   boxrect.bottom-1);
pdc-> lineto(boxrect.right,   boxrect.bottom-1);
}
}
*presult   =   cdrf_skipdefault;

//   invalidate
if   (changeselection)   {
int   topsel   =   m_selected+((m_selected> -1   &&   (int)getitemdata(m_selected) <1)?(int)getitemdata(m_selected):0);
int   toplast   =   m_lastselected+((m_lastselected> -1   &&   (int)getitemdata(m_lastselected) <1)?(int)getitemdata(m_lastselected):0);
crect   clean;
if   (topsel   !=   toplast)   {
if   (topsel   >   -1)   {
getitemrect(topsel,   &clean,   lvir_bounds);
if   ((int)getitemdata(topsel)> 1   &&   topsel <getitemcount()-1   &&   (int)getitemdata(topsel+1) <1)
{
getitemrect(topsel+getitemdata(topsel)-1,   &bounds,   lvir_bounds);
clean.bottom   =   bounds.bottom;
invalidaterect(clean,   false);
} }
if   (toplast   >   -1)   {
getitemrect(toplast,   &clean,   lvir_bounds);
if   ((int)getitemdata(toplast)> 1   &&   toplast <getitemcount()-1   &&   (int)getitemdata(toplast+1) <1)
{
getitemrect(toplast+getitemdata(toplast)-1,   &bounds,   lvir_bounds);
clean.bottom   =   bounds.bottom;
invalidaterect(clean,   false);
} } } }
return;
}
}
}
发表于:2007-09-16 17:42:4315楼 得分:2
oncustomdraw吧,clistctrl有自己的dc在lpnmlvcustomdraw     lplvcd   =   (lpnmlvcustomdraw)pnmhdr;可以获取,之后获取itemrect(getitemrect)根据rect的位置用dc来textout你要输出的字符,麻烦一点儿但不是很复杂。
发表于:2007-09-17 13:08:5116楼 得分:2
楼主还不结帖??????????????????????
发表于:2007-09-17 13:52:5717楼 得分:4
ctreelist   control

http://www.codeproject.com/treectrl/treelistctrlgerolf.asp
发表于:2007-09-23 11:12:1018楼 得分:0
http://www.codeproject.com/listctrl/expandablelistcontrol.asp?df=100&forumid=448270&exp=0&select=2183031


快速检索

最新资讯
热门点击