-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorListBox.cpp
275 lines (245 loc) · 8.49 KB
/
ColorListBox.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// ColorListBox.cpp : implementation file
//-------------------------------------------------------------------
//
// CColorListBox class -
// A CListBox-derived class with optional colored items.
//
// Version: 1.0 01/10/1998 Copyright © Patrice Godard
//
// Version: 2.0 09/17/1999 Copyright © Paul M. Meidinger
//
//-------------------------------------------------------------------
#include "stdafx.h"
#include "ColorListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorListBox
//-------------------------------------------------------------------
//
CColorListBox::CColorListBox()
//
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Standard constructor.
//
{
} // CColorListBox
//-------------------------------------------------------------------
//
CColorListBox::~CColorListBox()
//
// Return Value: None.
//
// Parameters : None.
//
// Remarks : Destructor.
//
{
} // ~CColorListBox()
BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
//{{AFX_MSG_MAP(CColorListBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorListBox message handlers
//-------------------------------------------------------------------
//
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
//
// Return Value: None.
//
// Parameters : lpDIS - A long pointer to a DRAWITEMSTRUCT structure
// that contains information about the type of drawing required.
//
// Remarks : Called by the framework when a visual aspect of
// an owner-draw list box changes.
//
{
if ((int)lpDIS->itemID < 0)
return;
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
COLORREF crText;
CString sText;
COLORREF crNorm = (COLORREF)lpDIS->itemData; // Color information is in item data.
COLORREF crHilite = RGB(255-GetRValue(crNorm), 255-GetGValue(crNorm), 255-GetBValue(crNorm));
// If item has been selected, draw the highlight rectangle using the item's color.
if ((lpDIS->itemState & ODS_SELECTED) &&
(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush brush(crNorm);
pDC->FillRect(&lpDIS->rcItem, &brush);
}
// If item has been deselected, draw the rectangle using the window color.
if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
{
CBrush brush(::GetSysColor(COLOR_WINDOW));
pDC->FillRect(&lpDIS->rcItem, &brush);
}
// If item has focus, draw the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);
// If item does not have focus, redraw (erase) the focus rect.
if ((lpDIS->itemAction & ODA_FOCUS) && !(lpDIS->itemState & ODS_FOCUS))
pDC->DrawFocusRect(&lpDIS->rcItem);
// Set the background mode to TRANSPARENT to draw the text.
int nBkMode = pDC->SetBkMode(TRANSPARENT);
// If the item's color information is set, use the highlight color
// gray text color, or normal color for the text.
if (lpDIS->itemData)
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(crHilite);
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(crNorm);
}
// Else the item's color information is not set, so use the
// system colors for the text.
else
{
if (lpDIS->itemState & ODS_SELECTED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
else if (lpDIS->itemState & ODS_DISABLED)
crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
}
// Get and display item text.
GetText(lpDIS->itemID, sText);
CRect rect = lpDIS->rcItem;
// Setup the text format.
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
if (GetStyle() & LBS_USETABSTOPS)
nFormat |= DT_EXPANDTABS;
// Calculate the rectangle size before drawing the text.
pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
pDC->DrawText(sText, -1, &rect, nFormat);
pDC->SetTextColor(crText);
pDC->SetBkMode(nBkMode);
} // DrawItem
//-------------------------------------------------------------------
//
void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
//
// Return Value: None.
//
// Parameters : lpMIS - A long pointer to a
// MEASUREITEMSTRUCT structure.
//
// Remarks : Called by the framework when a list box with
// an owner-draw style is created.
//
{
// ### Is the default list box item height the same as
// the menu check height???
lpMIS->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
} // MeasureItem
//-------------------------------------------------------------------
//
int CColorListBox::AddString(LPCTSTR lpszItem)
//
// Return Value: The zero-based index to the string in the list box.
// The return value is LB_ERR if an error occurs; the
// return value is LB_ERRSPACE if insufficient space
// is available to store the new string.
//
// Parameters : lpszItem - Points to the null-terminated
// string that is to be added.
//
// Remarks : Call this member function to add a string to a list
// box. Provided because CListBox::AddString is NOT
// a virtual function.
//
{
return ((CListBox*)this)->AddString(lpszItem);
} // AddString
//-------------------------------------------------------------------
//
int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb)
//
// Return Value: The zero-based index to the string in the list box.
// The return value is LB_ERR if an error occurs; the
// return value is LB_ERRSPACE if insufficient space
// is available to store the new string.
//
// Parameters : lpszItem - Points to the null-terminated
// string that is to be added.
// rgb - Specifies the color to be associated with the item.
//
// Remarks : Call this member function to add a string to a list
// box with a custom color.
//
{
int nItem = AddString(lpszItem);
if (nItem >= 0)
SetItemData(nItem, rgb);
return nItem;
} // AddString
//-------------------------------------------------------------------
//
int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem)
//
// Return Value: The zero-based index of the position at which the
// string was inserted. The return value is LB_ERR if
// an error occurs; the return value is LB_ERRSPACE if
// insufficient space is available to store the new string.
//
// Parameters : nIndex - Specifies the zero-based index of the position
// to insert the string. If this parameter is –1, the string
// is added to the end of the list.
// lpszItem - Points to the null-terminated string that
// is to be inserted.
//
// Remarks : Inserts a string into the list box. Provided because
// CListBox::InsertString is NOT a virtual function.
//
{
return ((CListBox*)this)->InsertString(nIndex, lpszItem);
} // InsertString
//-------------------------------------------------------------------
//
int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb)
//
// Return Value: The zero-based index of the position at which the
// string was inserted. The return value is LB_ERR if
// an error occurs; the return value is LB_ERRSPACE if
// insufficient space is available to store the new string.
//
// Parameters : nIndex - Specifies the zero-based index of the position
// to insert the string. If this parameter is –1, the string
// is added to the end of the list.
// lpszItem - Points to the null-terminated string that
// is to be inserted.
// rgb - Specifies the color to be associated with the item.
//
// Remarks : Inserts a colored string into the list box.
//
{
int nItem = ((CListBox*)this)->InsertString(nIndex,lpszItem);
if (nItem >= 0)
SetItemData(nItem, rgb);
return nItem;
} // InsertString
//-------------------------------------------------------------------
//
void CColorListBox::SetItemColor(int nIndex, COLORREF rgb)
//
// Return Value: None.
//
// Parameters : nIndex - Specifies the zero-based index of the item.
// rgb - Specifies the color to be associated with the item.
//
// Remarks : Sets the 32-bit value associated with the specified
// item in the list box.
//
{
SetItemData(nIndex, rgb);
RedrawWindow();
} // SetItemColor