-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorStatic.cpp
576 lines (531 loc) · 15.3 KB
/
ColorStatic.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// ColorStatic.cpp : implementation file
//
#include "stdafx.h"
#include "ColorStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorStatic
CColorStatic::CColorStatic()
{
// Init our vars.
m_clBackColor=::GetSysColor(COLOR_BTNFACE);
m_clTextColor=::GetSysColor(COLOR_WINDOWTEXT);
m_nTextXPos=3;
m_nTextYPos=3;
m_nScrollXPos=m_nTextXPos;
m_nScrollYPos=m_nTextYPos;
m_nStep=5;
m_bIsSingleLine=true;
m_bUseRoll=false;
m_bUseSysFont=true;
m_bHCentered=false;
m_bVCentered=false;
m_bTextInfoGotten=false;
m_bRollPosSetted=false;
m_bRollTimerKilled=false;
m_bBkTransparent=false;
m_bDCStored=false;
m_nRollDirection=ROLL_LEFT;
}
CColorStatic::~CColorStatic()
{
}
BEGIN_MESSAGE_MAP(CColorStatic, CStatic)
//{{AFX_MSG_MAP(CColorStatic)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorStatic message handlers
///////////////////////////////////////////////////////////
// Name: OnEraseBkgnd(CDC* pDC)
///////////////////////////////////////////////////////////
// Function: Draw the background and the text.
//
///////////////////////////////////////////////////////////
BOOL CColorStatic::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect clientRect;
CRgn clipRgn;
GetClientRect(&clientRect);
if(!m_bDCStored)
{
CBitmap bitmap;
m_StoreDC.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC,clientRect.Width(),clientRect.Height());
m_StoreDC.SelectObject(&bitmap);
m_StoreDC.BitBlt(0,0,clientRect.Width(),clientRect.Height(),pDC,0,0,SRCCOPY);
m_bDCStored=true;
}
// Get our client rect and fill it with bk-color
GetClientRect(&clientRect);
if(m_bBkTransparent)
{
pDC->BitBlt(0,0,clientRect.Width(),clientRect.Height(),&m_StoreDC,0,0,SRCCOPY);
}
else
{
pDC->FillRect(&clientRect,&CBrush(m_clBackColor));
}
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(m_clTextColor);
// Get and Set our font.
CFont *pCurrentFont;
if(m_bUseSysFont)
{
// Get the dialog's font.
pCurrentFont= GetFont();
}
else
{
// Use our member font.
pCurrentFont=&m_font;
}
CFont *pOldFont = pDC->SelectObject(pCurrentFont);
// Set clip rgn.
clipRgn.CreateRectRgnIndirect(&clientRect);
pDC->SelectClipRgn(&clipRgn);
// Draw the Text.
if(m_sText)
{
// judge if the text is a single line.
m_bIsSingleLine=(m_sText.Find("\n")>=0)?false:true;
if(m_bIsSingleLine)
{
if(!m_bTextInfoGotten)
{
// We'll get the text width and height to roll it.
CSize size=pDC->GetTextExtent(m_sText);
m_nTextWidth=size.cx ;
m_nTextHeight=size.cy ;
// We have gotten the text info.
m_bTextInfoGotten=true;
}
if(m_bHCentered)
{
// H Center our text.
m_nTextXPos=(clientRect.Width()-m_nTextWidth)/2;
}
if(m_bVCentered)
{
// V center our text.
m_nTextYPos=(clientRect.Height()-m_nTextHeight)/2;
}
if(!m_bRollPosSetted)
{
// Set our start roll pos.
m_nScrollXPos=m_nTextXPos;
m_nScrollYPos=m_nTextYPos;
m_bRollPosSetted=true;
}
// Draw a single-line text.
pDC->TextOut(m_bUseRoll?m_nScrollXPos:m_nTextXPos,m_bUseRoll?m_nScrollYPos:m_nTextYPos,m_sText);
}
else
{
// Draw multi-line text.
CString sShow,sOthers;
sOthers=m_sText;
int width; // every line's width
int nPos=0; // new line's pos (in text)
int x=m_bUseRoll?m_nScrollXPos:m_nTextXPos;
int y=m_bUseRoll?m_nScrollYPos:m_nTextYPos;
if(!m_bTextInfoGotten)
{
m_nTextWidth=m_nTextHeight=0;
// Get the line-height.
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
m_nLineHeight=tm.tmHeight+tm.tmExternalLeading;
}
while(!sOthers.IsEmpty())
{
nPos=sOthers.Find("\n");
if(nPos>=0)
{
// trim left to get the line to show.
sShow=sOthers.Left(nPos);
// trim right.
sOthers=sOthers.Right(sOthers.GetLength()-nPos-1);
pDC->TextOut(x,y,sShow);
y+=m_nLineHeight;
// We'll get the multi-line text max width and height.
if(m_bUseRoll && !m_bTextInfoGotten)
{
// Get the text info.
CSize size=pDC->GetTextExtent(sShow);
width=size.cx;
if(width>m_nTextWidth)
m_nTextWidth=width;
m_nTextHeight+=m_nLineHeight;
}
}
else
{
// There is no more line,so just show it.
sShow=sOthers;
sOthers.Empty();
pDC->TextOut(x,y,sShow);
if(m_bUseRoll && !m_bTextInfoGotten)
{
CSize size=pDC->GetTextExtent(sShow);
width=size.cx;
if(width>m_nTextWidth)
m_nTextWidth=width;
m_nTextHeight+=m_nLineHeight;
m_bTextInfoGotten=true;
}
}
}
}
pDC->SelectObject(pOldFont);
}
return TRUE;
}
///////////////////////////////////////////////////////////
// Name: SetTextColor()
///////////////////////////////////////////////////////////
// Function: Change the color of the text.
// Parm: COLORREF clTextColor
///////////////////////////////////////////////////////////
// Remark: Call this if you want to change the text color.
///////////////////////////////////////////////////////////
void CColorStatic::SetTextColor(COLORREF clTextColor)
{
m_clTextColor=clTextColor;
Invalidate();
}
///////////////////////////////////////////////////////////
// Name: SetBkColor()
///////////////////////////////////////////////////////////
// Function: Change the color of the background.
// Parm: COLORREF clBkColor
///////////////////////////////////////////////////////////
// Remark: Call this if you want to change the back color.
///////////////////////////////////////////////////////////
void CColorStatic::SetBkColor(COLORREF clBkColor)
{
m_clBackColor=clBkColor;
Invalidate();
}
///////////////////////////////////////////////////////////
// We do not want the default effect,
// so we judt do nothing in function OnPaint()
///////////////////////////////////////////////////////////
void CColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CStatic::OnPaint() for painting messages
}
///////////////////////////////////////////////////////////
// Name: PreSubclassWindow()
///////////////////////////////////////////////////////////
// Function: Get the static's text.
///////////////////////////////////////////////////////////
// Remark: Because we don't use the SetWindowText() call,
// in stead we use SetText() call to change the text,
// so we must get the text here,not in OnEraseBkgnd().
///////////////////////////////////////////////////////////
void CColorStatic::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
GetWindowText(m_sText);
CStatic::PreSubclassWindow();
}
///////////////////////////////////////////////////////////
// Name: SetText()
///////////////////////////////////////////////////////////
// Function: Change the text of the ststic.
// Parm: CString sText
///////////////////////////////////////////////////////////
// Remark: Use this but not SetWindowText() if you need to
// change the text.
// **If we use the SetWindowText() call,there will be
// some dreadful effect.So I wrote this function.
///////////////////////////////////////////////////////////
void CColorStatic::SetText(CString sText)
{
if(m_sText!=sText)
{
m_sText=sText;
m_bTextInfoGotten=false;
m_bRollPosSetted=false;
Invalidate();
}
}
///////////////////////////////////////////////////////////
// Name: BeginRoll()
///////////////////////////////////////////////////////////
// Function: Set the vars and set a timer.
// Parm1: int nStep default:5
// - the move step of the rolling text.Increase this
// if you want the text rolling faster.
// Parm2: int nTimer default:100
// - the time interval of the rolling text.Decrease it
// if you want the text rolling faster.
///////////////////////////////////////////////////////////
void CColorStatic::BeginRoll(int nStep,int nTimer)
{
m_nStep=nStep;
m_bUseRoll=true;
m_bRollPosSetted=false;
SetTimer(ID_ROLLTIMER,nTimer,NULL);
}
///////////////////////////////////////////////////////////
// Name: SetRollDirection()
///////////////////////////////////////////////////////////
// Function: Set the rolling direction.
// Parm: int direction.value:
//
// ROLL_LEFT 0
// ROLL_RIGHT 1
// ROLL_UP 2
// ROLL_DOWN 3
///////////////////////////////////////////////////////////
// Remark: Use this before BeginRoll()
///////////////////////////////////////////////////////////
void CColorStatic::SetRollDirection(int direction)
{
m_nRollDirection=direction;
}
///////////////////////////////////////////////////////////
// Name: StopRoll()
///////////////////////////////////////////////////////////
// Function: kill the timer.
// Parm:
///////////////////////////////////////////////////////////
void CColorStatic::StopRoll()
{
m_bUseRoll=false;
KillTimer(ID_ROLLTIMER);
Invalidate();
m_bRollTimerKilled=true;
}
///////////////////////////////////////////////////////////
// Name: OnTimer(UINT nIDEvent)
///////////////////////////////////////////////////////////
// Function: Change the output text (if it is set to
// show current time) or caculate the output position
// (if it is set to roll).
// Parm:
///////////////////////////////////////////////////////////
void CColorStatic::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==ID_ROLLTIMER)
{
if(!m_bTextInfoGotten)
return;
CRect clientRect;
GetClientRect(&clientRect);
switch(m_nRollDirection)
{
case ROLL_LEFT:
{
if(m_nScrollXPos<=clientRect.left-m_nTextWidth)
{
m_nScrollXPos=clientRect.right;
}
else
{
m_nScrollXPos-=m_nStep;
}
break;
}
case ROLL_RIGHT:
{
if(m_nScrollXPos>=clientRect.right)
{
m_nScrollXPos=clientRect.left-m_nTextWidth;
}
else
{
m_nScrollXPos+=m_nStep;
}
break;
}
case ROLL_UP:
{
if(m_nScrollYPos<=clientRect.top-m_nTextHeight)
{
m_nScrollYPos=clientRect.bottom;
}
else
{
m_nScrollYPos-=m_nStep;
}
break;
}
case ROLL_DOWN:
{
if(m_nScrollYPos>=clientRect.bottom)
{
m_nScrollYPos=clientRect.top-m_nTextHeight;
}
else
{
m_nScrollYPos+=m_nStep;
}
break;
}
}
}
else if(nIDEvent==ID_CLOCKTIMER)
{
CTime time=CTime::GetCurrentTime ();
CString scurTime;
scurTime=time.Format ("%H:%M:%S");
SetText(scurTime);
}
Invalidate();
CStatic::OnTimer(nIDEvent);
}
///////////////////////////////////////////////////////////
// Name: HCenterText()
///////////////////////////////////////////////////////////
// Function: H center our text.
// Parm:
///////////////////////////////////////////////////////////
void CColorStatic::HCenterText()
{
m_bHCentered=true;
Invalidate();
}
///////////////////////////////////////////////////////////
// Name: VCenterText()
///////////////////////////////////////////////////////////
// Function: V center our text.
// Parm:
///////////////////////////////////////////////////////////
void CColorStatic::VCenterText()
{
m_bVCentered=true;
Invalidate();
}
///////////////////////////////////////////////////////////
// Name: SetTextXPos()
///////////////////////////////////////////////////////////
// Function: Set the output text's x position.
// Parm:
///////////////////////////////////////////////////////////
// Remark: Use this to change the x pos.
///////////////////////////////////////////////////////////
void CColorStatic::SetTextXPos(int x)
{
m_nScrollXPos=m_nTextXPos=x;
}
///////////////////////////////////////////////////////////
// Name: SetTextYPos(int y)
///////////////////////////////////////////////////////////
// Function:
// Parm:
///////////////////////////////////////////////////////////
void CColorStatic::SetTextYPos(int y)
{
m_nScrollYPos=m_nTextYPos=y;
}
///////////////////////////////////////////////////////////
// Name: SetTextFont()
///////////////////////////////////////////////////////////
// Function: Set the output font of the text.
// Parm: LOGFONT *lf
///////////////////////////////////////////////////////////
void CColorStatic::SetTextFont(LOGFONT *lf)
{
m_font.CreateFontIndirect(lf);
m_bUseSysFont=false;
Invalidate();
}
//´´½¨Ðº¯Êý£º
void CColorStatic::SetTextFont(int a, LPCTSTR b)
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight=a;
strcpy_s(lf.lfFaceName, b); // request a face name "Arial"
m_font.CreateFontIndirect(&lf);
m_bUseSysFont=false;
Invalidate();
}
///////////////////////////////////////////////////////////
// Name: SetTextHeight()
///////////////////////////////////////////////////////////
// Function: Set the height of the output text.
// Parm: int height
///////////////////////////////////////////////////////////
// Remark: Call this if you only want to change the
// the height of the text.
// If you call SetTextFont(LOGFONT *lf),
// make sure don't call this.
///////////////////////////////////////////////////////////
void CColorStatic::SetTextHeight(int height)
{
LOGFONT lf;
CFont *pDlgFont=GetFont();
pDlgFont->GetLogFont(&lf);
lf.lfHeight =height;
lf.lfWidth =0;
m_font.CreateFontIndirect(&lf);
m_bUseSysFont=false;
m_bTextInfoGotten=false;
Invalidate();
}
///////////////////////////////////////////////////////////
// Name: SetClock()
///////////////////////////////////////////////////////////
// Function: Set the static to show time or date.
// Parm: bool bTime, value
// - true show time
// - false show date
///////////////////////////////////////////////////////////
void CColorStatic::SetClock(bool bTime)
{
if(bTime)
{
SetTimer(ID_CLOCKTIMER,500,NULL);
HCenterText();
VCenterText();
}
else
{
CString scurTime;
CTime time=CTime::GetCurrentTime ();
scurTime=time.Format ("%Y-%m-%d");
SetText(scurTime);
HCenterText();
VCenterText();
}
}
///////////////////////////////////////////////////////////
// Name:
///////////////////////////////////////////////////////////
// Function:
// Parm:
///////////////////////////////////////////////////////////
void CColorStatic::OnDestroy()
{
CStatic::OnDestroy();
// TODO: Add your message handler code here
if(!m_bRollTimerKilled)
KillTimer(ID_ROLLTIMER);
KillTimer(ID_CLOCKTIMER);
}
void CColorStatic::SetBkTransparent(bool btranparent)
{
if(btranparent)
{
m_bDCStored = false;
m_StoreDC.DeleteDC();
}
m_bBkTransparent=btranparent;
Invalidate();
}