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