📄 clrcombobox.cpp
字号:
// ClrComboBox.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "ClrComboBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClrComboBox
CClrComboBox::CClrComboBox()
{
}
CClrComboBox::~CClrComboBox()
{
}
BEGIN_MESSAGE_MAP(CClrComboBox, CComboBox)
//{{AFX_MSG_MAP(CClrComboBox)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClrComboBox message handlers
void CClrComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// 由于组合框具有 CBS_OWNERDRAWFIXED 样式,因此以 0 为参数调用成员函数
// GetItemHeight 获得每一项的固定高度
lpMeasureItemStruct->itemHeight=GetItemHeight(0);
}
void CClrComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
COLORREF cr=(COLORREF)lpDrawItemStruct->itemData;
// 注意到在出错的情况下,GetCurSel 和 GetItemData 返回 CB_ERR,而常量
// CB_ERR 被定义为 -1,这时不应把它视为一种系统颜色。
if (cr==CB_ERR)
cr=GetSysColor(COLOR_WINDOW);
if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
{
// 需要重绘整个项
// 以该项所对应的颜色填充整个项
CBrush br(cr);
pDC->FillRect(&lpDrawItemStruct->rcItem, &br);
// 反色居中显示该颜色的 RGB 组成
CString str;
str.Format(L"R: %d G: %d B: %d", GetRValue(cr), GetGValue(cr), GetBValue(cr));
CSize size;
size=pDC->GetTextExtent(str);
CRect rect=lpDrawItemStruct->rcItem;
COLORREF tcr;
tcr=~cr & 0x00FFFFFF; // 获得背景色的反色,不能简单的使用 ~cr
pDC->SetTextColor(tcr);
pDC->SetBkColor(cr);
pDC->DrawText(str,rect,DT_CENTER|DT_NOCLIP);//,rect.left+(rect.Width()-size.cx)/2, rect.top+(rect.Height()-size.cy)/2,ETO_CLIPPED,rect, str);
}
if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
// 选中状态由未选中变为选中,其边框被加亮显示
COLORREF crHilite=~cr & 0x00FFFFFF;
CBrush br(crHilite);
pDC->FrameRect(&lpDrawItemStruct->rcItem, &br);
}
if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & ODA_SELECT))
{
// 选中状态由选中变为非选中,清除其边框的加亮显示
CBrush br(cr);
pDC->FrameRect(&lpDrawItemStruct->rcItem, &br);
}
}
int CClrComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
// TODO: Add your code to determine the sorting order of the specified items
// return -1 = item 1 sorts before item 2
// return 0 = item 1 and item 2 sort the same
// return 1 = item 1 sorts after item 2
COLORREF cr1 = (COLORREF)lpCompareItemStruct->itemData1;
COLORREF cr2 = (COLORREF)lpCompareItemStruct->itemData2;
if (cr1 == cr2)
{
// 项 1 和项 2 具有相同的颜色
return 0;
}
// 进行亮度比较, 亮度低的排列顺序在前
int intensity1 = GetRValue(cr1) + GetGValue(cr1) + GetBValue(cr1);
int intensity2 = GetRValue(cr2) + GetGValue(cr2) + GetBValue(cr2);
if (intensity1 < intensity2)
return -1;
else if (intensity1 > intensity2)
return 1;
// 如果亮度相同, 按颜色进行排序, (蓝色最前, 红色最后)
if (GetBValue(cr1) > GetBValue(cr2))
return -1;
else if (GetGValue(cr1) > GetGValue(cr2))
return -1;
else if (GetRValue(cr1) > GetRValue(cr2))
return -1;
else
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -