1 #include "studiox_includes.h"
2
3 extern CFont NormalFont;
4
BEGIN_MESSAGE_MAP(color_box_button,CButton)5 BEGIN_MESSAGE_MAP(color_box_button, CButton)
6 ON_WM_PAINT()
7 ON_WM_ENABLE()
8 ON_WM_MOUSELEAVE()
9 ON_WM_MOUSEMOVE()
10 END_MESSAGE_MAP()
11
12 ///////////////////////////////////////////////////////////////////////////////
13 color_box_button::color_box_button()
14 {
15 m_boxcolor = 0;
16 m_hover = FALSE;
17 }
18
19 ///////////////////////////////////////////////////////////////////////////////
~color_box_button()20 color_box_button::~color_box_button()
21 {
22 }
23
24 ///////////////////////////////////////////////////////////////////////////////
SetBoxColor(COLORREF color)25 void color_box_button::SetBoxColor(COLORREF color)
26 {
27 m_boxcolor = color;
28 }
29
30 ///////////////////////////////////////////////////////////////////////////////
OnPaint()31 void color_box_button::OnPaint()
32 {
33 CPaintDC dc(this);
34
35 COLORREF color_border = GetSysColor(COLOR_ACTIVEBORDER);
36 COLORREF color_highlight = GetSysColor(COLOR_HIGHLIGHT);
37
38 CRect rect;
39 CString text;
40 RectF rectF;
41 int state;
42
43 GetClientRect(&rect);
44
45 CDC dcMem;
46 HBITMAP hbmMem;
47 HANDLE hOld;
48
49 /* Flush canvas memory to off-screen DC. */
50 dcMem.CreateCompatibleDC(&dc);
51 hbmMem = CreateCompatibleBitmap(dc.GetSafeHdc(), rect.Width(), rect.Height());
52 hOld = SelectObject(dcMem, hbmMem);
53
54 Graphics g(dcMem);
55
56 dc.SelectObject(&NormalFont);
57 Gdiplus::Font font(dc.GetSafeHdc());
58 StringFormat stringFormat;
59
60 // Center-justify each line of text.
61 stringFormat.SetAlignment(StringAlignmentCenter);
62
63 // Center the block of text (top to bottom) in the rectangle.
64 stringFormat.SetLineAlignment(StringAlignmentCenter);
65
66
67 rectF.X = (REAL)rect.left;
68 rectF.Y = (REAL)rect.top;
69 rectF.Width = (REAL)rect.Width() - 1;
70 rectF.Height = (REAL)rect.Height() - 1;
71
72 GetWindowText(text);
73
74 state = GetState();
75 if (state & BST_PUSHED)
76 {
77 SolidBrush selectBrush(Color(20, GetRValue(color_highlight), GetGValue(color_highlight), GetBValue(color_highlight)));
78 Pen selectPen(Color(128, GetRValue(color_highlight), GetGValue(color_highlight), GetBValue(color_highlight)));
79
80 g.FillRectangle(&selectBrush, rectF);
81 g.DrawRectangle(&selectPen, rectF);
82 }
83 else if ((state & BST_FOCUS) || m_hover)
84 {
85 SolidBrush selectBrush(Color(20, GetRValue(color_highlight), GetGValue(color_highlight), GetBValue(color_highlight)));
86 Pen selectPen(Color(GetRValue(color_highlight), GetGValue(color_highlight), GetBValue(color_highlight)));
87
88 g.FillRectangle(&selectBrush, rectF);
89 g.DrawRectangle(&selectPen, rectF);
90 }
91 else
92 {
93 SolidBrush normalBrush(Color(20, GetRValue(color_border), GetGValue(color_border), GetBValue(color_border)));
94 Pen normalPen(Color(GetRValue(color_border), GetGValue(color_border), GetBValue(color_border)));
95
96 g.FillRectangle(&normalBrush, rectF);
97 g.DrawRectangle(&normalPen, rectF);
98 }
99
100 // Draw color box
101 rectF.Inflate(-1, -1);
102 SolidBrush fill_brush(Color(GetRValue(m_boxcolor), GetGValue(m_boxcolor), GetBValue(m_boxcolor)));
103 Pen line_pen(Color(0, 0, 0));
104 g.FillRectangle(&fill_brush, rectF);
105 g.DrawRectangle(&line_pen, rectF);
106
107 /* Transfer the off-screen DC to the screen. */
108 dc.BitBlt(0, 0, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY);
109
110 /* Free-up the off-screen DC. */
111 SelectObject(dcMem, hOld);
112 DeleteObject(hbmMem);
113 DeleteDC(dcMem);
114 }
115
116 ///////////////////////////////////////////////////////////////////////////////
OnEnable(BOOL bEnable)117 void color_box_button::OnEnable(BOOL bEnable)
118 {
119 CButton::OnEnable(bEnable);
120
121 Invalidate();
122 UpdateWindow();
123 }
124
125 ///////////////////////////////////////////////////////////////////////////////
OnMouseLeave()126 void color_box_button::OnMouseLeave()
127 {
128 m_hover = FALSE;
129 Invalidate();
130
131 CButton::OnMouseLeave();
132 }
133
134 ///////////////////////////////////////////////////////////////////////////////
OnMouseMove(UINT nFlags,CPoint point)135 void color_box_button::OnMouseMove(UINT nFlags, CPoint point)
136 {
137 if (!m_hover)
138 {
139 m_hover = TRUE;
140 }
141 CButton::OnMouseMove(nFlags, point);
142 }
143