`
daojin
  • 浏览: 697817 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

学习C++中的CustomDraw

阅读更多

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.  

分享到:
评论

相关推荐

    visual c++资源合集.zip_MFC 自绘列表_mfc自绘列表框_列表框等几乎MFC的所有基本控件_包括对话框_按钮

    在IT领域,尤其是在Windows应用程序开发中,Visual C++与MFC(Microsoft Foundation Classes)框架是不可或缺的工具。这个“visual c++资源合集.zip”压缩包显然提供了关于使用MFC进行自定义绘制(自绘)控件的详细...

    cxGridChartView例子

    "cxGridChartView"是DevExpress组件库中的一个高级特性,主要用于在Delphi或C++ Builder开发的Windows应用程序中展示数据的图表视图。这个例子旨在帮助开发者了解如何利用cxGridChartView来创建交互式的数据可视化...

    VC开发树列表

    当需要更新颜色时,控件会发送LVN_ITEMCHANGED或NM_CUSTOMDRAW消息,开发者需要在相应消息处理函数中判断并处理颜色变更。 在VC++中,创建和定制这样的树列表控件通常涉及以下步骤: 1. 在资源编辑器中添加树列表...

    ListCtrlExp.zip

    在Windows编程领域,MFC(Microsoft Foundation Classes)是一个强大的库,它为开发人员提供了一种在C++中构建Windows应用程序的便捷方式。本项目“ListCtrlExp.zip”聚焦于MFC中的一个重要组件——ListCtrl,这是一...

    无边框/有边框菜单自绘Demo

    在编程领域,自绘(Custom Draw)是一种技术,允许开发者完全控制控件的外观和行为,以实现独特的用户界面设计。本示例“无边框/有边框菜单自绘Demo”聚焦于如何利用编程技巧在Windows操作系统下创建自定义的菜单,...

    wtl_listctrl

    7. **Custom Draw**:如果需要自定义List Control的绘制,可以利用其自绘特性,处理NM_CUSTOMDRAW通知。 8. **Virtual Mode**:对于大数据集,可以使用虚拟模式来提高性能,仅在需要时计算和显示列表项。 通过`WTL...

    VividTree_demo.zip

    在Windows编程中,自绘(Custom Draw)是指应用程序控制控件的绘制过程,而不是完全依赖操作系统默认的绘制方式。通过自绘,开发者可以实现更加个性化和复杂的效果,如本例中的“背景图案”和“子项自绘风格”。自绘...

    WTL教程之List Control

    5. **自定义绘制**: 如果需要更复杂的绘制效果,比如自定义项的背景色或图标,可以处理NM_CUSTOMDRAW消息,并在其中进行绘制操作。 6. **数据绑定**: 在更复杂的应用中,List Control的数据可能来自数据库或其他...

    vc非常漂亮列表类

    而“对表头和列表单元进行颜色添加”可能是通过重绘或消息处理机制实现的,例如利用OnDrawItem和OnDrawColumnHeader成员函数来绘制列表项和列头,或者响应LVN_COLUMNCLICK和NM_CUSTOMDRAW消息,以实现动态的颜色变化...

    VC树形控件应用

    在Microsoft Visual C++(简称VC)开发环境中,树形控件(Tree Control)是一种常见的用户界面元素,用于显示层次结构的数据。它常被用于文件管理器、设置面板或自定义菜单等场景。本主题将深入探讨如何在VC中有效地...

    可以排序 修改颜色 MFC LIST

    2. 为List Control添加自定义绘制支持,通过处理`NM_CUSTOMDRAW`通知消息。在这个消息处理函数中,你可以获取到绘图对象(如`NMLVCUSTOMDRAW`结构),然后使用其成员函数如`SetTextColor`和`SetBkColor`来设置文字和...

    ComboBox控件带工具提示项.zip_COMBOBOX_ComBoBox VC_VC 界面_VC 界面编程_VC界面编程

    要实现`ComboBox`的工具提示功能,开发者通常需要在控件上处理WM_NOTIFY消息,特别是NM_CUSTOMDRAW子消息,以便在绘制控件时添加工具提示。此外,还需要处理WM_MOUSEHOVER和WM_MOUSELEAVE消息,以控制工具提示的显示...

    一个样本的自定义绘制按钮控制演示

    自定义绘制(Custom Draw)是MFC提供的一种机制,允许开发者对控件的外观进行精细定制,以满足特定的界面设计需求。通常,Windows控件的绘制是由操作系统负责的,但自定义绘制则将这个责任交给了程序员,让你可以...

    CartoonDialogTest.rar

    2. **自绘(Custom Draw)**:为了实现非标准的动画效果,我们需要覆盖CDialog类的OnPaint方法,进行自定义绘制。在这个过程中,可以使用GDI(Graphics Device Interface)或者GDI+来绘制对话框的各个部分,如背景、...

    CustomButtonDemo

    【描述】"a sample custom draw button control demo." 暗示了这个项目的核心在于自定义绘制按钮。在默认情况下,Windows系统中的按钮控件具有标准的外观和行为,但通过自定义绘制,开发者可以改变按钮的外观,如...

    ResizeScrollbar_demo

    此外,可能还会涉及自绘(Custom Draw)技术,以自定义滚动条的外观和行为。 6. **调试与测试**:项目中很可能包含了调试步骤,比如设置断点、使用Visual Studio的调试工具来检查滚动条的大小变化和用户交互情况,...

    重绘MFC列表控件(ListCtrl)字体 表头和行高度 行颜色等

    最后,为了在鼠标悬停时改变行颜色,你需要处理`NM_CUSTOMDRAW`通知,通常在`OnCustomDraw`函数中完成。这里需要检查通知的类型和当前的状态,然后设置相应的颜色。 ```cpp void CMyListCtrl::OnCustomDraw(NMHDR* ...

    test_list_control_mfc虚拟列表_MFClist_visualc++_MFClistctrl_listcont

    4. **通知消息处理**:处理来自列表控件的消息,如NM_CUSTOMDRAW,这允许你在控件绘制时进行自定义操作。 5. **数据存储**:由于数据不在控件内部存储,你需要自己管理数据。这通常涉及到一个数据结构,如链表或...

    自绘按钮(VC+XP)

    在Windows编程中,自绘按钮(Custom Draw Button)是一种允许开发者根据自己的需求定制按钮外观的技术。在本案例中,我们关注的是使用Visual C++(VC)进行开发,并且该按钮设计适应于Windows XP操作系统。自绘按钮...

    自绘MFC各种基本控件

    自绘(Custom Draw)是MFC中的一种技术,允许程序员控制控件的绘制过程,从而实现自定义的视觉效果。本资源包"SkinControls(自绘MFC基本控件 )"显然包含了一系列用于自绘MFC基本控件的代码示例,对于学习和理解如何...

Global site tag (gtag.js) - Google Analytics