chenluhua1980
2 天以前 351086486441b80cfe71550b43cbe1e4dc440f5d
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// CSlotTableCtrl.cpp
#include "stdafx.h"
#include "CSlotTableCtrl.h"
 
BEGIN_MESSAGE_MAP(CSlotTableCtrl, CWnd)
    ON_WM_PAINT()
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
 
BOOL CSlotTableCtrl::Create(CWnd* pParent, const RECT& rc, UINT nID)
{
    CString className = AfxRegisterWndClass(
        CS_HREDRAW | CS_VREDRAW,
        ::LoadCursor(NULL, IDC_ARROW),
        (HBRUSH)::GetStockObject(WHITE_BRUSH),
        NULL);
 
    return CWnd::CreateEx(0, className, _T(""), WS_CHILD | WS_VISIBLE, rc, pParent, nID);
}
 
void CSlotTableCtrl::SetRows(const std::vector<Row>& rows)
{
    m_rows = rows;
    if (m_rows.size() > 8) {
        m_rows.resize(8);
    }
    Invalidate();
}
 
void CSlotTableCtrl::SetTitle(const CString& title)
{
    m_title = title;
    Invalidate();
}
 
void CSlotTableCtrl::SetRowHeight(int height)
{
    if (height < 14) height = 14;
    if (height > 40) height = 40;
    m_rowHeight = height;
    Invalidate();
}
 
void CSlotTableCtrl::SetPadding(int padding)
{
    if (padding < 2) padding = 2;
    if (padding > 16) padding = 16;
    m_padding = padding;
    Invalidate();
}
 
void CSlotTableCtrl::SetHeaderHeight(int height)
{
    if (height < 16) height = 16;
    if (height > 40) height = 40;
    m_headerHeight = height;
    Invalidate();
}
 
void CSlotTableCtrl::SetTitleHeight(int height)
{
    if (height < 16) height = 16;
    if (height > 40) height = 40;
    m_titleHeight = height;
    Invalidate();
}
 
void CSlotTableCtrl::SetLineColor(COLORREF color)
{
    m_lineColor = color;
    Invalidate();
}
 
void CSlotTableCtrl::SetHeaderBgColor(COLORREF color)
{
    m_headerBgColor = color;
    Invalidate();
}
 
void CSlotTableCtrl::Clear()
{
    m_rows.clear();
    m_title.Empty();
    Invalidate();
}
 
BOOL CSlotTableCtrl::OnEraseBkgnd(CDC* /*pDC*/)
{
    return TRUE;
}
 
void CSlotTableCtrl::OnPaint()
{
    CPaintDC dc(this);
    CRect rcClient;
    GetClientRect(&rcClient);
 
    CFont* pOldFont = (CFont*)dc.SelectStockObject(DEFAULT_GUI_FONT);
    dc.SetBkMode(TRANSPARENT);
 
    const COLORREF kBorder = m_lineColor;
    const COLORREF kHeaderBg = m_headerBgColor;
    const COLORREF kGrid = m_lineColor;
    const COLORREF kText = RGB(30, 30, 30);
    const COLORREF kTitle = RGB(80, 80, 80);
 
    dc.FillSolidRect(&rcClient, RGB(255, 255, 255));
    dc.SetTextColor(kText);
 
    const int titleHeight = m_title.IsEmpty() ? 0 : m_titleHeight;
    const int headerHeight = m_headerHeight;
    const int rowHeight = m_rowHeight;
 
    CRect rcTitle = rcClient;
    rcTitle.bottom = rcTitle.top + titleHeight;
    if (titleHeight > 0) {
        dc.FillSolidRect(&rcTitle, RGB(250, 250, 250));
        CRect rcTitleText = rcTitle;
        rcTitleText.DeflateRect(6, 2);
        dc.SetTextColor(kTitle);
        dc.DrawText(m_title, &rcTitleText, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
        dc.SetTextColor(kText);
    }
 
    CRect rcHeader = rcClient;
    rcHeader.top += titleHeight;
    rcHeader.bottom = rcHeader.top + headerHeight;
    dc.FillSolidRect(&rcHeader, kHeaderBg);
 
    int totalW = rcClient.Width();
    int col1 = max(40, totalW * 20 / 100);
    int col3 = max(40, totalW * 20 / 100);
    int col2 = totalW - col1 - col3;
    if (col2 < 60) col2 = 60;
    if (col1 + col2 + col3 > totalW) {
        col3 = max(30, totalW - col1 - col2);
    }
 
    int x1 = rcClient.left;
    int x2 = x1 + col1;
    int x3 = x2 + col2;
 
    CRect rc;
    rc = rcHeader; rc.right = x2; rc.DeflateRect(m_padding, 0);
    dc.DrawText(_T("Slot"), &rc, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
    rc = rcHeader; rc.left = x2; rc.right = x3; rc.DeflateRect(m_padding, 0);
    dc.DrawText(_T("Glass ID"), &rc, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
    rc = rcHeader; rc.left = x3; rc.DeflateRect(m_padding, 0);
    dc.DrawText(_T("G1/G2"), &rc, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
 
    // grid lines
    dc.FillSolidRect(rcClient.left, rcHeader.bottom - 1, rcClient.Width(), 1, kGrid);
    dc.FillSolidRect(x2 - 1, rcHeader.top, 1, rcClient.bottom - rcHeader.top, kGrid);
    dc.FillSolidRect(x3 - 1, rcHeader.top, 1, rcClient.bottom - rcHeader.top, kGrid);
 
    int bodyTop = rcHeader.bottom;
    if (m_rows.empty()) {
        CRect rcBody = rcClient;
        rcBody.top = bodyTop + 4;
        dc.DrawText(_T("No slot data"), &rcBody, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
    else {
        for (size_t i = 0; i < m_rows.size(); ++i) {
            CRect rcRow = rcClient;
            rcRow.top = bodyTop + static_cast<int>(i) * rowHeight;
            rcRow.bottom = rcRow.top + rowHeight;
            if (rcRow.top >= rcClient.bottom) break;
            // 行分隔线(最后一行不画,避免底部多一条线)
            if (i + 1 < m_rows.size() && rcRow.bottom < rcClient.bottom - 1) {
                dc.FillSolidRect(rcRow.left, rcRow.bottom - 1, rcClient.Width(), 1, kGrid);
            }
 
            CRect rcCell;
            rcCell = rcRow; rcCell.right = x2; rcCell.DeflateRect(m_padding, 0);
            dc.DrawText(m_rows[i].slot, &rcCell, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
            rcCell = rcRow; rcCell.left = x2; rcCell.right = x3; rcCell.DeflateRect(m_padding, 0);
            dc.DrawText(m_rows[i].glassId, &rcCell, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
            rcCell = rcRow; rcCell.left = x3; rcCell.DeflateRect(m_padding, 0);
            dc.DrawText(m_rows[i].type, &rcCell, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
        }
    }
 
    // border
    dc.Draw3dRect(&rcClient, kBorder, kBorder);
 
    dc.SelectObject(pOldFont);
}