【VC开源代码栏目提醒】:网学会员VC开源代码为您提供ColorListBox.cpp参考,解决您在ColorListBox.cpp学习中工作中的难题,参考学习。
// ColorListBox.cpp : implementation file
//
#include "stdafx.h"
#include "ColorListBoxExa.h"
#include "ColorListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorListBox
CColorListBox::CColorListBox()
{
}
CColorListBox::~CColorListBox()
{
}
BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
//{{AFX_MSG_MAP(CColorListBox)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorListBox message handlers
int CColorListBox::AddItem(CString str)
{
int nIndex = CListBox::AddString (str);
return nIndex;
}
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC* pDC = CDC::FromHandle (lpDrawItemStruct->hDC);
int nIndex = lpDrawItemStruct->itemID;
CRect rc = lpDrawItemStruct->rcItem;
int nState = lpDrawItemStruct->itemState;
CString strText;
GetText(nIndex,strText);
//
// Draw Back Rectangle...
//
COLORREF colBack;
if (nState & ODS_SELECTED )
colBack = RGB(0,0,255);
else
colBack = RGB(255,255,255);
CBrush brush(colBack);
CPen pen(PS_SOLID,1,colBack);
CBrush* pBrush = pDC->SelectObject (&brush);
CPen* pPen = pDC->SelectObject (&pen);
pDC->Rectangle (rc);
pDC->SelectObject (pPen);
pDC->SelectObject (pBrush);
//
// End Back Rectangle...
//
//
// Draw Color Rectangle...
//
CRect rrc = rc;
rrc.left += 1;
rrc.top += 1;
rrc.right = rrc.left + 16;
rrc.bottom = rrc.top + 16;
CBitmap bmp;
HBITMAP hBitmap= (HBITMAP) :: LoadImage(AfxGetInstanceHandle(),strText+".bmp",0,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
bmp.Attach(hBitmap);
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
// Create an in-memory DC compatible with the
// display DC we're using to paint
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
// Find a centerpoint for the bitmap in the client area
int nX = rrc.left + (rrc.Width() - bmpInfo.bmWidth) / 2;
int nY = rrc.top + (rrc.Height() - bmpInfo.bmHeight) / 2;
// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
//
// End Color Rectangle...
//
//
// Print String...
//
COLORREF colOld = pDC->SetBkColor (colBack);
pDC->TextOut (rrc.right + 2,rrc.top,strText);
pDC->SetBkColor (colOld);
//
// If Focus, Draw Focus rectangle...
//
if (lpDrawItemStruct->itemAction & ODA_FOCUS && lpDrawItemStruct->itemState & ODS_FOCUS )
pDC->DrawFocusRect (rc);
}
上一篇:
ColorLabel.cpp
下一篇:
爱情只不过就是,爱与不爱