学习C++中的CustomDraw

来源:互联网 发布:sql触发器实例 old 编辑:程序博客网 时间:2024/05/29 16:37

Custom   Draw   With   List   View   and   Tree   View   Controls   
  Most   common   controls   can   be   handled   in   essentially   the   same   way.   However,   the   list   view   and   tree   view   controls   have   some   features   that   require   a   somewhat   different   approach   to   custom   draw.   
    
  For   Version   5.0   of   the   common   controls,   these   two   controls   may   display   clipped   text   if   you   change   the   font   by   returning   CDRF_NEWFONT.   This   behavior   is   necessary   for   backward   compatibility   with   earlier   versions   of   the   common   controls.   If   you   want   to   change   the   font   of   a   list   view   or   tree   view   control,   you   will   get   better   results   if   you   send   a   CCM_SETVERSION   message   with   the   wParam   value   set   to   5   before   adding   any   items   to   the   control.   
    
  Custom   Draw   With   List   View   Controls   
  Because   list   view   controls   have   subitems   and   multiple   display   modes,   you   will   need   to   handle   the   NM_CUSTOMDRAW   notification   somewhat   differently   than   for   the   other   common   controls.   
    
  For   report   mode:   
    
  The   first   NM_CUSTOMDRAW   notification   will   have   the   dwDrawStage   member   of   the   associated   NMCUSTOMDRAW   structure   set   to   CDDS_PREPAINT.   Return   CDRF_NOTIFYITEMDRAW.     
  You   will   then   receive   an   NM_CUSTOMDRAW   notification   with   dwDrawStage   set   to   CDDS_ITEMPREPAINT.   If   you   specify   new   fonts   or   colors   and   return   CDRF_NEWFONT,   all   subitems   of   the   item   will   be   changed.   If   you   want   instead   to   handle   each   subitem   separately,   return   CDRF_NOTIFYSUBITEMDRAW.     
  If   you   returned   CDRF_NOTIFYITEMDRAW   in   the   previous   step,   you   will   then   receive   an   NM_CUSTOMDRAW   notification   for   each   subitem   with   dwDrawStage   set   to   CDDS_SUBITEM   |   CDDS_PREPAINT.   To   change   the   font   or   color   for   that   subitem,   specify   a   new   font   or   color   and   return   CDRF_NEWFONT.     
  For   the   large   icon,   small   icon,   and   list   modes:   
    
  The   first   NM_CUSTOMDRAW   notification   will   have   the   dwDrawStage   member   of   the   associated   NMCUSTOMDRAW   structure   set   to   CDDS_PREPAINT.   Return   CDRF_NOTIFYITEMDRAW.     
  You   will   then   receive   an   NM_CUSTOMDRAW   notification   with   dwDrawStage   set   to   CDDS_ITEMPREPAINT.   You   can   change   the   fonts   or   colors   of   an   item   by   specifying   new   fonts   and   colors   and   returning   CDRF_NEWFONT.   Because   these   modes   do   not   have   subitems,   you   will   not   receive   any   additional   NM_CUSTOMDRAW   notifications.     
  An   example   of   a   list   view   NM_CUSTOMDRAW   notification   handler   is   given   in   the   next   section.   
    
  Using   Custom   Draw   
  The   following   code   fragment   is   a   portion   of   a   WM_NOTIFY   handler   that   illustrates   how   to   handle   custom   draw   notifications   sent   to   a   list   view   control:   
    
  LPNMLISTVIEW     pnm         =   (LPNMLISTVIEW)lParam;   
    
  switch   (pnm->hdr.code){   
  ...   
  case   NM_CUSTOMDRAW:   
    
          LPNMLVCUSTOMDRAW     lplvcd   =   (LPNMLVCUSTOMDRAW)lParam;   
    
          switch(lplvcd->nmcd.dwDrawStage)   {   
          case   CDDS_PREPAINT   :   
                  return   CDRF_NOTIFYITEMDRAW;   
    
          case   CDDS_ITEMPREPAINT:   
                  SelectObject(lplvcd->nmcd.hdc,   
                                            GetFontForItem(lplvcd->nmcd.dwItemSpec,   
                                                                          lplvcd->nmcd.lItemlParam)   );   
                  lplvcd->clrText   =   GetColorForItem(lplvcd->nmcd.dwItemSpec,   
                                                                                      lplvcd->nmcd.lItemlParam);   
                  lplvcd->clrTextBk   =   GetBkColorForItem(lplvcd->nmcd.dwItemSpec,   
                                                                                              lplvcd->nmcd.lItemlParam);   
    
    
  /*   At   this   point,   you   can   change   the   background   colors   for   the   item   
  and   any   subitems   and   return   CDRF_NEWFONT.   If   the   list   view   control   
  is   in   report   mode,   you   can   simply   return   CDRF_NOTIFYSUBITEMREDRAW   
  to   customize   the   item's   subitems   individually   */   
    
                  ...   
                  return   CDRF_NEWFONT;   
  //     or   return   CDRF_NOTIFYSUBITEMREDRAW;   
    
          case   CDDS_SUBITEM   |   CDDS_ITEMPREPAINT:   
                  SelectObject(lplvcd->nmcd.hdc,   
                                            GetFontForSubItem(lplvcd->nmcd.dwItemSpec,   
                                                                                lplvcd->nmcd.lItemlParam,   
                                                                                lplvcd->iSubItem));   
                  lplvcd->clrText   =   GetColorForSubItem(lplvcd->nmcd.dwItemSpec,   
                                                                                            lplvcd->nmcd.lItemlParam,   
                                                                                            lplvcd->iSubItem));   
                  lplvcd->clrTextBk   =   GetBkColorForSubItem(lplvcd->nmcd.dwItemSpec,   
                                                                                                    lplvcd->nmcd.lItemlParam,   
                                                                                                    lplvcd->iSubItem));   
    
  /*   This   notification   is   received   only   if   you   are   in   report   mode   and   
  returned   CDRF_NOTIFYSUBITEMREDRAW   in   the   previous   step.   At   
  this   point,   you   can   change   the   background   colors   for   the   
  subitem   and   return   CDRF_NEWFONT.*/   
    
                  ...   
                  return   CDRF_NEWFONT;           
          }   
  ...   
  }   
    
  The   first   NM_CUSTOMDRAW   notification   has   the   dwDrawStage   member   of   the   NMCUSTOMDRAW   structure   set   to   CDDS_PREPAINT.   The   handler   returns   CDRF_NOTIFYITEMDRAW   to   indicate   that   it   wishes   to   modify   one   or   more   items   individually.   The   control   then   sends   an   NM_CUSTOMDRAW   notification   with   dwDrawStage   set   to   CDDS_PREPAINT   for   each   item.   The   handler   returns   CDRF_NOTIFYITEMDRAW   to   indicate   that   it   wishes   to   modify   the   item.   
    
  If   CDRF_NOTIFYITEMDRAW   was   returned   in   the   previous   step,   the   next   NM_CUSTOMDRAW   notification   has   dwDrawStage   set   to   CDDS_ITEMPREPAINT.   The   handler   gets   the   current   color   and   font   values.   At   this   point,   you   can   specify   new   values   for   small   icon,   large   icon,   and   list   modes.   If   the   control   is   in   report   mode,   you   can   also   specify   new   values   that   will   apply   to   all   subitems   of   the   item.   If   you   have   changed   anything,   return   CDRF_NEWFONT.   If   the   control   is   in   report   mode   and   you   want   to   handle   the   subitems   individually,   return   CDRF_NOTIFYSUBITEMREDRAW.   
    
  The   final   notification   is   only   sent   if   the   control   is   in   report   mode   and   you   returned   CDRF_NOTIFYSUBITEMREDRAW   in   the   previous   step.   The   procedure   for   changing   fonts   and   colors   is   the   same   as   that   step,   but   it   only   applies   to   a   single   subitem.   Return   CDRF_NEWFONT   to   notify   the   control   if   the   color   or   font   was   changed.  

原创粉丝点击