struct SColorAndName
{
SColorAndName()
{
ZeroMemory(this,sizeof(SColorAndName));
}
SColorAndName(COLORREF crColor,PCSTR cpColor)
{ ZeroMemory(this,sizeof(SColorAndName));
m_ColorValue=crColor;
strcpy(m_ColorName,cpColor);///设置颜色名称
}
COLORREF m_ColorValue;
char m_ColorName[16];
};
// CMyComBoxColor
class CMyComBoxColor : public CComboBox
{
DECLARE_DYNAMIC(CMyComBoxColor)
public:
CMyComBoxColor();
virtual ~CMyComBoxColor();
protected:
DECLARE_MESSAGE_MAP()
public:
bool m_bInitialize;
CString m_SelectColorName;
static SColorAndName m_AllColorAndName[16]; /3中颜色
private:
void Initialize(void);
public:
COLORREF GetSelectColorValue(void); 获得选择的颜色值
CString GetSelectColorName(void); ///获得选择的颜色名称
void SetSelectedColorValue(COLORREF crClr);
void SetSelectedColorName(PCSTR cpColor);
bool RemoveColor(PCSTR cpColor);
bool RemoveColor(COLORREF crClr);
int AddColor(PCSTR cpName,COLORREF crClr);
设置指定颜色值对应的列表项的颜色值
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void PreSubclassWindow();
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
// MyComBoxColor.cpp : 实现文件
//
#include "stdafx.h"
#include "listColor.h"
#include "MyComBoxColor.h"
#include ".\mycomboxcolor.h"
// CMyComBoxColor
SColorAndName CMyComBoxColor::m_AllColorAndName[16]=
{
SColorAndName( RGB( 0x00, 0x00, 0x00 ), _T("黑")),
SColorAndName( RGB( 0x80, 0x00, 0x00 ), _T("紅")),
SColorAndName( RGB( 0x00, 0x80, 0x00 ), _T("绿")),
SColorAndName( RGB( 0x80, 0x80, 0x00 ), "Olive"),
SColorAndName( RGB( 0x00, 0x00, 0x80 ), "Navy" ),
SColorAndName( RGB( 0x80, 0x00, 0x80 ), "Purple"),
SColorAndName( RGB( 0x00, 0x80, 0x80 ), "Teal" ),
SColorAndName( RGB( 0x80, 0x80, 0x80 ), "Grey" ),
SColorAndName( RGB( 0xC0, 0xC0, 0xC0 ), "Silver"),
SColorAndName( RGB( 0xFF, 0x00, 0x00 ), "Red" ),
SColorAndName( RGB( 0x00, 0xFF, 0x00 ), "Lime" ),
SColorAndName( RGB( 0xFF, 0xFF, 0x00 ), "Yellow"),
SColorAndName( RGB( 0x00, 0x00, 0xFF ), "Blue"),
SColorAndName( RGB( 0xFF, 0x00, 0xFF ), "Fushcia"),
SColorAndName( RGB( 0x00, 0xFF, 0xFF ), "Aqua"),
SColorAndName( RGB( 0xFF, 0xFF, 0xFF ), "White"),
};
IMPLEMENT_DYNAMIC(CMyComBoxColor, CComboBox)
CMyComBoxColor::CMyComBoxColor()
{
m_bInitialize=false;
}
CMyComBoxColor::~CMyComBoxColor()
{
}
BEGIN_MESSAGE_MAP(CMyComBoxColor, CComboBox)
ON_WM_CREATE()
END_MESSAGE_MAP()
// CMyComBoxColor 消息处理程序
void CMyComBoxColor::Initialize(void )
{
int iAddedItem=-1;
if(m_bInitialize)
return;
for(int i=0;i <16;i++)
{
// InsertString(i,m_AllColorAndName[i].m_ColorName);
iAddedItem=AddString(m_AllColorAndName[i].m_ColorName);
if(iAddedItem==CB_ERRSPACE)
break;
else
SetItemData(iAddedItem,m_AllColorAndName[i].m_ColorValue);
}
m_bInitialize=true;
}
获得选择的颜色值
COLORREF CMyComBoxColor::GetSelectColorValue(void)
{
int iSelectedItem=GetCurSel();
if(iSelectedItem==CB_ERR)
return(RGB(0,0,0));
return (GetItemData(iSelectedItem));
};
CString CMyComBoxColor::GetSelectColorName(void){
int iSelectedItem=GetCurSel();
if(iSelectedItem==CB_ERR)
return(m_SelectColorName);
GetLBText(iSelectedItem,m_SelectColorName);
return(m_SelectColorName);
};
bool CMyComBoxColor::RemoveColor(PCSTR cpColor)
{
int iItems=GetCount();
CString sColor;
bool bRemoved=false;
for(int iItem =0;iItem <iItems;iItem++)
{
GetLBText(iItem,sColor);
if(!sColor.CompareNoCase(m_SelectColorName))
{
DeleteString(iItem);
bRemoved=true;
break;
}
}
return bRemoved;
};
bool CMyComBoxColor::RemoveColor(COLORREF crClr)
{
int iItems=GetCount();
CString sColor;
bool bRemoved=false;
for(int iItem =0;iItem <iItems;iItem++)
{
GetLBText(iItem,sColor);
if(crClr==GetItemData(iItem))
{
DeleteString(iItem);
bRemoved=true;
break;
}
}
return bRemoved;
};
int CMyComBoxColor::AddColor(PCSTR cpName,COLORREF crClr)
{
int iItem=-1;
iItem=InsertString(-1,cpName);
if(iItem!=LB_ERR)
SetItemData(iItem,crClr);
return iItem;
};
void CMyComBoxColor::SetSelectedColorValue(COLORREF crClr)
{
int iItems=GetCount();
for(int item=0;item<iItems;item++)
{
if(crClr==GetItemData(item))
SetCurSel(item);
}
return;
};
void CMyComBoxColor::SetSelectedColorName(PCSTR cpColor)
{
int iItems=GetCount();
CString sColorName;
for(int item=0;item<iItems;item++)
{
GetLBText(item,sColorName);
if(sColorName.CompareNoCase(cpColor))
SetCurSel(item);
}
return;
};
void CMyComBoxColor::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC dc;
CRect rItemRect(lpDrawItemStruct->rcItem);
CRect rBlockRect(rItemRect); ///列表项矩形
CRect rTextRect(rBlockRect);
CBrush bRrush;
int iIditem=lpDrawItemStruct->itemID;
int iAction=lpDrawItemStruct->itemAction;
int istate=lpDrawItemStruct->itemState;
int iFourthWidth=(rBlockRect.Width()*0.7);
COLORREF crColor=0;
COLORREF crNormal=GetSysColor(COLOR_WINDOW);
COLORREF crSelect=GetSysColor(COLOR_HIGHLIGHT);
COLORREF crText=GetSysColor(COLOR_WINDOWTEXT);
static CString sColor;
if(!dc.Attach(lpDrawItemStruct->hDC))
return;
///1/4宽
bRrush.CreateStockObject(BLACK_BRUSH); 创建黑色画刷
如果该条被选择
if(istate & ODS_SELECTED)
{
dc.SetTextColor( (0x00FFFFFF &~(crText)));
dc.SetBkColor(crSelect);
dc.FillSolidRect(&rBlockRect,crSelect);
}
else
{
dc.SetTextColor(crText );
dc.SetBkColor(crNormal);
dc.FillSolidRect(&rBlockRect,crNormal);
}
/处于焦点状态
if(istate & ODS_FOCUS)
{
dc.DrawFocusRect(&rItemRect);
rTextRect.left+=(iFourthWidth+20);
rTextRect.top+=1;
}
计算图像区域
rBlockRect.DeflateRect(CSize(2,2));
rBlockRect.right=iFourthWidth;
if(iIditem!=-1)
{
GetLBText(iIditem,sColor);
///处于不可用状态
if(istate & ODS_DISABLED)
{
crColor=GetSysColor(COLOR_INACTIVECAPTIONTEXT);
dc.SetTextColor(crColor);
}
else //处于一般状态
crColor=GetItemData(iIditem);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(iFourthWidth+6,rTextRect.top, sColor);
dc.FillSolidRect(&rBlockRect,crColor);
dc.FrameRect(&rBlockRect,&bRrush);
}
dc.Detach();
// TODO: 添加您的代码以绘制指定项
}
void CMyComBoxColor::PreSubclassWindow()
{
Initialize();
// TODO: 在此添加专用代码和/或调用基类
CComboBox::PreSubclassWindow();
SetCurSel(0);
return;
}
int CMyComBoxColor::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CComboBox::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
return 0;
}
HBRUSH ClistColorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
LONG wndID=::GetWindowLong(pWnd->GetSafeHwnd(),GWL_ID);
switch(nCtlColor)
{
case CTLCOLOR_EDIT : //对所有编辑框的设置
if(wndID==IDC_EDIT1 || wndID==IDC_TREE1 ||wndID==IDC_RADIO1)
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,255,0));
pDC->SetBkColor(m_cmbCor.GetSelectColorValue());
HBRUSH B = CreateSolidBrush(m_cmbCor.GetSelectColorValue() );
return (HBRUSH) B;
}
case CTLCOLOR_BTN:
if( wndID==IDC_BUTTON4)
{
/* AfxMessageBox("...");
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,255,0))*/;
pDC->SetBkColor(m_cmbCor.GetSelectColorValue());
HBRUSH B = CreateSolidBrush(m_cmbCor.GetSelectColorValue() );
return (HBRUSH) B;
}
case CTLCOLOR_LISTBOX:
if(wndID==IDC_LIST1 || wndID==IDC_LIST2 )
{
// AfxMessageBox("...");
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,255,0));
pDC->SetBkColor(m_cmbCor.GetSelectColorValue());
HBRUSH B = CreateSolidBrush(m_cmbCor.GetSelectColorValue() );
return (HBRUSH) B;
}
case CTLCOLOR_STATIC:
if(wndID==IDC_STATIC)
{
// AfxMessageBox("...");
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,155,0));
}
case CTLCOLOR_DLG:
if(wndID==IDD_LISTCOLOR_DIALOG)
{
}
default:
return hbr;
}
//SetTimer(1,300)
//SetTimer(1,300,0);
return hbr;
}