-
Notifications
You must be signed in to change notification settings - Fork 0
/
BtnST.cpp
2115 lines (1824 loc) · 55.9 KB
/
BtnST.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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "BtnST.h"
#include "emule.h"
#ifdef BTNST_USE_SOUND
#define MMNODRV // mmsystem: Installable driver support
//#define MMNOSOUND // mmsystem: Sound support
#define MMNOWAVE // mmsystem: Waveform support
#define MMNOMIDI // mmsystem: MIDI support
#define MMNOAUX // mmsystem: Auxiliary audio support
#define MMNOMIXER // mmsystem: Mixer support
#define MMNOTIMER // mmsystem: Timer support
#define MMNOJOY // mmsystem: Joystick support
#define MMNOMCI // mmsystem: MCI support
#define MMNOMMIO // mmsystem: Multimedia file I/O support
#define MMNOMMSYSTEM // mmsystem: General MMSYSTEM functions
#include <Mmsystem.h>
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonST
// Mask for control's type
#ifdef BS_TYPEMASK
#undef BS_TYPEMASK
#endif
#define BS_TYPEMASK SS_TYPEMASK
CButtonST::CButtonST()
{
m_bIsPressed = FALSE;
m_bIsFocused = FALSE;
m_bIsDisabled = FALSE;
m_bMouseOnButton = FALSE;
leftAligned = false;
marked = false;
FreeResources(FALSE);
// Default type is "flat" button
m_bIsFlat = TRUE;
// Button will be tracked also if when the window is inactive (like Internet Explorer)
m_bAlwaysTrack = TRUE;
// By default draw border in "flat" button
m_bDrawBorder = TRUE;
// By default icon is aligned horizontally
m_byAlign = ST_ALIGN_HORIZ;
// By default use usual pressed style
SetPressedStyle(BTNST_PRESSED_LEFTRIGHT, FALSE);
// By default, for "flat" button, don't draw the focus rect
m_bDrawFlatFocus = FALSE;
// By default the button is not the default button
m_bIsDefault = FALSE;
// Invalid value, since type still unknown
m_nTypeStyle = BS_TYPEMASK;
// By default the button is not a checkbox
m_bIsCheckBox = FALSE;
m_nCheck = 0;
// Set default colors
SetDefaultColors(FALSE);
// No tooltip created
m_ToolTip.m_hWnd = NULL;
// Do not draw as a transparent button
m_bDrawTransparent = FALSE;
m_pbmpOldBk = NULL;
// No URL defined
SetURL(NULL);
// No cursor defined
m_hCursor = NULL;
// No associated menu
#ifndef BTNST_USE_BCMENU
m_hMenu = NULL;
#endif
m_hParentWndMenu = NULL;
m_bMenuDisplayed = FALSE;
m_bShowDisabledBitmap = TRUE;
m_ptImageOrg.x = 3;
m_ptImageOrg.y = 3;
// No defined callbacks
::ZeroMemory(&m_csCallbacks, sizeof(m_csCallbacks));
#ifdef BTNST_USE_SOUND
// No defined sounds
::ZeroMemory(&m_csSounds, sizeof(m_csSounds));
#endif
} // End of CButtonST
CButtonST::~CButtonST()
{
// Restore old bitmap (if any)
if (m_dcBk.m_hDC && m_pbmpOldBk)
{
m_dcBk.SelectObject(m_pbmpOldBk);
} // if
FreeResources();
// Destroy the cursor (if any)
if (m_hCursor) ::DestroyCursor(m_hCursor);
// Destroy the menu (if any)
#ifdef BTNST_USE_BCMENU
if (m_menuPopup.m_hMenu) VERIFY( m_menuPopup.DestroyMenu() );
#else
if (m_hMenu) VERIFY( ::DestroyMenu(m_hMenu) );
#endif
} // End of ~CButtonST
BEGIN_MESSAGE_MAP(CButtonST, CButton)
//{{AFX_MSG_MAP(CButtonST)
ON_WM_SETCURSOR()
ON_WM_KILLFOCUS()
ON_WM_MOUSEMOVE()
ON_WM_SYSCOLORCHANGE()
ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
ON_WM_ACTIVATE()
ON_WM_ENABLE()
ON_WM_CANCELMODE()
ON_WM_GETDLGCODE()
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
#ifdef BTNST_USE_BCMENU
ON_WM_MENUCHAR()
ON_WM_MEASUREITEM()
#endif
ON_MESSAGE(BM_SETSTYLE, OnSetStyle)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(BM_SETCHECK, OnSetCheck)
ON_MESSAGE(BM_GETCHECK, OnGetCheck)
END_MESSAGE_MAP()
void CButtonST::FreeResources(BOOL bCheckForNULL)
{
if (bCheckForNULL)
{
// Destroy icons
// Note: the following two lines MUST be here! even if
// BoundChecker says they are unnecessary!
if (m_csIcons[0].hIcon)
VERIFY( ::DestroyIcon(m_csIcons[0].hIcon) );
if (m_csIcons[1].hIcon)
VERIFY( ::DestroyIcon(m_csIcons[1].hIcon) );
// Destroy bitmaps
if (m_csBitmaps[0].hBitmap)
VERIFY( ::DeleteObject(m_csBitmaps[0].hBitmap) );
if (m_csBitmaps[1].hBitmap)
VERIFY( ::DeleteObject(m_csBitmaps[1].hBitmap) );
// Destroy mask bitmaps
if (m_csBitmaps[0].hMask)
VERIFY( ::DeleteObject(m_csBitmaps[0].hMask) );
if (m_csBitmaps[1].hMask)
VERIFY( ::DeleteObject(m_csBitmaps[1].hMask) );
} // if
::ZeroMemory(&m_csIcons, sizeof(m_csIcons));
::ZeroMemory(&m_csBitmaps, sizeof(m_csBitmaps));
} // End of FreeResources
void CButtonST::PreSubclassWindow()
{
UINT nBS;
nBS = GetButtonStyle();
// Set initial control type
m_nTypeStyle = nBS & BS_TYPEMASK;
// Check if this is a checkbox
if (nBS & BS_CHECKBOX) m_bIsCheckBox = TRUE;
// Set initial default state flag
if (m_nTypeStyle == BS_DEFPUSHBUTTON)
{
// Set default state for a default button
m_bIsDefault = TRUE;
// Adjust style for default button
m_nTypeStyle = BS_PUSHBUTTON;
} // If
// You should not set the Owner Draw before this call
// (don't use the resource editor "Owner Draw" or
// ModifyStyle(0, BS_OWNERDRAW) before calling PreSubclassWindow() )
ASSERT(m_nTypeStyle != BS_OWNERDRAW);
// Switch to owner-draw
ModifyStyle(BS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
CButton::PreSubclassWindow();
} // End of PreSubclassWindow
UINT CButtonST::OnGetDlgCode()
{
UINT nCode = CButton::OnGetDlgCode();
// Tell the system if we want default state handling
// (losing default state always allowed)
nCode |= (m_bIsDefault ? DLGC_DEFPUSHBUTTON : DLGC_UNDEFPUSHBUTTON);
return nCode;
} // End of OnGetDlgCode
BOOL CButtonST::PreTranslateMessage(MSG* pMsg)
{
InitToolTip();
m_ToolTip.RelayEvent(pMsg);
if (pMsg->message == WM_LBUTTONDBLCLK)
pMsg->message = WM_LBUTTONDOWN;
return CButton::PreTranslateMessage(pMsg);
} // End of PreTranslateMessage
HBRUSH CButtonST::CtlColor(CDC* /*pDC*/, UINT /*nCtlColor*/)
{
return (HBRUSH)::GetStockObject(NULL_BRUSH);
} // End of CtlColor
void CButtonST::OnSysColorChange()
{
CButton::OnSysColorChange();
m_dcBk.DeleteDC();
m_bmpBk.DeleteObject();
SetDefaultColors();
} // End of OnSysColorChange
LRESULT CButtonST::OnSetStyle(WPARAM wParam, LPARAM lParam)
{
UINT nNewType = (wParam & BS_TYPEMASK);
// Update default state flag
if (nNewType == BS_DEFPUSHBUTTON)
{
m_bIsDefault = TRUE;
} // if
else if (nNewType == BS_PUSHBUTTON)
{
// Losing default state always allowed
m_bIsDefault = FALSE;
} // if
// Can't change control type after owner-draw is set.
// Let the system process changes to other style bits
// and redrawing, while keeping owner-draw style
return DefWindowProc(BM_SETSTYLE,
(wParam & ~BS_TYPEMASK) | BS_OWNERDRAW, lParam);
} // End of OnSetStyle
LRESULT CButtonST::OnSetCheck(WPARAM wParam, LPARAM /*lParam*/)
{
ASSERT(m_bIsCheckBox);
switch (wParam)
{
case BST_CHECKED:
case BST_INDETERMINATE: // Indeterminate state is handled like checked state
SetCheck(1);
break;
default:
SetCheck(0);
break;
} // switch
return 0;
} // End of OnSetCheck
LRESULT CButtonST::OnGetCheck(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
ASSERT(m_bIsCheckBox);
return GetCheck();
} // End of OnGetCheck
#ifdef BTNST_USE_BCMENU
LRESULT CButtonST::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu)
{
LRESULT lResult;
if (BCMenu::IsMenu(pMenu))
lResult = BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
else
lResult = CButton::OnMenuChar(nChar, nFlags, pMenu);
return lResult;
} // End of OnMenuChar
#endif
#ifdef BTNST_USE_BCMENU
void CButtonST::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
BOOL bSetFlag = FALSE;
if (lpMeasureItemStruct->CtlType == ODT_MENU)
{
if (IsMenu((HMENU)lpMeasureItemStruct->itemID) && BCMenu::IsMenu((HMENU)lpMeasureItemStruct->itemID))
{
m_menuPopup.MeasureItem(lpMeasureItemStruct);
bSetFlag = TRUE;
} // if
} // if
if (!bSetFlag) CButton::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
} // End of OnMeasureItem
#endif
void CButtonST::OnEnable(BOOL bEnable)
{
CButton::OnEnable(bEnable);
if (bEnable == FALSE)
{
CWnd* pWnd = GetParent()->GetNextDlgTabItem(this);
if (pWnd)
pWnd->SetFocus();
else
GetParent()->SetFocus();
CancelHover();
} // if
} // End of OnEnable
void CButtonST::OnKillFocus(CWnd * pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
CancelHover();
} // End of OnKillFocus
void CButtonST::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CButton::OnActivate(nState, pWndOther, bMinimized);
if (nState == WA_INACTIVE && !marked) CancelHover();
} // End of OnActivate
void CButtonST::OnCancelMode()
{
CButton::OnCancelMode();
CancelHover();
} // End of OnCancelMode
BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// If a cursor was specified then use it!
if (m_hCursor != NULL)
{
::SetCursor(m_hCursor);
return TRUE;
} // if
return CButton::OnSetCursor(pWnd, nHitTest, message);
} // End of OnSetCursor
void CButtonST::CancelHover()
{
// Only for flat buttons
if (m_bIsFlat)
{
if (m_bMouseOnButton)
{
m_bMouseOnButton = FALSE;
Invalidate();
} // if
} // if
} // End of CancelHover
void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* wndUnderMouse = NULL;
CWnd* wndActive = this;
TRACKMOUSEEVENT csTME;
CButton::OnMouseMove(nFlags, point);
ClientToScreen(&point);
wndUnderMouse = WindowFromPoint(point);
// If the mouse enter the button with the left button pressed then do nothing
if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
// If our button is not flat then do nothing
if (m_bIsFlat == FALSE) return;
if (m_bAlwaysTrack == FALSE) wndActive = GetActiveWindow();
if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
{
if (!m_bMouseOnButton)
{
m_bMouseOnButton = TRUE;
Invalidate();
#ifdef BTNST_USE_SOUND
// Play sound ?
if (m_csSounds[0].lpszSound)
::PlaySound(m_csSounds[0].lpszSound, m_csSounds[0].hMod, m_csSounds[0].dwFlags);
#endif
csTME.cbSize = sizeof(csTME);
csTME.dwFlags = TME_LEAVE;
csTME.hwndTrack = m_hWnd;
::_TrackMouseEvent(&csTME);
} // if
} else CancelHover();
} // End of OnMouseMove
// Handler for WM_MOUSELEAVE
LRESULT CButtonST::OnMouseLeave(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
CancelHover();
return 0;
} // End of OnMouseLeave
BOOL CButtonST::OnClicked()
{
SetFocus();
#ifdef BTNST_USE_SOUND
// Play sound ?
if (m_csSounds[1].lpszSound)
::PlaySound(m_csSounds[1].lpszSound, m_csSounds[1].hMod, m_csSounds[1].dwFlags);
#endif
if (m_bIsCheckBox)
{
m_nCheck = !m_nCheck;
Invalidate();
} // if
else
{
// Handle the menu (if any)
#ifdef BTNST_USE_BCMENU
if (m_menuPopup.m_hMenu)
#else
if (m_hMenu)
#endif
{
CRect rWnd;
GetWindowRect(rWnd);
m_bMenuDisplayed = TRUE;
Invalidate();
#ifdef BTNST_USE_BCMENU
BCMenu* psub = (BCMenu*)m_menuPopup.GetSubMenu(0);
if (m_csCallbacks.hWnd) ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)psub, m_csCallbacks.lParam);
DWORD dwRetValue = psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, this, NULL);
#else
HMENU hSubMenu = ::GetSubMenu(m_hMenu, 0);
if (m_csCallbacks.hWnd) ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)hSubMenu, m_csCallbacks.lParam);
DWORD dwRetValue = ::TrackPopupMenuEx(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);
#endif
m_bMenuDisplayed = FALSE;
Invalidate();
if (dwRetValue)
::PostMessage(m_hParentWndMenu, WM_COMMAND, MAKEWPARAM(dwRetValue, 0), (LPARAM)NULL);
} // if
else
{
// Handle the URL (if any)
if (_tcslen(m_szURL) > 0)
{
SHELLEXECUTEINFO csSEI;
memset(&csSEI, 0, sizeof(csSEI));
csSEI.cbSize = sizeof(SHELLEXECUTEINFO);
csSEI.fMask = SEE_MASK_FLAG_NO_UI;
csSEI.lpVerb = _T("open");
csSEI.lpFile = m_szURL;
csSEI.nShow = SW_SHOWMAXIMIZED;
::ShellExecuteEx(&csSEI);
} // if
} // else
} // else
return FALSE;
} // End of OnClicked
void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CPen* pOldPen;
// Checkbox?
if (m_bIsCheckBox)
{
m_bIsPressed = ((lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0) || marked );
} // if
else // Normal button OR other button style ...
{
m_bIsPressed = ((lpDIS->itemState & ODS_SELECTED) || marked);
// If there is a menu and it's displayed, draw the button as pressed
if (
#ifdef BTNST_USE_BCMENU
m_menuPopup.m_hMenu
#else
m_hMenu
#endif
&& m_bMenuDisplayed) m_bIsPressed = TRUE;
} // else
m_bIsFocused = (lpDIS->itemState & ODS_FOCUS);
m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
CRect itemRect = lpDIS->rcItem;
pDC->SetBkMode(TRANSPARENT);
if (m_bIsFlat == FALSE)
{
if (m_bIsFocused || m_bIsDefault)
{
CBrush br(RGB(0,0,0));
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
} // if
} // if
// Prepare draw... paint button background
// Draw transparent?
if (m_bDrawTransparent)
PaintBk(pDC);
else
OnDrawBackground(pDC, &itemRect);
// Draw pressed button
if (m_bIsPressed)
{
if (m_bIsFlat)
{
if (m_bDrawBorder)
OnDrawBorder(pDC, &itemRect);
}
else
{
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
}
}
else // ...else draw non pressed button
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
if (m_bIsFlat)
{
if (m_bMouseOnButton && m_bDrawBorder)
OnDrawBorder(pDC, &itemRect);
}
else
{
// Draw top-left borders
// White line
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Light gray line
pDC->SelectObject(pen3DLight);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
pDC->LineTo(itemRect.left+1, itemRect.top+1);
pDC->LineTo(itemRect.right, itemRect.top+1);
// Draw bottom-right borders
// Black line
pDC->SelectObject(pen3DDKShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
// Dark gray line
pDC->SelectObject(penBtnShadow);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.top);
//
pDC->SelectObject(pOldPen);
} // else
} // else
// Read the button's title
CString sTitle;
GetWindowText(sTitle);
CRect captionRect = lpDIS->rcItem;
// Draw the icon
if (m_csIcons[0].hIcon)
{
DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
} // if
if (m_csBitmaps[0].hBitmap)
{
pDC->SetBkColor(RGB(255,255,255));
DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
} // if
// Write the button title (if any)
if (sTitle.IsEmpty() == FALSE)
{
DrawTheText(pDC, (LPCTSTR)sTitle, &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
} // if
if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
{
// Draw the focus rect
if (m_bIsFocused)
{
CRect focusRect = itemRect;
if (!m_bIsFlat)
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
} // if
} // if
} // End of DrawItem
void CButtonST::PaintBk(CDC* pDC)
{
CClientDC clDC(GetParent());
CRect rect;
CRect rect1;
GetClientRect(rect);
GetWindowRect(rect1);
GetParent()->ScreenToClient(rect1);
if (m_dcBk.m_hDC == NULL)
{
m_dcBk.CreateCompatibleDC(&clDC);
m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
} // if
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
} // End of PaintBk
HBITMAP CButtonST::CreateBitmapMask(HBITMAP hSourceBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTransColor)
{
HBITMAP hMask = NULL;
HDC hdcSrc = NULL;
HDC hdcDest = NULL;
HBITMAP hbmSrcT = NULL;
HBITMAP hbmDestT = NULL;
COLORREF crSaveBk;
COLORREF crSaveDestText;
hMask = ::CreateBitmap(dwWidth, dwHeight, 1, 1, NULL);
if (hMask == NULL) return NULL;
hdcSrc = ::CreateCompatibleDC(NULL);
hdcDest = ::CreateCompatibleDC(NULL);
hbmSrcT = (HBITMAP)::SelectObject(hdcSrc, hSourceBitmap);
hbmDestT = (HBITMAP)::SelectObject(hdcDest, hMask);
crSaveBk = ::SetBkColor(hdcSrc, crTransColor);
::BitBlt(hdcDest, 0, 0, dwWidth, dwHeight, hdcSrc, 0, 0, SRCCOPY);
crSaveDestText = ::SetTextColor(hdcSrc, RGB(255, 255, 255));
::SetBkColor(hdcSrc,RGB(0, 0, 0));
::BitBlt(hdcSrc, 0, 0, dwWidth, dwHeight, hdcDest, 0, 0, SRCAND);
SetTextColor(hdcDest, crSaveDestText);
::SetBkColor(hdcSrc, crSaveBk);
::SelectObject(hdcSrc, hbmSrcT);
::SelectObject(hdcDest, hbmDestT);
::DeleteDC(hdcSrc);
::DeleteDC(hdcDest);
return hMask;
} // End of CreateBitmapMask
//
// Parameters:
// [IN] bHasTitle
// TRUE if the button has a text
// [IN] rpItem
// A pointer to a RECT structure indicating the allowed paint area
// [IN/OUT]rpTitle
// A pointer to a CRect object indicating the paint area reserved for the
// text. This structure will be modified if necessary.
// [IN] bIsPressed
// TRUE if the button is currently pressed
// [IN] dwWidth
// Width of the image (icon or bitmap)
// [IN] dwHeight
// Height of the image (icon or bitmap)
// [OUT] rpImage
// A pointer to a CRect object that will receive the area available to the image
//
void CButtonST::PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage)
{
CRect rBtn;
rpImage->CopyRect(rpItem);
switch (m_byAlign)
{
case ST_ALIGN_HORIZ:
if (bHasTitle == FALSE)
{
// Center image horizontally
rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
}
else
{
// Image must be placed just inside the focus rect
rpImage->left += m_ptImageOrg.x;
rpTitle->left += dwWidth + m_ptImageOrg.x;
}
// Center image vertically
rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
break;
case ST_ALIGN_HORIZ_RIGHT:
GetClientRect(&rBtn);
if (bHasTitle == FALSE)
{
// Center image horizontally
rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
}
else
{
// Image must be placed just inside the focus rect
rpTitle->right = rpTitle->Width() - dwWidth - m_ptImageOrg.x;
rpTitle->left = m_ptImageOrg.x;
rpImage->left = rBtn.right - dwWidth - m_ptImageOrg.x;
// Center image vertically
rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
}
break;
case ST_ALIGN_VERT:
// Center image horizontally
rpImage->left += ((rpImage->Width() - (long)dwWidth)/2);
if (bHasTitle == FALSE)
{
// Center image vertically
rpImage->top += ((rpImage->Height() - (long)dwHeight)/2);
}
else
{
rpImage->top = m_ptImageOrg.y;
rpTitle->top += dwHeight;
}
break;
case ST_ALIGN_OVERLAP:
break;
} // switch
// If button is pressed then press image also
if (bIsPressed && m_bIsCheckBox == FALSE)
rpImage->OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
} // End of PrepareImageRect
void CButtonST::DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
{
BYTE byIndex = 0;
// Select the icon to use
if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
byIndex = 0;
else
byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
CRect rImage;
PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);
// Ole'!
pDC->DrawState( rImage.TopLeft(),
rImage.Size(),
m_csIcons[byIndex].hIcon,
(bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);
} // End of DrawTheIcon
void CButtonST::DrawTheBitmap(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpCaption, BOOL bIsPressed, BOOL bIsDisabled)
{
HDC hdcBmpMem = NULL;
HBITMAP hbmOldBmp = NULL;
HDC hdcMem = NULL;
HBITMAP hbmT = NULL;
BYTE byIndex = 0;
// Select the bitmap to use
if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
byIndex = 0;
else
byIndex = (m_csBitmaps[1].hBitmap == NULL ? 0 : 1);
CRect rImage;
PrepareImageRect(bHasTitle, rpItem, rpCaption, bIsPressed, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, &rImage);
hdcBmpMem = ::CreateCompatibleDC(pDC->m_hDC);
hbmOldBmp = (HBITMAP)::SelectObject(hdcBmpMem, m_csBitmaps[byIndex].hBitmap);
hdcMem = ::CreateCompatibleDC(NULL);
hbmT = (HBITMAP)::SelectObject(hdcMem, m_csBitmaps[byIndex].hMask);
if (bIsDisabled && m_bShowDisabledBitmap)
{
HDC hDC = NULL;
HBITMAP hBitmap = NULL;
hDC = ::CreateCompatibleDC(pDC->m_hDC);
hBitmap = ::CreateCompatibleBitmap(pDC->m_hDC, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight);
HBITMAP hOldBmp2 = (HBITMAP)::SelectObject(hDC, hBitmap);
RECT rRect;
rRect.left = 0;
rRect.top = 0;
rRect.right = rImage.right + 1;
rRect.bottom = rImage.bottom + 1;
::FillRect(hDC, &rRect, (HBRUSH)RGB(255, 255, 255));
COLORREF crOldColor = ::SetBkColor(hDC, RGB(255,255,255));
::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
::BitBlt(hDC, 0, 0, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
::SetBkColor(hDC, crOldColor);
::SelectObject(hDC, hOldBmp2);
::DeleteDC(hDC);
pDC->DrawState( CPoint(rImage.left/*+1*/, rImage.top),
CSize(m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight),
hBitmap, DST_BITMAP | DSS_DISABLED);
VERIFY( ::DeleteObject(hBitmap) );
} // if
else
{
::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcMem, 0, 0, SRCAND);
::BitBlt(pDC->m_hDC, rImage.left, rImage.top, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, hdcBmpMem, 0, 0, SRCPAINT);
} // else
::SelectObject(hdcMem, hbmT);
::DeleteDC(hdcMem);
::SelectObject(hdcBmpMem, hbmOldBmp);
::DeleteDC(hdcBmpMem);
} // End of DrawTheBitmap
void CButtonST::DrawTheText(CDC* pDC, LPCTSTR lpszText, RECT* /*rpItem*/, CRect* rpCaption, BOOL /*bIsPressed*/, BOOL /*bIsDisabled*/)
{
// Draw the button's title
// If button is pressed then "press" title also
if (m_bIsPressed && m_bIsCheckBox == FALSE)
rpCaption->OffsetRect(m_ptPressedOffset.x, m_ptPressedOffset.y);
// ONLY FOR DEBUG
//CBrush brBtnShadow(RGB(255, 0, 0));
//pDC->FrameRect(rCaption, &brBtnShadow);
// Center text
CRect centerRect = rpCaption;
pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
if (!leftAligned) rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())/2, (centerRect.Height() - rpCaption->Height())/2);
else rpCaption->OffsetRect( 8, (centerRect.Height() - rpCaption->Height())/2);
/* RFU
rpCaption->OffsetRect(0, (centerRect.Height() - rpCaption->Height())/2);
rpCaption->OffsetRect((centerRect.Width() - rpCaption->Width())-4, (centerRect.Height() - rpCaption->Height())/2);
*/
pDC->SetBkMode(TRANSPARENT);
/*
pDC->DrawState(rCaption->TopLeft(), rCaption->Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
*/
if (m_bIsDisabled)
{
rpCaption->OffsetRect(1, 1);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
rpCaption->OffsetRect(-1, -1);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
} // if
else
{
if (m_bMouseOnButton || m_bIsPressed)
{
pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
} // if
else
{
pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
} // else
pDC->DrawText(lpszText, -1, rpCaption, DT_WORDBREAK | DT_CENTER);
} // if
} // End of DrawTheText
// This function creates a grayscale icon starting from a given icon.
// The resulting icon will have the same size of the original one.
//
// Parameters:
// [IN] hIcon
// Handle to the original icon.
//
// Return value:
// If the function succeeds, the return value is the handle to the newly created
// grayscale icon.
// If the function fails, the return value is NULL.
//
// Updates:
// 03/May/2002 Removed dependancy from m_hWnd
// Removed 1 BitBlt operation
//
HICON CButtonST::CreateGrayscaleIcon(HICON hIcon)
{
HICON hGrayIcon = NULL;
HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
BITMAP bmp;
HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
ICONINFO csII, csGrayII;
BOOL bRetValue = FALSE;
bRetValue = ::GetIconInfo(hIcon, &csII);
if (bRetValue == FALSE) return NULL;
hMainDC = ::GetDC(HWND_DESKTOP);
hMemDC1 = ::CreateCompatibleDC(hMainDC);
hMemDC2 = ::CreateCompatibleDC(hMainDC);
if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
{
DWORD dwWidth = csII.xHotspot*2;
DWORD dwHeight = csII.yHotspot*2;
csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
if (csGrayII.hbmColor)
{
hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
//::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
DWORD dwLoopY = 0, dwLoopX = 0;
COLORREF crPixel = 0;
BYTE byNewPixel = 0;
for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
{
for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
{
crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
if (crPixel) ::SetPixel(hMemDC2, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
} // for
} // for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
csGrayII.hbmMask = csII.hbmMask;
csGrayII.fIcon = TRUE;
hGrayIcon = ::CreateIconIndirect(&csGrayII);
} // if