[go: up one dir, main page]

Menu

[r24]: / colorbtn.cpp  Maximize  Restore  History

Download this file

646 lines (462 with data), 14.9 kB

  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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// ColorBtn.cpp : implementation file
#include "stdafx.h"
#include "ColorBtn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// The color table, initialized to windows' 20 static colors
COLORREF CColorBtnDlg::colors[20] =
{
RGB(0,0,0),
RGB(128,0,0),
RGB(0,128,0),
RGB(128,128,0),
RGB(0,0,128),
RGB(128,0,128),
RGB(0,128,128),
RGB(192,192,192),
RGB(192,220,192),
RGB(166,202,240),
RGB(255,251,240),
RGB(160,160,164),
RGB(128,128,128),
RGB(255,0,0),
RGB(0,255,0),
RGB(255,255,0),
RGB(0,0,255),
RGB(255,0,255),
RGB(0,255,255),
RGB(255,255,255)
};
// MRU table. See notes for Reset()
BYTE CColorBtnDlg::used[20] =
{
1,3,5,7,9,11,13,15,17,19,20,18,16,14,12,10,8,6,4,2
};
/////////////////////////////////////////////////////////////////////////////
// CColorBtn
CColorBtn::CColorBtn()
{
currentcolor = RGB(255,255,255);
dlg.parent = this; // This will allow the dialog to position itself
// Create the pens and brushes that we'll need to draw the button
nullpen.CreateStockObject(NULL_PEN);
blackpen.CreateStockObject(BLACK_PEN);
whitepen.CreateStockObject(WHITE_PEN);
nullbrush.CreateStockObject(NULL_BRUSH);
backbrush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
dkgray.CreatePen(PS_SOLID,1,RGB(128,128,128));
}
CColorBtn::~CColorBtn()
{
}
BEGIN_MESSAGE_MAP(CColorBtn, CButton)
//{{AFX_MSG_MAP(CColorBtn)
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorBtn message handlers
void CColorBtn::DrawItem(LPDRAWITEMSTRUCT lpd)
{
// Draw the button
CBrush colorbrush;
CDC DC;
DC.Attach(lpd->hDC);
int top,left,bottom,right;
// Store this for convinience
top = lpd->rcItem.top;
left = lpd->rcItem.left;
bottom = lpd->rcItem.bottom;
right = lpd->rcItem.right;
colorbrush.CreateSolidBrush(currentcolor);
oldpen = DC.SelectObject(&nullpen);
oldbrush = DC.SelectObject(&backbrush);
// Clear the background using the 3DFACE color.
DC.Rectangle(&lpd->rcItem);
// Draw the border
if (!(lpd->itemState & ODS_SELECTED))
{
// Button is up
DC.SelectObject(&blackpen);
DC.MoveTo(left,bottom-1);
DC.LineTo(right-1,bottom-1);
DC.LineTo(right-1,top);
DC.SelectObject(&dkgray);
DC.MoveTo(left+1,bottom-2);
DC.LineTo(right-2,bottom-2);
DC.LineTo(right-2,top+1);
DC.SelectObject(&whitepen);
DC.LineTo(left+1,top+1);
DC.LineTo(left+1,bottom-2);
}
else
{
// Button is down
DC.SelectObject(&dkgray);
DC.MoveTo(left,bottom-1);
DC.LineTo(left,top);
DC.LineTo(right-1,top);
DC.SelectObject(&whitepen);
DC.MoveTo(right-1,top-1);
DC.LineTo(right-1,bottom-1);
DC.LineTo(left+1,bottom-1);
DC.SelectObject(&blackpen);
DC.MoveTo(left+1,bottom-2);
DC.LineTo(left+1,top+1);
DC.LineTo(right-2,top+1);
// by moving this, we get the things inside the button
// to draw themselves one pixel down and one to the right.
// This completes the "pushed" effect
left++;
right++;
bottom++;
top++;
}
// The division
DC.SelectObject(&whitepen);
DC.MoveTo(right-10,top+4);
DC.LineTo(right-10,bottom-4);
DC.SelectObject(dkgray);
DC.MoveTo(right-11,top+4);
DC.LineTo(right-11,bottom-4);
// The triangle
if (lpd->itemState & ODS_DISABLED)
DC.SelectObject(dkgray);
else
DC.SelectObject(blackpen);
DC.MoveTo(right-4,(bottom/2)-1);
DC.LineTo(right-9,(bottom/2)-1);
DC.MoveTo(right-5,(bottom/2));
DC.LineTo(right-8,(bottom/2));
if (lpd->itemState & ODS_DISABLED)
{
DC.SetPixel(right-4,(bottom/2)-1,RGB(255,255,255));
DC.SetPixel(right-5,(bottom/2),RGB(255,255,255));
DC.SetPixel(right-6,(bottom/2)+1,RGB(255,255,255));
}
else
{
DC.SetPixel(right-6,(bottom/2)+1,RGB(0,0,0));
}
if (!(lpd->itemState & ODS_DISABLED))
{
// The color rectangle, only if enabled
DC.SelectObject(&colorbrush);
DC.Rectangle(left+5,top+4,right-15,bottom-4);
}
if (lpd->itemState & ODS_FOCUS)
{
// Draw the focus
//
// It would have been nice just to
// draw a rectangle using a pen created
// with the PS_ALTERNATE style, but
// this is not supported by WIN95
int i;
for (i=left+3;i<right-4;i+=2)
{
DC.SetPixel(i,top+3,RGB(0,0,0));
DC.SetPixel(i,bottom-4,RGB(0,0,0));
}
for (i=top+3;i<bottom-4;i+=2)
{
DC.SetPixel(left+3,i,RGB(0,0,0));
DC.SetPixel(right-4,i,RGB(0,0,0));
}
}
DC.SelectObject(oldpen);
DC.SelectObject(oldbrush);
DC.Detach();
}
void CColorBtn::OnClicked()
{
// When the button is clicked, show the dialog.
if (dlg.DoModal() == IDOK)
{
currentcolor = CColorBtnDlg::colors[dlg.colorindex];
InvalidateRect(NULL);
}
}
// Store and Load use an undocumented CWinApp function
BOOL CColorBtn::Store()
{
return (AfxGetApp()->WriteProfileBinary("ColorData", "ColorTable",(LPBYTE)CColorBtnDlg::colors,sizeof(COLORREF)*20) &&
AfxGetApp()->WriteProfileBinary("ColorData", "MRU",(LPBYTE)CColorBtnDlg::used,sizeof(BYTE)*20));
}
BOOL CColorBtn::Load()
{
BYTE *data = NULL;
UINT size;
// This function allocates the memory it needs
AfxGetApp()->GetProfileBinary("ColorData", "ColorTable",&data,&size);
if (data)
{
// Copy the data into our table and get rid of the buffer
memcpy((void *)CColorBtnDlg::colors,(void *)data,size);
free((void *)data);
AfxGetApp()->GetProfileBinary("ColorData", "MRU",&data,&size);
if (data)
{
memcpy((void *)CColorBtnDlg::used,(void *)data,size);
free((void *)data);
return TRUE;
}
}
// If the loading fails, back to the defaults
Reset();
return FALSE;
}
void CColorBtn::Reset()
{
CColorBtnDlg::colors[0] = RGB(0,0,0);
CColorBtnDlg::colors[1] = RGB(128,0,0);
CColorBtnDlg::colors[2] = RGB(0,128,0);
CColorBtnDlg::colors[3] = RGB(128,128,0);
CColorBtnDlg::colors[4] = RGB(0,0,128);
CColorBtnDlg::colors[5] = RGB(128,0,128);
CColorBtnDlg::colors[6] = RGB(0,128,128);
CColorBtnDlg::colors[7] = RGB(192,192,192);
CColorBtnDlg::colors[8] = RGB(192,220,192);
CColorBtnDlg::colors[9] = RGB(166,202,240);
CColorBtnDlg::colors[10] = RGB(255,251,240);
CColorBtnDlg::colors[11] = RGB(160,160,164);
CColorBtnDlg::colors[12] = RGB(128,128,128);
CColorBtnDlg::colors[13] = RGB(255,0,0);
CColorBtnDlg::colors[14] = RGB(0,255,0);
CColorBtnDlg::colors[15] = RGB(255,255,0);
CColorBtnDlg::colors[16] = RGB(0,0,255);
CColorBtnDlg::colors[17] = RGB(255,0,255);
CColorBtnDlg::colors[18] = RGB(0,255,255);
CColorBtnDlg::colors[19] = RGB(255,255,255);
// This "colorful" (no pun intended) order ensures
// that the colors at the center of the color table
// will get replaced first. This preserves the white
// and black colors even if they're not used (They'll
// get replaced last).
CColorBtnDlg::used[0]= 1;
CColorBtnDlg::used[1]= 3;
CColorBtnDlg::used[2]= 5;
CColorBtnDlg::used[3]= 7;
CColorBtnDlg::used[4]= 9;
CColorBtnDlg::used[5]= 11;
CColorBtnDlg::used[6]= 13;
CColorBtnDlg::used[7]= 15;
CColorBtnDlg::used[8]= 17;
CColorBtnDlg::used[9]= 19;
CColorBtnDlg::used[10]= 20;
CColorBtnDlg::used[11]= 18;
CColorBtnDlg::used[12]= 16;
CColorBtnDlg::used[13]= 14;
CColorBtnDlg::used[14]= 12;
CColorBtnDlg::used[15]= 10;
CColorBtnDlg::used[16]= 8;
CColorBtnDlg::used[17]= 6;
CColorBtnDlg::used[18]= 4;
CColorBtnDlg::used[19]= 2;
}
void CColorBtn::Serialize( CArchive& ar )
{
if (ar.IsStoring())
{
ar.Write((void *)CColorBtnDlg::colors,sizeof(COLORREF)*20);
ar.Write((void *)CColorBtnDlg::used,sizeof(BYTE)*20);
}
else
{
ar.Read((void *)CColorBtnDlg::colors,sizeof(COLORREF)*20);
ar.Read((void *)CColorBtnDlg::used,sizeof(BYTE)*20);
}
}
/////////////////////////////////////////////////////////////////////////////
// CColorBtnDlg dialog
CColorBtnDlg::CColorBtnDlg(CWnd* pParent /*=NULL*/)
: CDialog(CColorBtnDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CColorBtnDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CColorBtnDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CColorBtnDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CColorBtnDlg, CDialog)
//{{AFX_MSG_MAP(CColorBtnDlg)
ON_BN_CLICKED(IDC_OTHER, OnOther)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_DRAWITEM()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(IDC_COLOR1,IDC_COLOR20,OnColor)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorBtnDlg message handlers
BOOL CColorBtnDlg::OnInitDialog()
{
CDialog::OnInitDialog();
RECT r,r2;
parent->GetWindowRect(&r);
// Move the dialog to be below the button
SetWindowPos(NULL,r.left,r.bottom,0,0,SWP_NOSIZE|SWP_NOZORDER);
GetWindowRect(&r2);
// Check to see if the dialog has a portion outside the
// screen, if so, adjust.
if (r2.bottom > GetSystemMetrics(SM_CYSCREEN))
{
r2.top = r.top-(r2.bottom-r2.top);
}
if (r2.right > GetSystemMetrics(SM_CXSCREEN))
{
r2.left = GetSystemMetrics(SM_CXSCREEN) - (r2.right-r2.left);
}
SetWindowPos(NULL,r2.left,r2.top,0,0,SWP_NOSIZE|SWP_NOZORDER);
// Capture the mouse, this allows the dialog to close when
// the user clicks outside.
// Remember that the dialog has no "close" button.
SetCapture();
return TRUE;
}
void CColorBtnDlg::EndDialog( int nResult )
{
ReleaseCapture();
CDialog::EndDialog(nResult);
}
void CColorBtnDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
RECT r;
POINT p;
p.x = point.x;
p.y = point.y;
ClientToScreen(&p);
GetWindowRect(&r);
// The user clicked...
if (!PtInRect(&r,p))
{
// ...outside the dialog, close.
EndDialog(IDCANCEL);
}
else
{
// ...inside the dialog. Since this window
// has the mouse captured, its children
// get no messages. So, check to see
// if the click was in one of its children
// and tell him.
// If the user clicks inside the dialog
// but not on any of the controls,
// ChildWindowFromPoint returns a
// pointer to the dialog. In this
// case we do not resend the message
// (obviously) because it would cause
// a stack overflow.
CWnd *child = ChildWindowFromPoint(point);
if (child && child != this)
child->SendMessage(WM_LBUTTONDOWN,0,0l);
}
CDialog::OnLButtonDown(nFlags, point);
}
void CColorBtnDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpd)
{
CDC dc;
CPen nullpen;
CBrush brush;
CPen *oldpen;
CBrush *oldbrush;
// Draw the wells using the current color table
nullpen.CreateStockObject(NULL_PEN);
brush.CreateSolidBrush(colors[nIDCtl-IDC_COLOR1]);
dc.Attach(lpd->hDC);
oldpen = dc.SelectObject(&nullpen);
oldbrush = dc.SelectObject(&brush);
lpd->rcItem.right++;
lpd->rcItem.bottom++;
dc.Rectangle(&lpd->rcItem);
dc.SelectObject(oldpen);
dc.SelectObject(oldbrush);
dc.Detach();
CDialog::OnDrawItem(nIDCtl, lpd);
}
void CColorBtnDlg::OnColor(UINT id)
{
// A well has been clicked, set the color index
// and close.
colorindex = id-IDC_COLOR1;
int i;
// This color is now the MRU
for (i=0;i<20;i++)
{
if (used[colorindex] > used[i])
{
used[i]++;
}
}
used[colorindex] = 1;
EndDialog(IDOK);
}
void CColorBtnDlg::OnOther()
{
int i;
COLORREF newcolor;
// The "Other" button.
ReleaseCapture();
CColorDialog dlg;
dlg.m_cc.Flags |= CC_FULLOPEN;
if (dlg.DoModal() == IDOK)
{
// The user clicked OK.
// set the color and close
newcolor = dlg.GetColor();
// Check to see if the selected color is
// already in the table.
colorindex = -1;
for (i=0;i<20;i++)
{
if (colors[i] == newcolor)
{
colorindex = i;
break;
}
}
// If the color was not found,
// replace the LRU with this color
if (colorindex == -1)
{
for (i=0;i<20;i++)
{
if (used[i] == 20)
{
colors[i] = newcolor;
colorindex = i;
break;
}
}
}
// This is the new MRU
for (i=0;i<20;i++)
{
if (used[colorindex] > used[i])
{
used[i]++;
}
}
used[colorindex] = 1;
EndDialog(IDOK);
return;
}
// If the user clicked "Cancel" reclaim the mouse capture.
SetCapture();
}
void CColorBtnDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// See notes for OnLButtonDown.
CWnd *child = ChildWindowFromPoint(point,CWP_ALL);
if (child && child != this)
child->SendMessage(WM_LBUTTONDOWN,0,0l);
CDialog::OnLButtonUp(nFlags, point);
}