LineComboBox.cpp
资源名称:44757463.rar [点击查看]
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:5k
源码类别:
绘图程序
开发平台:
Visual C++
- //////////////////////////////////////////////////////////////////////
- // CLASS : CLineComboBox
- // ABSTRACT : 线条宽度选择组合框
- // NOTE : 主要是用在图形的属性设置时的笔宽设置
- // CREATE : FNST)handwolf 2004-4-21
- // UPDATE :
- // :
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "LineComboBox.h"
- //################################################################
- CLineComboBox::CLineComboBox()
- {
- //当前鼠标是否在对象上
- m_nMouseState = 0;
- m_nLineLength = 45;
- m_nItemHeight = 20;
- m_nMargin=2;
- }
- //################################################################
- CLineComboBox::~CLineComboBox()
- {
- while(!m_LineWidthList.IsEmpty()){
- delete m_LineWidthList.RemoveHead();
- }
- }
- //################################################################
- BOOL CLineComboBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
- {
- DWORD dw = dwStyle;
- //dw |= CBS_OWNERDRAWVARIABLE;
- if( !CComboBox::Create(dw, rect, pParentWnd, nID) )
- return false;
- CFont * font = CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT));
- SetFont(font);
- return true;
- }
- //################################################################
- void CLineComboBox::Init()
- {
- for(int i = 0; i <= 6; i++)
- {
- LPBSCBITEM lpItem = new BSCBITEM;
- CString strText;
- strText.Format(" %d",i+1);
- lpItem->lpCaption=strText;
- lpItem->iIndex = AddString(strText);
- lpItem->nLineWidth = i+1;
- if(m_LineWidthList.IsEmpty())
- m_LineWidthList.AddHead(lpItem);
- else
- m_LineWidthList.AddTail(lpItem);
- }
- }
- //################################################################
- IMPLEMENT_DYNCREATE(CLineComboBox, CComboBox)
- BEGIN_MESSAGE_MAP(CLineComboBox, CComboBox)
- //{{AFX_MSG_MAP(CLineComboBox)
- ON_WM_ERASEBKGND()
- ON_WM_PAINT()
- ON_WM_MOUSEMOVE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- //################################################################
- BOOL CLineComboBox::OnEraseBkgnd(CDC* pDC)
- {
- ASSERT(pDC->GetSafeHdc());
- return false;
- }
- //################################################################
- void CLineComboBox::OnPaint()
- {
- CPaintDC dc(this);
- OnCBPaint(&dc);
- }
- //################################################################
- void CLineComboBox::OnCBPaint(CDC* pDC)
- {
- ASSERT(pDC->GetSafeHdc());
- //绘制客户区
- CDC dMemDC;
- dMemDC.CreateCompatibleDC(pDC);
- dMemDC.SetMapMode(pDC->GetMapMode());
- //画动作
- CBitmap mNewBmp;
- RECT rc;
- GetClientRect(&rc);
- mNewBmp.CreateCompatibleBitmap(pDC, rc.right - rc.left, rc.bottom - rc.top);
- CBitmap* pOldBmp = dMemDC.SelectObject(&mNewBmp);
- //子类可以以friend方式来访问父类的protected成员变量和函数
- CWnd::DefWindowProc(WM_PAINT, (WPARAM)dMemDC.m_hDC, 0);
- pDC->BitBlt(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, &dMemDC,
- rc.left ,rc.top, SRCCOPY);
- //恢复
- dMemDC.SelectObject(pOldBmp);
- pOldBmp->DeleteObject();
- dMemDC.DeleteDC();
- GetWindowRect(&rc);
- ScreenToClient(&rc);
- pDC->DrawEdge(&rc, (m_nMouseState ? BDR_RAISEDINNER : BDR_SUNKENINNER), BF_RECT);
- }
- //################################################################
- void CLineComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
- {
- lpMeasureItemStruct->itemHeight = m_nItemHeight;
- }
- //################################################################
- void CLineComboBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- ASSERT(lpDIS->CtlType == ODT_COMBOBOX);
- //画笔
- CDC* pDC = CDC::FromHandle(lpDIS->hDC);
- ASSERT(pDC->GetSafeHdc());
- //绘制区
- RECT rc = lpDIS->rcItem;
- CRect rcLine(rc), rcTxt(rc);
- //当前的Item索引号
- LPBSCBITEM lpItem = GetItem(lpDIS->itemID);
- if(lpItem != NULL){
- BOOL bSelected =lpDIS->itemState & ODS_SELECTED;
- pDC->FillSolidRect(&rcLine,RGB(255,255,255));//察除以前的画面
- //如果选中
- if(bSelected){
- pDC->FillSolidRect(&rcLine,RGB(0,255,255));
- }
- //画Line
- CPen pen,*pOldPen;
- pen.CreatePen(PS_SOLID,lpItem->nLineWidth,RGB(0,0,255));
- pOldPen=pDC->SelectObject(&pen);
- rcLine.right=rcLine.left+m_nLineLength;
- rcLine.DeflateRect(m_nMargin,m_nMargin);
- pDC->MoveTo(rcLine.left,(rcLine.top+rcLine.bottom)/2);
- pDC->LineTo(rcLine.right,(rcLine.top+rcLine.bottom)/2);
- pDC->SelectObject(pOldPen);
- // pDC->DrawEdge(&rcLine, BDR_RAISEDINNER, BF_RECT);
- //开始画文字
- int nOldBkMode = pDC->SetBkMode(TRANSPARENT);
- pDC->SetTextColor(RGB(0, 0, 0));
- rcTxt.left = rcLine.right + m_nMargin;
- // rcTxt.top = rcLine.top;
- pDC->DrawText(lpItem->lpCaption,&rcTxt,DT_VCENTER|DT_END_ELLIPSIS|DT_NOCLIP|DT_SINGLELINE);
- pDC->SetBkMode(nOldBkMode);
- }
- }
- //################################################################
- LPBSCBITEM CLineComboBox::GetItem(int iIndex)
- {
- //当前的Item索引号
- POSITION pos = m_LineWidthList.FindIndex(iIndex);
- if(pos)
- {
- LPBSCBITEM lpItem = m_LineWidthList.GetAt(pos);
- ASSERT(lpItem);
- return lpItem;
- }
- else
- return (LPBSCBITEM)NULL;
- }
- //################################################################
- void CLineComboBox::OnMouseMove(UINT nFlags, CPoint point)
- {
- CComboBox::OnMouseMove(nFlags, point);
- }
- int CLineComboBox::GetCurLineWidth()
- {
- int nSel=GetCurSel();
- POSITION pos = m_LineWidthList.FindIndex(nSel);
- if(pos)
- {
- LPBSCBITEM lpItem = m_LineWidthList.GetAt(pos);
- ASSERT(lpItem);
- return lpItem->nLineWidth;
- }
- else
- return 0;
- }