我在CSDn文档中心看到如下东西,不太明白,求助大家给解释一下如何创建的好吗?
经常使用ClistCtrl来显示大量的数据,但是总感觉焦点行的背景颜色为蓝色,显示的效果挺难看的,参考《MFC技术内幕》这本书中的例子,自己修改了ClistCtrl的OnPaint代码,把焦点行改为白色的背景,加了一个矩形的边框,下面实际的效果,自我感觉还不错;
    实现思路:1、  首先调用Default,让ClistCtrl自己画2、  调用GetDC()函数获得DC3、  调用GetFont()获得当前字体4、  使用GetSubItemRect函数获得每一个列的矩形大小,然后使用DrawText函数把文字画出来5、  最后使用Rectangle函数画出边框,这样就达到了图中的效果了下面是主要的代码void CXListCtrl::OnPaint(){            Default();         CRect rect;                        int nSel=GetCurSel();       if (nSel<0)      return;          CDC *dc=GetDC(); // don't use CPaintDC          CFont * pFont=GetFont();       //把背景刷为白色       GetItemRect(nSel,rect,LVIR_BOUNDS);       CBrush brush(RGB(255,255,255));          CBrush * pOldBrush=dc->SelectObject(&brush);          dc->Rectangle(rect.left,rect.top,rect.right,rect.bottom);          //开始画焦点行的每一列文字          CFont * pOldFont=dc->SelectObject(pFont);          CString strTemp;          int nColumns=GetColumns();           HDITEM hditem;       UINT nFormat;          int nFmt;                 for (int i=0;i<nColumns;i++)          {                 strTemp=GetItemText(nSel,i);                 GetSubItemRect(nSel, i, LVIR_BOUNDS, rect);               //得到每一列文字的对齐方式                     hditem.mask = HDI_FORMAT;                     m_HeaderCtrl.GetItem(i, &hditem);                                          nFmt = hditem.fmt & HDF_JUSTIFYMASK;                                          nFormat= DT_VCENTER | DT_SINGLELINE;                     if (nFmt == HDF_CENTER)                            nFormat |= DT_CENTER;                     else if (nFmt == HDF_LEFT)                            nFormat |= DT_LEFT;                     else                            nFormat |= DT_RIGHT;                     dc->DrawText(strTemp, &rect, nFormat);                   }          //画边框          CPen * pOldPen=dc->SelectObject(m_SelPen);          GetItemRect(nSel,rect,LVIR_BOUNDS);             dc->SelectStockObject(NULL_BRUSH); //设置NULL_BRUSH          dc->Rectangle(rect.left,rect.top,rect.right,rect.bottom);       dc->SelectObject(pOldPen);          dc->SelectObject(pOldBrush);       dc->SelectObject(pFont);} 在实现过程中,要注意的问题:1、  焦点行的字体要和其他行的字体一样2、  每一列的对齐方式3、  画完焦点行的文字以后,再画外边框 当然,这只是一个种效果,还可以画出另外自己需要的效果;

解决方案 »

  1.   

    为什么要用CDC * dc->getdc() 而不是CPaintDc 它们之间有什么区别?
    还有怎么取得原来字体的设置方式。以上代码不能用!
      

  2.   

    LVIR_LABEL 和 LVIR_BOUNDS 有什么区别?
      

  3.   

    用自画不是更简单void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
      

  4.   

    LVIR_BOUNDS   Returns the bounding rectangle of the entire item, including the icon and label.
    LVIR_ICON   Returns the bounding rectangle of the icon or small icon.
    LVIR_LABEL   Returns the bounding rectangle of the item text.