Combobox的简单自绘实现
2016-04-28 14:39
239 查看
#pragma once class CCustomComboBox:public CComboBox{ DECLARE_MESSAGE_MAP(); DECLARE_DYNAMIC(CCustomComboBox); public: CCustomComboBox(); ~CCustomComboBox(); void setItemBkColor(const COLORREF& color); COLORREF getItemBkColor()const; void setItemBkPic(const char* picFilePath); string getItemBkPic()const; void setItemTextColor(const COLORREF& color); COLORREF getItemTextColor()const; void setEditFontColor(const COLORREF& color); COLORREF getEditFontColor()const; void setEditBkColor(const COLORREF& color); COLORREF getEditBkColor()const; void setEditBkPic(const char* picFilePath); string getEditBkPic()const; void setEditOffset(int offsetX,int offsetY); void setItemOffset(int offsetX,int offsetY); void setDropDownImage(const char* dropDownImage); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnPaint(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnLButtonDown(UINT nFlags,CPoint point); afx_msg void OnLButtonUp(UINT nFlags,CPoint point); afx_msg void OnMouseMove(UINT nFlags,CPoint point); afx_msg LRESULT OnMouseLeave(WPARAM wParam,LPARAM lParam); afx_msg LRESULT OnMouseHover(WPARAM wParam,LPARAM lParam); afx_msg void OnCbnSelchange(); protected: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct); virtual int CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct); virtual void DeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct); private: COLORREF itemBkColor; string itemBkPic; COLORREF itemTextColor; COLORREF editBkColor; COLORREF editTextColor; string editBkPic; string dropDownImage; int dropDownBtnState; //0-normal,1-hover,2-pressed,3-disabled bool mouseTracking; int editOffsetX; int editOffsetY; int itemOffsetX; int itemOffsetY; }; #include "CustomComboBox.h" IMPLEMENT_DYNAMIC(CustomComboBox,CComboBox) BEGIN_MESSAGE_MAP(CustomComboBox,CComboBox) ON_WM_CREATE() ON_WM_PAINT() ON_WM_ERASEBKGND() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave) ON_MESSAGE(WM_MOUSEHOVER,OnMouseHover) ON_WM_DRAWITEM_REFLECT() ON_WM_MEASUREITEM_REFLECT() ON_WM_DELETEITEM_REFLECT() ON_WM_COMPAREITEM_REFLECT() ON_CONTROL_REFLECT(CBN_SELCHANGE,OnCbnSelchange) END_MESSAGE_MAP() CCustomComboBox::CCustomComboBox() { itemBkColor = RGB(255,255,255); itemTextColor = RGB(0,0,0); editTextColor = RGB(0,0,0); editBkColor = RGB(255,255,255); dropDownBtnState = 0; mouseTracking = false; itemOffsetX = 0; itemOffsetY = 0; editOffsetX = 0; editOffsetY = 0; } CCustomComboBox::~CCustomComboBox() { } void CCustomComboBox::setItemBkColor(const COLORREF& color) { itemBkColor = color; } COLORREF CCustomComboBox::getItemBkColor()const { return itemBkColor; } void CCustomComboBox::setItemBkPic(const char* picFilePath) { if(picFilePath!=NULL) { itemBkPic = picFilePath; } } string CCustomComboBox::getItemBkPic()const { return itemBkPic; } void CCustomComboBox::setItemTextColor(const COLORREF& color) { itemTextColor = color; } COLORREF CCustomComboBox::getItemTextColor()const { return itemTextColor; } void CCustomComboBox::setEditFontColor(const COLORREF& color) { editTextColor = color; } COLORREF CCustomComboBox::getEditFontColor()const { return editTextColor; } void CCustomComboBox::setEditBkColor(const COLORREF& color) { editBkColor = color; } COLORREF CCustomComboBox::getEditBkColor()const { return editBkColor; } void setEditBkPic(const char* picFilePath) { if(picFilePath!=NULL) { editBkPic = picFilePath; } } string CCustomComboBox::getEditBkPic()const { return editBkPic; } void CCustomComboBox::setEditOffset(int offsetX,int offsetY) { editOffsetX = offsetX; editOffsetY = offsetY; } void CCustomComboBox::setItemOffset(int offsetX,int offsetY) { itemOffsetX = offsetX; itemOffsetY = offsetY; } void CCustomComboBox::setDropDownImage(const char* dropDownImage) { if(dropDownImage!=NULL) { this->dropDownImage = dropDownImage; } } int CCustomComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct) { ModifyStyle(m_hWnd,0,CBS_OWNERDRAWVARIABLES|CBS_HASSTRING,0); return __super::OnCreate(lpCreateStruct); } //绘制编辑区域的内容 void CCustomComboBox::OnPaint() { CPaintDC dc(this); CRect rcClient; GetClientRect(&rcClient); CDC memDC; CBitmap bmp; memDC.CreateCompatibleDC(&dc); bmp.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height()); memDC.SelectObject(&bmp); //绘制背景颜色 CBrush brush; brush.CreateSolidBrush(editBkColor); memDC.FillRect(&rcClient,&brush); //绘制边框memDC.FrameRect(&rcClient,&brush); if(!editBkPic.empty()) { //绘制背景图 } CRect rcText = rcClient; //绘制下拉按钮 if(!dropDownImage.empty()) { //获取图片大小w,h并绘制,每帧图片宽sliceW rcText.right -= sliceW; } //绘制编辑框文字 CString cstrBuffer; GetWindowText(cstrBuffer); rcText.DeflateRect(editOffsetX,editOffsetY); CFont* font = GetFont(); memDC.SelectObject(*font); memDC.SetBkMode(TRANSPARENT); memDC.SetBkColor(editBkColor); memDC.SetTextColor(editTextColor); memDC.DrawText(cstrBuffer,&rcText,DT_SINGLELINE|DT_LEFT|DT_VCENTER); dc.BitBlt(0,0,rcClient.Width(),rcClient.Height(),&memDC,0,0,SRCCOPY); } BOOL CCustomComboBox::OnEraseBkgnd(CDC* pDC) { return TRUE; } void CCustomComboBox::OnLButtonDown(UINT nFlags,CPoint point) { dropDownBtnState = 2; RedrawWindow(); __super::OnLButtonDown(nFlags,point); } void CCustomComboBox::OnLButtonUp(UINT nFlags,CPoint point) { dropDownBtnState = 2; RedrawWindow(); __super::OnLButtonUp(nFlags,point); } void CCustomComboBox::OnMouseMove(UINT nFlags,CPoint point) { if(mouseTracking==false) { TRACKMOUSEEVENT tme; tme.cbSize = sizeof(tme); tme.dwFlags = TME_LEAVE|TME_HOVER; tme.dwHoverTime = 1; tme.hwndTrack = this->m_hWnd; if(::_TrackMouseEvent(&tme)) { mouseTracking = true; } } __super::OnMouseMove(nFlags,point); } LRESULT CCustomComboBox::OnMouseLeave(WPARAM wParam,LPARAM lParam) { dropDownBtnState = 0; mouseTracking = false; RedrawWindow(); return 0; } LRESULT CCustomComboBox::OnMouseHover(WPARAM wParam,LPARAM lParam) { dropDownBtnState = 1; RedrawWindow(); return 0; } void CCustomComboBox::OnCbnSelchange() { CString cstrText; GetLBText(GetCurSel(),cstrText); SetWindowText(cstrText); } //绘制列表项 void CCustomComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { if(lpDrawItemStruct->CtlType==ODT_COMBOBOX) { CDC dc; dc.Attach(lpDrawItemStruct->hDC); CRect rcClient = lpDrawItemStruct->rcItem; UINT state = lpDrawItemStruct->itemState; CBrush br; if(state&ODS_SELECTED) { br.CreateSolidBrush(RGB(51,153,255)); } else { br.CreateSolidBrush(itemBkColor); } dc.FillRect(&rcClient,&br); if(!itemBkPic.empty()) { //绘制列表项的背景图 } dc.SetBkMode(TRANSPARENT); dc.SetBkColor(itemBkColor); dc.SetTextColor(itemTextColor); //如果不是空项 if(lpDrawItemStruct->itemID!=(UINT)-1) { UINT id = lpDrawItemStruct->itemID; CString cstrBuffer; GetLBText(id,cstrBuffer); rcClient.DeflateRect(itemOffsetX,itemOffsetY); dc.DrawText(cstrBuffer,*rcClient,DT_SINGLELINE|DT_VCENTER|DT_LEFT); } dc.Detack(); //再重绘一遍编辑框 RedrawWindow(); } } void CCustomComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { if((lpMeasureItemStruct->CtlType==ODT_COMBOBOX)&& (lpMeasureItemStruct->itemID!=(UINT)-1)) { CString cstrBuffer; GetLBText(lpMeasureItemStruct->itemID,cstrBuffer); CSize sz; CDC* pDC = GetDC(); sz = pDC->GetTextExtent(cstrBuffer); ReleaseDC(pDC); lpMeasureItemStruct->itemHeight = sz.cy*3/2; } } int CCustomComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) { int iCmp = 0; if(lpCompareItemStruct->CtlType==ODT_COMBOBOX) { CString cstrText1,cstrText2; GetLBText(lpCompareItemStruct->itemID1,cstrText1); GetLBText(lpCompareItemStruct->itemID2,cstrText2); if((!cstrText1.IsEmpty())&&(!cstrText2,IsEmpty())) { iCmp = strcmp((const char*)cstrText1,(const char*)cstrText2); } } return iCmp; } void CCustomComboBox::DeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct) { }
相关文章推荐
- 静态库的打包及使用(干货)
- Metasploit是一款开源的安全漏洞检测工具,
- Java进程和线程的概述
- UML六种关系
- SYMFONY 命令
- sublime text 3 插件 cssrem
- Android中Preference的使用以及监听事件分析
- Git:代码冲突常见解决方法
- [opencv]6.图片的处理:侵蚀、膨胀、反转
- docker 搭建php开发环境
- java 连接access数据库两种方式
- DNS域名解析过程
- poj-2826 An Easy Problem?!(计算几何,好题)
- 求一个字符串的hash值
- <a href="javascript:;">中href中的javascript:;的作用
- getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()路径
- MyBatis学习总结(15)——定制Mybatis自动代码生成的maven插件
- MyBatis学习总结(15)——定制Mybatis自动代码生成的maven插件
- [从头学声学] 第200节 [机器小伟]的200岁生日
- MyBatis学习总结(15)——定制Mybatis自动代码生成的maven插件