看到很多軟件的設(shè)置對(duì)話框都是由一個(gè)自繪的ListBox和幾個(gè)Child風(fēng)格的CDialog組成的,如圖飛秋的設(shè)置對(duì)話框:

自繪的ListBox可以繪制一些自己需要的圖標(biāo)或者文字的字體、顏色和大小。琢磨了一下,做法如下:
從CListBox派生自己的類CListBoxEx
(1)設(shè)置Item的大小在MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)函數(shù)
MeasureItem是個(gè)虛函數(shù),在這里覆寫它,設(shè)置Item大小
void CListBoxEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: 添加您的代碼以確定指定項(xiàng)的大小
lpMeasureItemStruct->itemHeight = m_nHeight; //設(shè)置Item高度
}
(2)在虛函數(shù)DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)內(nèi)繪制我們想要的效果
添加圖標(biāo),繪制想要的文字效果。圖標(biāo)可以是Bitmap或者Icon
Bitmap使用兼容DC拷貝上去;ICON直接利用CDC的DrawIcon畫上去
主要繪制沒(méi)有選中時(shí)的效果和選中的效果
void CListBoxEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代碼以繪制指定項(xiàng)
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rectItem(lpDrawItemStruct->rcItem);
CRect rectIcon(rectItem.left, rectItem.top, rectItem.left+m_nWidth, rectItem.top+m_nHeight);
CRect rectText(rectIcon.right,rectItem.top,rectItem.right,rectItem.bottom);
CListItem* pItem = static_cast<CListItem*>(GetItemDataPtr(lpDrawItemStruct->itemID));
pDC->SetBkMode(TRANSPARENT);
UINT action, state;
action = lpDrawItemStruct->itemAction;
state = lpDrawItemStruct->itemState;
// 繪制整個(gè)Item或者Item沒(méi)有被選中將要選中,填充背景
if ((action & ODA_DRAWENTIRE) || ( !(state & ODS_SELECTED) && (action & ODA_SELECT)))
{
pDC->FillSolidRect(rectItem, m_clrBack); // 填充背景
if (m_bIsEdge)
{
pDC->DrawEdge(rectItem, EDGE_SUNKEN, BF_BOTTOM); // 畫邊框
}
if (lpDrawItemStruct->itemData != NULL)
{
// 畫圖標(biāo)
CRect rect(rectIcon);
rect.DeflateRect(0,5,0,0);
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.LoadBitmap(pItem->m_nBitMap);
CBitmap* pOldBmp = dcMem.SelectObject(&bmp);
pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldBmp);
bmp.DeleteObject();
// 文字
//pDC->TextOut(rectText.left+2, rectText.top,pItem->m_szItemName);
rect = rectText;
rect.DeflateRect(0,5,0,0);
pDC->SetTextColor(RGB(0,0,0));
rect.OffsetRect(2,0);
if (pItem->m_szItemName != NULL)
{
pDC->DrawText(pItem->m_szItemName, lstrlen(pItem->m_szItemName),rect, DT_LEFT | DT_SINGLELINE);
}
}
}
// Item被選中
if ( (state & ODS_SELECTED) && (action & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CRect rect(rectItem);
// 背景
CPen Pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&Pen);
pDC->Rectangle(rect);
pDC->SelectObject(pOldPen);
rect.DeflateRect(0,1,0,0);
pDC->FillRect(rect, &CBrush(m_clrSel));
// 邊框
if (m_bIsEdge)
{
pDC->DrawEdge(rect, EDGE_SUNKEN, BF_BOTTOM);
}
// 圖標(biāo)
rect = rectIcon;
rect.DeflateRect(0,5,0,0);
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.LoadBitmap(pItem->m_nBitMap);
CBitmap* pOldBmp = dcMem.SelectObject(&bmp);
pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldBmp);
bmp.DeleteObject();
// 文字
rect.CopyRect(rectText);
rect.DeflateRect(0,5,0,0);
pDC->SetTextColor(m_clrText); // 被選中時(shí)的顏色
rect.OffsetRect(10,0);
if (pItem->m_szItemName != NULL)
{
pDC->DrawText(pItem->m_szItemName, lstrlen(pItem->m_szItemName),rect, DT_LEFT | DT_SINGLELINE);
}
}
}
(3)其他
可以添加一些利用鍵盤上下鍵選擇Item的功能,覆寫函數(shù)DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
int CListBoxEx::VKeyToItem(UINT nKey, UINT nIndex)
{
// TODO: 添加處理特定虛擬鍵的代碼
// 返回 -1 = 默認(rèn)操作
// 返回 -2 = 沒(méi)有進(jìn)一步的操作
// 返回索引 = 執(zhí)行以下項(xiàng)上的鍵擊的默認(rèn)操作
// 索引指定的項(xiàng)
if ((nKey == VK_UP) && (nIndex > 0))
SetCurSel(nIndex);
else if ((nKey == VK_DOWN) && (nIndex < (UINT)GetCount()))
SetCurSel(nIndex);
return -1;
}
因?yàn)橐邮真I盤上下鍵消息,所以在listbox屬性中,將Want Key Input改為True
(4)碰到的問(wèn)題
代碼添加完畢,利用自己派生的類CListBoxEx關(guān)聯(lián)一個(gè)ListBox控件,運(yùn)行卻沒(méi)有自己要的自繪效果,跟蹤代碼發(fā)現(xiàn),DrawItem根本沒(méi)有被調(diào)用,怎么回事,哦,原來(lái)是忘記設(shè)置控件的屬性,標(biāo)示其是否自繪。
查看Listbox Control屬性→行為→Owner Draw,將No改為variable。
一般也不需要自動(dòng)排序,將Sort改為False。
(5)運(yùn)行結(jié)果

詳細(xì)可看這里