使用CustomDraw来自绘CListCtrl控件

Custom Draw 基础
  我将会尽我所能把Custom Draw的处理描述清楚,而不是简单的引用MSDN的文档。这些例子都需要你的程序有一个ListCtrl在对话框上,并且这个ListCtrl处于Report和多列模式。
Custom draw可以被想象成一个轻量级的,容易使用的重绘方法(重绘方法还有几种,例如Owner Draw等)。这种容易来自于我们只需要处理一个消息(NM_CUSTOMDRAW),就可以让Windows为你干活了,你就不用被逼去处理"重绘过程"中所有的脏活了。
Custom Draw 的消息映射入口
  Custom draw 是一个类似于回调的处理过程,Windows在绘制List Ctrl的某个时间点上通过 Notification 消息通知你的程序,你可以选择忽略所有的通知(这样你就会看到标准的ListCtrl),或者处理某部分的绘制(实现简单的效果),甚至整个的控件都由你来绘制(就象使用Owner-Drawing一样)。这个机制的真正卖点是:你只需要实现一些你需要的,其余的可以让Windows为你代劳。
开始使用Custom draw
首先添加一个消息映射:
1.在xxxx.cpp文件中的BEGIN_MESSAGE_MAP下添加,其中IDC_LIST1是CListCtrl控件的ID
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, OnCustomdrawList)//cjz

BEGIN_MESSAGE_MAP(CDemoListDlg, CDialog)
//{{AFX_MSG_MAP(CDemoListDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, OnCustomdrawList)//cjz
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
2.在xxxx.h文件类的声明内添加
public:
afx_msg void OnCustomdrawList(NMHDR*, LRESULT*);//cjz

3.在xxxx.cpp中写函数实现

 1 voidCRollMainView::OnCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult)
 2 {
3 //////////////////////////////////////////////////////
4 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
5
6 // Take the default processing unless we
7
8 // set this to something else below.
9
10 *pResult = CDRF_DODEFAULT;
11
12 // First thing - check the draw stage. If it's the control's prepaint
13
14 // stage, then tell Windows we want messages for every item.
15
16
17 if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
18 {
19 *pResult = CDRF_NOTIFYITEMDRAW;
20 }
21 else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
22 {
23 COLORREF crText,crBk;
24      //奇偶判断
25 if ( (pLVCD->nmcd.dwItemSpec % 2) == 0 ){
26 crText = RGB(0,0,0);//RGB(32,32,255);
27 crBk =RGB(229,232,239);
28 }
29 else if ( (pLVCD->nmcd.dwItemSpec % 2) == 1 ){
30 crText = RGB(0,0,0);
31 crBk = RGB(240,247,249);
32 }
33 else{
34 crText = RGB(0,0,0);
35 crBk = RGB(0,0,126);
36 }
37
38 // Store the color back in the NMLVCUSTOMDRAW struct.
39
40 pLVCD->clrText = crText;
41 pLVCD->clrTextBk = crBk;
42 //设置选择项的颜色
43 if( this->m_ctrlList.GetItemState(pLVCD->nmcd.dwItemSpec, CDIS_SELECTED) ){
44 crBk =RGB(75, 149, 229);//itunes//RGB(10, 36, 106);//RGB(0, 0, 64);
45 crText = RGB(255,255,255);
46 pLVCD->clrText = crText;
47 pLVCD->clrTextBk = crBk;
48 *pResult = CDRF_NEWFONT;
49 }
50 if(LVIS_SELECTED == m_ctrlList.GetItemState(pLVCD->nmcd.dwItemSpec,LVIS_SELECTED))
51 {
52 //清除选择状态,如果不清除的话,还是会显示出蓝色的高亮条
53 BOOL b = m_ctrlList.SetItemState(pLVCD->nmcd.dwItemSpec,0,LVIS_SELECTED);
54 pLVCD->clrText = crText;
55 pLVCD->clrTextBk = crBk;
56
57 *pResult = CDRF_NEWFONT;
58 return;
59 }
60 *pResult = CDRF_NEWFONT;
61 //*pResult = CDRF_DODEFAULT;
62 }
63 }

经过以上三步就完成了Custom draw消息的添加,可以实现一些简单的美化效果。

一些需要注意的细节

  如果你有一个从ClistCtr派生的类,你想为它添加custom draw,你就可以使用ON_NOTIFY_REFLECT来代替。如下:

ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomdraw )

OnCustomdraw的原形和上面的函数一致,但它是声明在你的派生类里的。

 

 

 

 

posted on 2011-11-06 19:42  媳妇是阿东  阅读(8160)  评论(1)    收藏  举报

导航