分类: C/C++
2008-08-04 09:35:14
color.h color.cpp ColorListCtrl.h ColorListCtrl.cpp
2、在对话框头文件中加入ColorListCtrl.h的说明,然后用CColorListCtrl代替CListCtrl声明成员变量。
3、具体的使用方法,请看示例代码中的ColoredListCtrlDlg.cpp文件。
CColorListCtrl类存在的不足:
在使用CColorListCtrl类的过程中,我体会到了随意添加条目颜色的乐趣,但同时也发现它存在的一些不足之处。下面我列出color.h和color.cpp文件代码来进行分析。color.h源代码如下所示:
// Author: SFr frydaysoft@gmx.de #ifndef AFX_COLOR_H__9FC7D36B_FC88_42FD_8DAA_2F70016038BA__INCLUDED_ #define AFX_COLOR_H__9FC7D36B_FC88_42FD_8DAA_2F70016038BA__INCLUDED_ #include "windows.h" typedef enum { DEF_BACKGROUND=0, DEF_TEXT, DEF_SELBACKGROUND, DEF_SELTEXT, DEF_DESELBACKGROUND, DEF_DESELTEXT, BLACK,BLUE,GREEN,CYAN,RED, MANGENTA,YELLOW,WHITE,DARKBLUE,DARKGREEN, BLUEGREEN,BROWN,PURPUR,OLIV,DARKGRAY, LIGHTGRAY,LIGHTBLUE,DARKWHITE,MIDDLEGRAY } ITEM_COLOR; typedef struct { char str[50]; ITEM_COLOR col; }ColorList; #define COLLIST_MAX 25 extern ColorList colList[COLLIST_MAX]; COLORREF GetColorRef(ITEM_COLOR lParam); #endif color.cpp源代码如下所示: // Author: SFr frydaysoft@gmx.de #include "stdafx.h" #include "color.h" ColorList colList[COLLIST_MAX]= { {"DEF_BACKGROUND",DEF_BACKGROUND}, {"DEF_TEXT",DEF_TEXT}, {"DEF_SELBACKGROUND",DEF_SELBACKGROUND}, {"DEF_SELTEXT",DEF_SELTEXT}, {"DEF_DESELBACKGROUND",DEF_DESELBACKGROUND}, {"DEF_DESELTEXT",DEF_DESELTEXT}, {"BLACK",BLACK}, {"BLUE",BLUE}, {"GREEN",GREEN}, {"CYAN",CYAN}, {"RED",RED}, {"MANGENTA",MANGENTA}, {"YELLOW",YELLOW}, {"WHITE",WHITE}, {"DARKBLUE",DARKBLUE}, {"DARKGREEN",DARKGREEN}, {"BLUEGREEN",BLUEGREEN}, {"BROWN",BROWN}, {"PURPUR",PURPUR}, {"OLIV",OLIV}, {"DARKGRAY",DARKGRAY}, {"LIGHTGRAY",LIGHTGRAY}, {"LIGHTBLUE",LIGHTBLUE}, {"DARKWHITE",DARKWHITE}, {"MIDDLEGRAY",MIDDLEGRAY} }; COLORREF GetColorRef(ITEM_COLOR lParam) { switch (lParam) { case DEF_BACKGROUND: return ::GetSysColor(COLOR_WINDOW); break; case DEF_TEXT: return ::GetSysColor(COLOR_WINDOWTEXT); break; case DEF_SELBACKGROUND: return ::GetSysColor(COLOR_ACTIVECAPTION); break; case DEF_SELTEXT: return ::GetSysColor(COLOR_HIGHLIGHTTEXT); break; case DEF_DESELBACKGROUND: return ::GetSysColor(COLOR_INACTIVECAPTION); break; case DEF_DESELTEXT: return ::GetSysColor(COLOR_INACTIVECAPTIONTEXT ); break; case BLACK: return RGB(0,0,0); break; //black case BLUE: return RGB(0,0,255); break; //blue case GREEN: return RGB(0,255,0); break; //green case CYAN: return RGB(0,255,255); break; //cyan case RED: return RGB(255,0,0); break; //red case MANGENTA: return RGB(255,0,255); break; //mangenta case YELLOW: return RGB(255,255,0); break; //yellow case WHITE: return RGB(255,255,255);break; //white case DARKBLUE: return RGB(0,0,128); break; //dark-blue case DARKGREEN: return RGB(0,128,0); break; //dark-green case BLUEGREEN: return RGB(0,128,128); break; //blue-green case BROWN: return RGB(128,0,0); break; //brown case PURPUR: return RGB(128,0,128); break; //purpur case OLIV: return RGB(128,128,0); break; //oliv case DARKGRAY: return RGB(128,128,128);break; //dark-gray case LIGHTGRAY: return RGB(192,192,192);break; //light-gray case LIGHTBLUE: return RGB(166,202,240);break; //light-blue case DARKWHITE: return RGB(255,251,240);break; //dark-white case MIDDLEGRAY: return RGB(160,160,164);break; //middle-gray default: return RGB(0,128,0); break; //dark-green } }由上述源代码可以看出,CColorListCtrl类给我们提供的可供填充的颜色数量十分有限,只有BLACK,BLUE,GREEN,CYAN,RED,MANGENTA,YELLOW,WHITE,DARKBLUE,DARKGREEN,BLUEGREEN,BROWN,PURPUR,
ITEM_COLOR *m_pBackColor; ITEM_COLOR *m_pTextColor; m_pBackColor=new ITEM_COLOR[column_count]; m_pTextColor=new ITEM_COLOR[column_count]; BOOL SetBackColor(ITEM_COLOR color, int iSub) { if (iSub修改后的代码: COLORREF *m_pBackColor; COLORREF *m_pTextColor; m_pBackColor=new COLORREF[column_count]; m_pTextColor=new COLORREF[column_count]; BOOL SetBackColor(COLORREF color, int iSub) { if (iSub4、在类CColorListCtrl的定义和函数说明中进行如下修改: 源代码: BOOL SetItemTextColor(ITEM_COLOR color, int Index, int iSub); BOOL SetItemBackgndColor(ITEM_COLOR color, int Index, int iSub); BOOL CColorListCtrl::SetItemBackgndColor(ITEM_COLOR color, int Index, int iSub) BOOL CColorListCtrl::SetItemTextColor(ITEM_COLOR color, int Index, int iSub)修改后的代码:BOOL SetItemTextColor(COLORREF color, int Index, int iSub); BOOL SetItemBackgndColor(COLORREF color, int Index, int iSub); BOOL CColorListCtrl::SetItemBackgndColor(COLORREF color, int Index, int iSub) BOOL CColorListCtrl::SetItemTextColor(COLORREF color, int Index, int iSub)删除的源代码:// only for TEST if (ret_index<3) { p->SetBackColor(ITEM_COLOR(ret_index BLUE),0); p->SetTextColor(ITEM_COLOR(ret_index RED ),0); } else if (ret_index<=6) { p->SetBackColor(ITEM_COLOR(ret_index BLUE),1); p->SetTextColor(ITEM_COLOR(ret_index RED ),1); } else { p->SetBackColor(ITEM_COLOR((ret_index BLUE)&0xF),2); p->SetTextColor(ITEM_COLOR((ret_index RED )&0xF),2); } // end: only for TEST5、替换如下所示的代码:DEF_BACKGROUND替换成::GetSysColor(COLOR_WINDOW); DEF_TEXT替换成::GetSysColor(COLOR_WINDOWTEXT); GetColorRef(DEF_DESELTEXT)替换成::GetSysColor(COLOR_HIGHLIGHTTEXT); GetColorRef(DEF_SELBACKGROUND)替换成::GetSysColor(COLOR_ACTIVECAPTION); GetColorRef(DEF_SELTEXT)替换成::GetSysColor(COLOR_HIGHLIGHTTEXT); GetColorRef(DEF_DESELBACKGROUND)替换成::GetSysColor(COLOR_INACTIVECAPTION); pDC->SetTextColor(pMyLParam->GetTextColor(DEF_DESELTEXT))替换成pDC->SetTextColor(pMyLParam->GetTextColor(5));修改后的示例程序如下图所示,基本功能与原来的程序一样。
本来我还计划把对CListCtrl类的排序功能加入其中,可是到实现的时候才发现异常复杂,因为在CListCtrl控件的“More Style”属性中已选中“Owner draw fixed”选项,对CListCtrl控件颜色的修改实际上是通过自画来实现的,导致了CListCtrl控件某些函数的不可使用,例如InsertItem()等等,我尝试做了一下排序,总是会出现一些无法理解的错误,照我的理解只能把项全部存进列表中,然后对列表进行排序,再清空CListCtrl控件的所有项罗列出已排序的项。如果哪位高手有独到的解决方法,请不吝指教!
下载本文示例代码