RT,我要把聊天记录中发送出去的消息和接收到的消息用两种不同颜色的字体表示
比如我发送出去的都以"Re:"开头,用红色表示
其他都用绿色
就这个功能就OK了,请问要如何改写DrawItem函数?
改了一晚都改不好,没信心了....求达人给示例代码(最好就是只实现改变字体颜色功能而已),越详细越好,谢谢

解决方案 »

  1.   

    首先将listbox的风格设置成ownerdraw,然后在drawitem中用settextcolor自己textout文字(自己判断内容是否以RE开头)
    自画风格的Listbox,重载   DrawItem()函数   
      class   CCustomListBox   :   public   CListBox   
      {   
      public:   
      //   Operations   
              DECLARE_DYNCREATE(CCustomListBox)   
              int   AddLBItem(LPSTR);   
              void   HandleSelectionState(LPDRAWITEMSTRUCT   lpdis);   
              void   HandleFocusState(LPDRAWITEMSTRUCT   lpdis);   
              virtual   void   DrawItem(LPDRAWITEMSTRUCT   lpDIS);   
      };   
        
      cpp   file   
        
      IMPLEMENT_DYNCREATE(CCustomListBox,   CListBox)   
        
      int   CCustomListBox::AddLBItem(LPSTR   itemStr)   
      {   
              AddString((LPCSTR)itemStr);   
              return   0;   
      }   
        
      void   CCustomListBox::DrawItem(LPDRAWITEMSTRUCT   lpDIS)   
      {   
              CDC*   pDC   =   CDC::FromHandle(lpDIS->hDC);   
        
              if   ((lpDIS->itemState   &   ODS_SELECTED)   &&   
                      (lpDIS->itemAction   &   (ODA_SELECT   |   ODA_DRAWENTIRE)))   
              {   
                      pDC->InvertRect(&lpDIS->rcItem);   
                      pDC->DrawFocusRect(&lpDIS->rcItem);   
              }   
        
              if   (!(lpDIS->itemState   &   ODS_SELECTED)   &&   
                      (lpDIS->itemAction   &   ODA_SELECT))   
              {   
                      pDC->InvertRect(&lpDIS->rcItem);   
                      pDC->DrawFocusRect(&lpDIS->rcItem);   
              }   
      }   
        
      void   CCustomListBox::HandleSelectionState(LPDRAWITEMSTRUCT   lpdis)   
      {   
      //   Ordinarily   could   check   for   "if   (lpdis->itemState   &   ODS_SELECTED)"   
      //   and   do   drawing   for   selected   state,   "else"   draw   non-selected   state.   
      //   But   second   call   to   InvertRect   restores   rectangle   to   original   
      //   state,   so   will   just   call   function   whether   selected   or   unselected.   
        
              ::InvertRect   (lpdis->hDC,   (LPRECT)&lpdis->rcItem);   
      }   
        
      void   CCustomListBox::HandleFocusState(LPDRAWITEMSTRUCT   lpdis)   
      {   
      //   Ordinarily   would   check   for   "if   (lpdis->itemState   &   ODS_FOCUS)"   
      //   and   do   drawing   for   focus   state,   "else"   draw   non-focus   state.   
      //   But   second   call   to   DrawFocusRect   restores   rectangle   to   original   
      //   state,   so   will   just   call   function   whether   focus   or   non-focus.   
      //   New   to   Windows   3.0,   this   function   draws   a   black   dashed-rect   
      //   border   on   the   border   of   the   specified   rectangle   
        
              ::DrawFocusRect(   lpdis->hDC,   (LPRECT)   &lpdis->rcItem   );   
      }
      

  2.   

    http://www.vckbase.com/document/viewdoc/?id=1480
      

  3.   


    void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    CDC*  pDC  =  CDC::FromHandle(lpDrawItemStruct->hDC);  
         CString str=(char *)lpDrawItemStruct->itemData; if (str.GetAt(0)=='R'&&str.GetAt(1)=='e'&&str.GetAt(2)==' ')
    {
    pDC->SetTextColor(RGB(255,0,0));
    pDC->DrawText(str,&(lpDrawItemStruct->rcItem),DT_LEFT);
    }

    }
    昨晚整个试验工程我都删了,我大概就这种思路吧,但是Rect不知道该怎么传