LineComboBox.cpp
上传用户:lj3531212
上传日期:2007-06-18
资源大小:346k
文件大小:5k
源码类别:

绘图程序

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////////////
  2. // CLASS    :  CLineComboBox
  3. // ABSTRACT :  线条宽度选择组合框
  4. // NOTE     :  主要是用在图形的属性设置时的笔宽设置
  5. // CREATE   :  FNST)handwolf  2004-4-21
  6. // UPDATE   :  
  7. //          : 
  8. //////////////////////////////////////////////////////////////////////
  9. #include "stdafx.h"
  10. #include "LineComboBox.h"
  11. //################################################################
  12. CLineComboBox::CLineComboBox()
  13. {
  14. //当前鼠标是否在对象上
  15. m_nMouseState = 0;
  16. m_nLineLength = 45;
  17. m_nItemHeight = 20;
  18. m_nMargin=2;
  19. }
  20. //################################################################
  21. CLineComboBox::~CLineComboBox()
  22. {
  23. while(!m_LineWidthList.IsEmpty()){
  24. delete m_LineWidthList.RemoveHead();
  25. }
  26. }
  27. //################################################################
  28. BOOL CLineComboBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  29. {
  30. DWORD dw = dwStyle;
  31. //dw |= CBS_OWNERDRAWVARIABLE;
  32. if( !CComboBox::Create(dw, rect, pParentWnd, nID) )
  33. return false;
  34. CFont * font = CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT));
  35. SetFont(font);
  36. return true;
  37. }
  38. //################################################################
  39. void CLineComboBox::Init()
  40. {
  41. for(int i = 0; i <= 6; i++)
  42. {
  43. LPBSCBITEM lpItem = new BSCBITEM;
  44. CString strText;
  45. strText.Format("   %d",i+1);
  46. lpItem->lpCaption=strText;
  47. lpItem->iIndex = AddString(strText);
  48. lpItem->nLineWidth = i+1;
  49. if(m_LineWidthList.IsEmpty())
  50. m_LineWidthList.AddHead(lpItem);
  51. else
  52. m_LineWidthList.AddTail(lpItem);
  53. }
  54. }
  55. //################################################################
  56. IMPLEMENT_DYNCREATE(CLineComboBox, CComboBox)
  57. BEGIN_MESSAGE_MAP(CLineComboBox, CComboBox)
  58. //{{AFX_MSG_MAP(CLineComboBox)
  59. ON_WM_ERASEBKGND()
  60. ON_WM_PAINT()
  61. ON_WM_MOUSEMOVE()
  62. //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64. //################################################################
  65. BOOL CLineComboBox::OnEraseBkgnd(CDC* pDC) 
  66. {
  67. ASSERT(pDC->GetSafeHdc());
  68. return false;
  69. }
  70. //################################################################
  71. void CLineComboBox::OnPaint() 
  72. {
  73. CPaintDC dc(this); 
  74. OnCBPaint(&dc);
  75. }
  76. //################################################################
  77. void CLineComboBox::OnCBPaint(CDC* pDC)
  78. {
  79. ASSERT(pDC->GetSafeHdc());
  80. //绘制客户区
  81. CDC dMemDC;
  82. dMemDC.CreateCompatibleDC(pDC);
  83. dMemDC.SetMapMode(pDC->GetMapMode());
  84. //画动作
  85. CBitmap mNewBmp;
  86. RECT rc;
  87. GetClientRect(&rc);
  88. mNewBmp.CreateCompatibleBitmap(pDC, rc.right - rc.left, rc.bottom - rc.top);
  89. CBitmap* pOldBmp = dMemDC.SelectObject(&mNewBmp);
  90. //子类可以以friend方式来访问父类的protected成员变量和函数
  91. CWnd::DefWindowProc(WM_PAINT, (WPARAM)dMemDC.m_hDC, 0);
  92. pDC->BitBlt(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, &dMemDC,
  93. rc.left ,rc.top, SRCCOPY);
  94. //恢复
  95. dMemDC.SelectObject(pOldBmp);
  96. pOldBmp->DeleteObject();
  97. dMemDC.DeleteDC();
  98. GetWindowRect(&rc);
  99. ScreenToClient(&rc);
  100. pDC->DrawEdge(&rc, (m_nMouseState ? BDR_RAISEDINNER : BDR_SUNKENINNER), BF_RECT);
  101. }
  102. //################################################################
  103. void CLineComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
  104. {
  105. lpMeasureItemStruct->itemHeight = m_nItemHeight;
  106. }
  107. //################################################################
  108. void CLineComboBox::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
  109. {
  110. ASSERT(lpDIS->CtlType == ODT_COMBOBOX);
  111.     
  112. //画笔
  113. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  114. ASSERT(pDC->GetSafeHdc());
  115. //绘制区
  116. RECT rc = lpDIS->rcItem;
  117. CRect rcLine(rc), rcTxt(rc);
  118. //当前的Item索引号
  119. LPBSCBITEM lpItem = GetItem(lpDIS->itemID);
  120. if(lpItem != NULL){
  121. BOOL bSelected =lpDIS->itemState & ODS_SELECTED;
  122. pDC->FillSolidRect(&rcLine,RGB(255,255,255));//察除以前的画面
  123. //如果选中
  124. if(bSelected){
  125. pDC->FillSolidRect(&rcLine,RGB(0,255,255));
  126. }
  127. //画Line
  128. CPen pen,*pOldPen;
  129. pen.CreatePen(PS_SOLID,lpItem->nLineWidth,RGB(0,0,255));
  130. pOldPen=pDC->SelectObject(&pen);       
  131. rcLine.right=rcLine.left+m_nLineLength;
  132. rcLine.DeflateRect(m_nMargin,m_nMargin);
  133. pDC->MoveTo(rcLine.left,(rcLine.top+rcLine.bottom)/2);
  134. pDC->LineTo(rcLine.right,(rcLine.top+rcLine.bottom)/2);
  135. pDC->SelectObject(pOldPen);
  136.         
  137. // pDC->DrawEdge(&rcLine, BDR_RAISEDINNER, BF_RECT);
  138. //开始画文字
  139. int nOldBkMode = pDC->SetBkMode(TRANSPARENT);
  140. pDC->SetTextColor(RGB(0, 0, 0));
  141. rcTxt.left = rcLine.right + m_nMargin;
  142. // rcTxt.top = rcLine.top;
  143. pDC->DrawText(lpItem->lpCaption,&rcTxt,DT_VCENTER|DT_END_ELLIPSIS|DT_NOCLIP|DT_SINGLELINE);
  144. pDC->SetBkMode(nOldBkMode);
  145. }
  146. }
  147. //################################################################
  148. LPBSCBITEM CLineComboBox::GetItem(int iIndex)
  149. {
  150. //当前的Item索引号
  151. POSITION pos = m_LineWidthList.FindIndex(iIndex);
  152. if(pos)
  153. {
  154. LPBSCBITEM lpItem = m_LineWidthList.GetAt(pos);
  155. ASSERT(lpItem);
  156. return lpItem;
  157. }
  158. else
  159. return (LPBSCBITEM)NULL;
  160. }
  161. //################################################################
  162. void CLineComboBox::OnMouseMove(UINT nFlags, CPoint point) 
  163. {
  164. CComboBox::OnMouseMove(nFlags, point);
  165. }
  166. int CLineComboBox::GetCurLineWidth()
  167. {
  168. int nSel=GetCurSel();
  169. POSITION pos = m_LineWidthList.FindIndex(nSel);
  170. if(pos)
  171. {
  172. LPBSCBITEM lpItem = m_LineWidthList.GetAt(pos);
  173. ASSERT(lpItem);
  174. return lpItem->nLineWidth;
  175. }
  176. else
  177. return 0;
  178. }