Created
February 15, 2011 05:04
-
-
Save thinkhy/827115 to your computer and use it in GitHub Desktop.
ListCtrl Custom Draw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
// CCustomListBox | |
class CCustomListBox : public CListBox | |
{ | |
DECLARE_DYNAMIC(CCustomListBox) | |
////////////////////////////////////////////自绘ListBox | |
CArray<int> m_arySeparators; | |
int m_nHorizontalMargin; | |
int m_nBottomMargin; | |
int m_nSepWidth; | |
int m_nPenStyle; | |
COLORREF m_crColor; | |
CBrush m_brRedBrush; | |
public: | |
CCustomListBox(); | |
virtual ~CCustomListBox(); | |
//////////////////////////////////////////////////////////////////////////ListItem显示 | |
virtual void PreSubclassWindow(void); | |
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); | |
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct); | |
//////////////////////////////////////////////////////////////////////////ListItem Separator | |
void SetSeparator(int iSep); | |
void AdjustItemHeight(int nInc=3); | |
void SetSepLineStyle(int iSep) { m_nPenStyle = iSep; } | |
void SetSepLineColor(COLORREF crColor) { m_crColor = crColor; } | |
void SetSepLineWidth(int iWidth) { m_nSepWidth = iWidth; } | |
void SetBottomMargin(int iMargin) { m_nBottomMargin = iMargin; } | |
void SetHorizontalMargin(int iMargin) { m_nHorizontalMargin = iMargin; } | |
HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); | |
void AddItemIcons(UINT nIDResBitmap); | |
protected: | |
DECLARE_MESSAGE_MAP() | |
public: | |
afx_msg void OnDestroy(); | |
private: | |
CImageList m_imgList; | |
}; | |
////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////// | |
// CustomListBox.cpp : 实现文件 | |
// | |
#include "stdafx.h" | |
#include "PackageTool.h" | |
#include "CustomListBox.h" | |
#include "PackageDefs.h" | |
// CCustomListBox | |
IMPLEMENT_DYNAMIC(CCustomListBox, CListBox) | |
CCustomListBox::CCustomListBox(): | |
m_nPenStyle(PS_DOT), | |
m_crColor(RGB(64,64,64)), | |
m_nBottomMargin(1), | |
m_nSepWidth(1), | |
m_nHorizontalMargin(1) | |
{ | |
} | |
CCustomListBox::~CCustomListBox() | |
{ | |
} | |
BEGIN_MESSAGE_MAP(CCustomListBox, CListBox) | |
ON_WM_DESTROY() | |
END_MESSAGE_MAP() | |
// CCustomListBox 消息处理程序 | |
void CCustomListBox::PreSubclassWindow(void) | |
{ | |
CRect rect; | |
GetClientRect(rect); | |
this->SetItemHeight(-1,rect.Height()/10); | |
CListBox::PreSubclassWindow(); | |
} | |
void CCustomListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)////////////////////////////自绘ListBox | |
{ | |
CDC* pDC = CDC::FromHandle(lpDIS->hDC); | |
COLORREF clrItemText; | |
switch ( lpDIS->itemAction ) //lpDIS为一个LPMEASUREITEMSTRUCT(记录控件信息)判断listbox控件状态 | |
{ | |
case ODA_SELECT: | |
case ODA_DRAWENTIRE: | |
case ODA_FOCUS: | |
// Is the item selected? | |
if ( lpDIS->itemState & ODS_SELECTED ) | |
{ HBRUSH hbrush = CreateSolidBrush(RGB(0,190,255));//创建一个画刷 | |
clrItemText = GetSysColor(COLOR_WINDOWTEXT/*COLOR_HIGHLIGHTTEXT*/); | |
// Clear the rectangle | |
FillRect( lpDIS->hDC,&lpDIS->rcItem,hbrush); | |
SetTextColor(lpDIS->hDC,clrItemText); | |
DeleteObject(hbrush); | |
} | |
else | |
{ | |
clrItemText = GetSysColor( COLOR_WINDOWTEXT ); | |
// Clear the rectangle | |
FillRect( lpDIS->hDC,&lpDIS->rcItem,(HBRUSH)(PACKAGETOOL_BK_COLOR) ); | |
SetTextColor(lpDIS->hDC,clrItemText); | |
} | |
} | |
CRect rectFull = lpDIS->rcItem; | |
CRect rect = rectFull; | |
rect.left= rect.Width(); | |
CRect rect2 = rectFull; | |
rect2.right = rect.left - 1; | |
UINT nIndex = lpDIS->itemID; | |
if (nIndex != (UINT) -1) | |
{ | |
LOGFONT lf; | |
pDC->GetCurrentFont()->GetLogFont(&lf); | |
CFont font, *pOldFont; | |
lf.lfCharSet=GB2312_CHARSET; | |
/* lf.lfWeight=FW_BOLD;*/ | |
lf.lfHeight=12; | |
lf.lfWidth=0; | |
lstrcpy(lf.lfFaceName,_T("宋体")); | |
font.CreateFontIndirect(&lf); | |
pOldFont=pDC->SelectObject(&font); | |
CString Text; | |
GetText(nIndex,Text); | |
pDC->SetBkMode(TRANSPARENT); | |
pDC->DrawText(Text,CRect(rect2.left,rect2.top,rect2.right,rect2.bottom),DT_CENTER | DT_SINGLELINE|DT_VCENTER); | |
pDC->SelectObject(pOldFont); | |
m_imgList.Draw(pDC, lpDIS->itemID, CPoint(lpDIS->rcItem.left+2, lpDIS->rcItem.top + 7), ILD_TRANSPARENT); | |
} | |
OnCtlColor(pDC,this,CTLCOLOR_LISTBOX); | |
} | |
void CCustomListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) | |
{ | |
// Changes the Width and the Height of item in the list box | |
lpMeasureItemStruct->itemHeight = 40; | |
lpMeasureItemStruct->itemWidth = 100; | |
} | |
HBRUSH CCustomListBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) | |
{ | |
if (nCtlColor == CTLCOLOR_LISTBOX) | |
{ | |
if (this->GetSafeHwnd() ==NULL) | |
{ | |
this->SubclassWindow(pWnd->GetSafeHwnd()); | |
} | |
CRect r; | |
int nIndex, n = this->GetCount(); | |
CPen pen(m_nPenStyle, m_nSepWidth, m_crColor), *pOldPen; | |
pOldPen = pDC->SelectObject(&pen); | |
for (int i=0; i< m_arySeparators.GetSize(); i++) | |
{ | |
nIndex = m_arySeparators[i]; | |
if (nIndex<0) nIndex += n-1; | |
if (nIndex < n-1) | |
{ | |
this->GetItemRect(nIndex,&r); | |
pDC->MoveTo(r.left+m_nHorizontalMargin, r.bottom-m_nBottomMargin); | |
pDC->LineTo(r.right-m_nHorizontalMargin, r.bottom-m_nBottomMargin); | |
} | |
} | |
pDC->SelectObject(pOldPen); | |
} | |
return (HBRUSH)::GetStockObject(NULL_BRUSH); | |
} | |
void CCustomListBox::OnDestroy() | |
{ | |
if (this->GetSafeHwnd() !=NULL) | |
this->UnsubclassWindow(); | |
CListBox::OnDestroy(); | |
} | |
void CCustomListBox::SetSeparator(int iSep) | |
{ | |
if (!m_arySeparators.GetSize()) | |
AdjustItemHeight(); | |
m_arySeparators.Add(iSep); | |
} | |
void CCustomListBox::AdjustItemHeight(int nInc/*=3*/) | |
{ | |
SetItemHeight(0, nInc+ GetItemHeight(0)); | |
} | |
void CCustomListBox::AddItemIcons(UINT nIDResBitmap) | |
{ | |
if (m_imgList.GetSafeHandle() != NULL) | |
m_imgList.DeleteImageList(); | |
CBitmap bmpImage; | |
VERIFY(bmpImage.LoadBitmap(nIDResBitmap)); | |
BITMAP bmpInfo; | |
bmpImage.GetBitmap(&bmpInfo); | |
m_imgList.Create(bmpInfo.bmWidth/10, bmpInfo.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 1); | |
m_imgList.Add(&bmpImage, RGB(255, 255, 255)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment