HeaderCtrlCl.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // HeaderCtrlCl.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "HeaderCtrlCl.h"
  5. // CHeaderCtrlCl
  6. IMPLEMENT_DYNAMIC(CHeaderCtrlCl, CHeaderCtrl)
  7. CHeaderCtrlCl::CHeaderCtrlCl()
  8. : m_R(171)
  9. , m_G(199)
  10. , m_B(235)
  11. , m_Gradient(8)
  12. {
  13. m_Format = "";
  14. m_Height = 1;
  15. m_fontHeight = 15;
  16. m_fontWith = 0;
  17. m_color = RGB(0,0,0);
  18. }
  19. CHeaderCtrlCl::~CHeaderCtrlCl()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(CHeaderCtrlCl, CHeaderCtrl)
  23. ON_WM_PAINT()
  24. ON_MESSAGE(HDM_LAYOUT, OnLayout)
  25. END_MESSAGE_MAP()
  26. // CHeaderCtrlCl 消息处理程序
  27. void CHeaderCtrlCl::OnPaint()
  28. {
  29. CPaintDC dc(this); // device context for painting
  30. // TODO: 在此处添加消息处理程序代码
  31. // 不为绘图消息调用 CHeaderCtrl::OnPaint()
  32. int nItem;
  33. nItem = GetItemCount();//得到有几个单元
  34. for(int i = 0; i<nItem;i ++)
  35. {
  36. CRect tRect;
  37. GetItemRect(i,&tRect);//得到Item的尺寸
  38. int R = m_R,G = m_G,B = m_B;
  39. CRect nRect(tRect);//拷贝尺寸到新的容器中
  40. nRect.left++;//留出分割线的地方
  41. //绘制立体背景
  42. for(int j = tRect.top;j<=tRect.bottom;j++)
  43. {
  44. nRect.bottom = nRect.top+1;
  45. CBrush _brush;
  46. _brush.CreateSolidBrush(RGB(R,G,B));//创建画刷
  47. dc.FillRect(&nRect,&_brush); //填充背景
  48. _brush.DeleteObject(); //释放画刷
  49. R-=m_Gradient;G-=m_Gradient;B-=m_Gradient;
  50. if (R<0)R = 0;
  51. if (G<0)G = 0;
  52. if (B<0)B= 0;
  53. nRect.top = nRect.bottom;
  54. }
  55. dc.SetBkMode(TRANSPARENT);
  56. CFont nFont ,* nOldFont;
  57. //dc.SetTextColor(RGB(250,50,50));
  58. dc.SetTextColor(m_color);
  59. nFont.CreateFont(m_fontHeight,m_fontWith,0,0,0,FALSE,FALSE,0,0,0,0,0,0,_TEXT("宋体"));//创建字体
  60. nOldFont = dc.SelectObject(&nFont);
  61. UINT nFormat = 1;
  62. if (m_Format[i]=='0')
  63. {
  64. nFormat = DT_LEFT;
  65. tRect.left+=3;
  66. }
  67. else if (m_Format[i]=='1')
  68. {
  69. nFormat = DT_CENTER;
  70. }
  71. else if (m_Format[i]=='2')
  72. {
  73. nFormat = DT_RIGHT;
  74. tRect.right-=3;
  75. }
  76. TEXTMETRIC metric;
  77. dc.GetTextMetrics(&metric);
  78. int ofst = 0;
  79. ofst = tRect.Height() - metric.tmHeight;
  80. tRect.OffsetRect(0,ofst/2);
  81. dc.DrawText(m_HChar[i],&tRect,nFormat);
  82. dc.SelectObject(nOldFont);
  83. nFont.DeleteObject(); //释放字体
  84. }
  85. //画头部剩余部分
  86. CRect rtRect;
  87. CRect clientRect;
  88. GetItemRect(nItem - 1,rtRect);
  89. GetClientRect(clientRect);
  90. rtRect.left = rtRect.right+1;
  91. rtRect.right = clientRect.right;
  92. int R = m_R,G = m_G,B = m_B;
  93. CRect nRect(rtRect);
  94. //绘制立体背景
  95. for(int j = rtRect.top;j<=rtRect.bottom;j++)
  96. {
  97. nRect.bottom = nRect.top+1;
  98. CBrush _brush;
  99. _brush.CreateSolidBrush(RGB(R,G,B));//创建画刷
  100. dc.FillRect(&nRect,&_brush); //填充背景
  101. _brush.DeleteObject(); //释放画刷
  102. R-=m_Gradient;G-=m_Gradient;B-=m_Gradient;
  103. if (R<0)R = 0;
  104. if (G<0)G = 0;
  105. if (B<0)B= 0;
  106. nRect.top = nRect.bottom;
  107. }
  108. }
  109. LRESULT CHeaderCtrlCl::OnLayout( WPARAM wParam, LPARAM lParam )
  110. {
  111. LRESULT lResult = CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);
  112. HD_LAYOUT &hdl = *( HD_LAYOUT * ) lParam;
  113. RECT *prc = hdl.prc;
  114. WINDOWPOS *pwpos = hdl.pwpos;
  115. //表头高度为原来1.5倍,如果要动态修改表头高度的话,将1.5设成一个全局变量
  116. int nHeight = (int)(pwpos->cy * m_Height);
  117. pwpos->cy = nHeight;
  118. prc->top = nHeight;
  119. return lResult;
  120. }