MFC中有许多控件,将控件拖到窗口中右键鼠标,点击属性,到样式标签页中会有很多样式可以选择
但是往往这些样式会满足不了我们的需求,这时候,我们需要自己来完成我们想要的样式控件
我们就来拿ListBox为例子,因为我发现ListBox的样式扩展的需求量是最大的
(1)在Dialog中拖入ListBox控件
(2)在类视图添加一个该控件的子类
(3)然后按Ctrl+W在类向导中给你的控件加一个CColorListBox的变量
(4)可以开始为你的CColorListBox添加样式了
- int CColorListBox::AddMsg(CString lpszItem, DWORD info)
- {
- int nItem = AddString(lpszItem);
- if (nItem >= 0)
- SetItemData(nItem,info);
- int numList = GetCount()-1;
- SetTopIndex(numList);
- SetCurSel(numList);
- //水平滚动
- int max_width=0;
- CSize sz;
- CClientDC dc(this);
- for(int i=0;i<GetCount();i++)
- {
- GetText(i,lpszItem);
- sz=dc.GetTextExtent(lpszItem);
- if(max_width<sz.cx)
- max_width=sz.cx;
- }
- SendMessage(LB_SETHORIZONTALEXTENT,max_width,0);
-
- return nItem;
- }
- void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- // TODO: Add your code to draw the specified item
- if ((int)lpDIS->itemID < 0)
- return;
-
- CDC *pDC = CDC::FromHandle(lpDIS->hDC);
-
- COLORREF crText;
- CString sText;
- COLORREF crNorm;
- HICON icon;
- switch (lpDIS->itemData)
- {
- case M_OK:
- crNorm = #00ff00;
- icon = AfxGetApp()->LoadIcon(IDI_ICON_OK);
- break;
- case M_ERROR:
- crNorm=#ff0000;
- icon = AfxGetApp()->LoadIcon(IDI_ICON_ERROR);
- break;
- case M_WARING:
- crNorm=#0000ff;
- icon = AfxGetApp()->LoadIcon(IDI_ICON_WARING);
- break;
- case M_USER:
- crNorm=#00ff00;
- icon = AfxGetApp()->LoadIcon(IDI_ICON_USER);
- break;
- case M_USER_:
- crNorm=#000000;
- icon = AfxGetApp()->LoadIcon(IDI_ICON_USER_);
- break;
- }
-
-
- // 字符串颜色
- COLORREF crHilite = RGB(255-GetRValue(crNorm), 255-GetGValue(crNorm), 255-GetBValue(crNorm));
- CRect rectBK(lpDIS->rcItem.left+22,lpDIS->rcItem.top,lpDIS->rcItem.right+18,lpDIS->rcItem.bottom);
-
-
- // 如果被选中,使用高亮色会矩形
- if ((lpDIS->itemState & ODS_SELECTED) &&
- (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
- {
- CBrush brush(crNorm);
- pDC->FillRect(rectBK, &brush);
- }
-
- // 如果取消选中,恢复
- if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
- {
- CBrush brush(::GetSysColor(COLOR_WINDOW));
- pDC->FillRect(rectBK, &brush);
- }
-
- // 绘制焦点矩形
- if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
- pDC->DrawFocusRect(&lpDIS->rcItem);
-
- // 取消焦点矩形
- if ((lpDIS->itemAction & ODA_FOCUS) && !(lpDIS->itemState & ODS_FOCUS))
- pDC->DrawFocusRect(&lpDIS->rcItem);
-
-
- // 透明背景
- int nBkMode = pDC->SetBkMode(TRANSPARENT);
-
-
- if (lpDIS->itemData)
- {
- if (lpDIS->itemState & ODS_SELECTED)
- crText = pDC->SetTextColor(crHilite);
- else if (lpDIS->itemState & ODS_DISABLED)
- crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
- else
- crText = pDC->SetTextColor(crNorm);
- }
-
- else
- {
- if (lpDIS->itemState & ODS_SELECTED)
- crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
- else if (lpDIS->itemState & ODS_DISABLED)
- crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
- else
- crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
- }
-
-
- // 显示字符串
- GetText(lpDIS->itemID, sText);
- CRect rect = lpDIS->rcItem;
-
- // 字符格式
- UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
- if (GetStyle() & LBS_USETABSTOPS)
- nFormat |= DT_EXPANDTABS;
-
-
- pDC->DrawText(sText, -1, &rectBK, nFormat | DT_CALCRECT&DT_CENTER);
- pDC->DrawText(sText, -1, &rectBK, nFormat);
- DrawIconEx(lpDIS->hDC,rect.left+2,rect.top+1,icon,16,16,0,NULL, DI_NORMAL | DI_COMPAT);
-
- pDC->SetTextColor(crText);
- pDC->SetBkMode(nBkMode);
- }
关于改变成什么样子的,就要好好运用windows编程里面的知识了
(5)最重要的一点:右键控件点开属性中的样式(Styles)将所有者下拉框改成可变的(variable)把有字符串(has strings)勾上最终的效果是: