WTL Controls
Back to the WTL Controls Home Page.
ColorBlob.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 //--------------------------------------------------------------------------------------
3 //--------------------------------------------------------------------------------------
4 //
5 //
6 // WTL Control Library
7 // by
8 // Bright Ideas Software®
9 // Copyright © 2018 by Bright Ideas Software.
10 //
11 //
12 //
13 //--------------------------------------------------------------------------------------
14 //--------------------------------------------------------------------------------------
15 
16 
17 namespace BIS_WTL_CONTROLS
18 {
19  //----------------------------------------------------------------------------------------------
21  {
22  m_hMemDC = NULL;
23  m_hOldMap = NULL;
24  m_hbrBackBrush = NULL;
25 
26  m_bDirtyColor = true;
27  m_bUpdateContent = true;
28 
29  m_clrBackColor = CLR_RED;
30  m_EdgeStyle = CEdgeStyle::esLine;
31  }
32  //----------------------------------------------------------------------------------------------
34  {
35  ATLASSERT( m_hWnd == NULL);
36 
37  DestroyGDIStuff();
38 
39  if (m_hbrBackBrush != NULL)
40  {
41  ::DeleteObject (m_hbrBackBrush);
42  m_hbrBackBrush = NULL;
43  }
44  }
45  //----------------------------------------------------------------------------------------------
46  //----------------------------------------------------------------------------------------------
58  {
59 
60  CWindowImpl<CColorBlob, CStatic>::SubclassWindow(hWnd);
61  UpdateBackBrush();
62 
63  }
64  //----------------------------------------------------------------------------------------------
71  {
72 
73  DestroyGDIStuff();
74 
75  if (m_hbrBackBrush != NULL)
76  {
77  ::DeleteObject (m_hbrBackBrush);
78  m_hbrBackBrush = NULL;
79  }
80 
81  UnsubclassWindow();
82  }
83  //----------------------------------------------------------------------------------------------
84  //----------------------------------------------------------------------------------------------
93  COLORREF CColorBlob::GetBackColor() const
94  {
95  return(m_clrBackColor);
96  }
97  //----------------------------------------------------------------------------------------------
107  void CColorBlob::SetBackColor(COLORREF value)
108  {
109  m_bDirtyColor = true;
110  m_clrBackColor = value;
111  }
112  //----------------------------------------------------------------------------------------------
113  //----------------------------------------------------------------------------------------------
122  {
123  return(m_EdgeStyle);
124  }
125  //----------------------------------------------------------------------------------------------
136  {
137  if (m_EdgeStyle != style)
138  {
139  m_EdgeStyle = style;
140  m_bUpdateContent = true;
141  }
142  }
143  //----------------------------------------------------------------------------------------------
144  //----------------------------------------------------------------------------------------------
151  {
152  m_bUpdateContent = true;
153  Invalidate();
154  UpdateWindow();
155  }
156  //----------------------------------------------------------------------------------------------
157  //----------------------------------------------------------------------------------------------
158  //
159  // Private Methods
160  //
161  //----------------------------------------------------------------------------------------------
162  //----------------------------------------------------------------------------------------------
163  void CColorBlob::UpdateBackBrush()
164  {
165  m_bDirtyColor = false;
166 
167  if (m_hbrBackBrush != NULL)
168  {
169  ::DeleteObject (m_hbrBackBrush);
170  m_hbrBackBrush = NULL;
171  }
172 
173  m_hbrBackBrush = ::CreateSolidBrush (m_clrBackColor);
174 
175  m_bUpdateContent = true;
176  }
177  //----------------------------------------------------------------------------------------------
178  void CColorBlob::PrepareGDIStuff()
179  {
180  DestroyGDIStuff();
181 
182  CRect r;
183  GetClientRect(&r);
184 
185  if (::IsWindow(m_hWnd))
186  {
187  if ((r.Width() > 0)&&(r.Height() > 0))
188  {
189  m_hCanvas.Create(r.Width(), r.Height(), 24, 0);
190 
191  if (! m_hCanvas.IsNull())
192  {
193 
194  HDC dc = ::GetDC(m_hWnd);
195  m_hMemDC = ::CreateCompatibleDC(dc);
196 
197  if (m_hMemDC != NULL)
198  m_hOldMap = (HBITMAP)::SelectObject(m_hMemDC, (HBITMAP)m_hCanvas);
199  else
200  m_hCanvas.Destroy();;
201 
202  ::ReleaseDC(m_hWnd, dc);
203  m_bUpdateContent = true;
204  }
205  }
206  }
207  }
208  //----------------------------------------------------------------------------------------------
209  void CColorBlob::DestroyGDIStuff()
210  {
211 
212  if (m_hMemDC != NULL)
213  {
214  if (m_hCanvas != NULL)
215  {
216  ::SelectObject(m_hMemDC, m_hOldMap);
217  m_hOldMap = NULL;
218 
219  m_hCanvas.Destroy();
220  }
221  ::DeleteDC(m_hMemDC);
222  m_hMemDC = NULL;
223  }
224 
225  }
226  //----------------------------------------------------------------------------------------------
227  void CColorBlob::DrawEdge(CRect r)
228  {
229 
230  switch (m_EdgeStyle)
231  {
232  case CEdgeStyle::esNone:
233  break;
234  case CEdgeStyle::esLine:
235  OutlineRect(m_hMemDC, r);
236  break;
237  case CEdgeStyle::esBump:
238  ::DrawEdge(m_hMemDC, r, EDGE_BUMP,BF_RECT );
239  break;
241  ::DrawEdge(m_hMemDC, r, EDGE_ETCHED,BF_RECT );
242  break;
244  ::DrawEdge(m_hMemDC, r, EDGE_RAISED,BF_RECT );
245  break;
247  ::DrawEdge(m_hMemDC, r, EDGE_SUNKEN,BF_RECT);
248  break;
250  ::DrawEdge(m_hMemDC, r, BDR_RAISEDOUTER,BF_RECT );
251  break;
253  ::DrawEdge(m_hMemDC, r, BDR_SUNKENOUTER,BF_RECT);
254  break;
255  }
256  }
257  //----------------------------------------------------------------------------------------------
258  //----------------------------------------------------------------------------------------------
259  //
260  // Message Handlers
261  //
262  //----------------------------------------------------------------------------------------------
263  //----------------------------------------------------------------------------------------------
264  BOOL CColorBlob::OnEraseBkgnd(CDCHandle dc)
265  {
266  return(TRUE);
267  }
268  //----------------------------------------------------------------------------------------------
269  LRESULT CColorBlob::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
270  {
271  CRect rect;
272 
273  HDC dc = NULL;
274  bool do_end_paint = false;
275  PAINTSTRUCT ps;
276 
277  if (wParam != NULL)
278  dc = (HDC)wParam;
279  else
280  {
281  RECT update_rect{ 0 };
282  if (GetUpdateRect(&update_rect) != 0)
283  {
284  dc = BeginPaint(&ps);
285  do_end_paint = true;
286  }
287  }
288 
289  if (dc == NULL)
290  return(0);
291 
292  if (m_bDirtyColor == true)
293  UpdateBackBrush();
294 
295  GetClientRect(&rect);
296 
297 
298  if ((m_hMemDC == NULL)||(m_hCanvas.IsNull()))
299  PrepareGDIStuff();
300 
301  if ((m_hMemDC != NULL)&&( ! m_hCanvas.IsNull()))
302  {
303 
304  if ((rect.Width() != m_hCanvas.GetWidth())||(rect.Height() != m_hCanvas.GetHeight()))
305  PrepareGDIStuff();
306 
307  if (m_bUpdateContent)
308  {
309  m_bUpdateContent = false;
310 
311  ::FillRect(m_hMemDC, rect, m_hbrBackBrush);
312  ::SetBkColor(m_hMemDC, m_clrBackColor);
313 
314  DrawEdge(rect);
315  }
316 
317  ::BitBlt (dc, 0, 0,rect.Width (), rect.Height (), m_hMemDC, 0, 0, SRCCOPY);
318 
319  }
320 
321  if (do_end_paint == true)
322  EndPaint(&ps);
323 
324  bHandled = TRUE;
325 
326  return(0);
327  }
328  //----------------------------------------------------------------------------------------------
329 };
void SetEdgeStyle(CEdgeStyle style)
Specify a new edge style. A call to UpdateContent may be required to make the new color visible...
Definition: ColorBlob.cpp:135
CEdgeStyle
Style of edge defining control boundaries.
Definition: WTL_Controls.h:37
BOOL OutlineRect(HDC HDestDC, const RECT &RDest)
Draw a rectangular outline with the current pen.
#define CLR_RED
Definition: WTL_Controls.h:136
void SubclassStatic(HWND hWnd)
Subclass a control window and create the colored background brush.
Definition: ColorBlob.cpp:57
void UpdateContent()
Wraps calls to Invalidate and UpdateWindow, updating the back buffer and blitting the new content to ...
Definition: ColorBlob.cpp:150
void UnSubclassStatic()
Un-subclass the window and release GDI resources.
Definition: ColorBlob.cpp:70
COLORREF GetBackColor() const
Default: COLOR_RED.
Definition: ColorBlob.cpp:93
void SetBackColor(COLORREF value)
Specify a new background color. A call to UpdateContent may be required to make the new color visible...
Definition: ColorBlob.cpp:107
CEdgeStyle GetEdgeStyle() const
Default: CEdgeStyle::esLine.
Definition: ColorBlob.cpp:121