WTL Controls
Back to the WTL Controls Home Page.
ComboBoxPlus.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 
20  //----------------------------------------------------------------------------------------------
22  {
23 
24  m_bAutoAdjustDropWidth = true;
25  m_iMaxDropDownWidth = 400;
26 
27  m_ListBoxBackColor = GetSysColor(COLOR_WINDOW);
28  m_ControlBackColor = m_ListBoxBackColor;
29  m_ControlFocusedBackColor = m_ListBoxBackColor;
30 
31  m_ListBoxTextColor = ::GetSysColor(COLOR_WINDOWTEXT);
32  m_ControlTextColor = m_ListBoxTextColor;
33  m_ControlFocusedTextColor = CLR_RED;
34 
35  m_ControlBackBrush = NULL;
36  m_ListBoxBackBrush = NULL;
37  m_ControlFocusedBackBrush = NULL;
38 
39  m_wndControl = NULL;
40  m_wndListBox = NULL;
41 
42  m_bReadOnly = false;
43 
44  }
45  //----------------------------------------------------------------------------------------------
47  {
48 
49  ATLASSERT(m_hWnd == NULL);
50  DeleteBrushes();
51 
52  }
53  //----------------------------------------------------------------------------------------------
54  //----------------------------------------------------------------------------------------------
68  {
69 
70  CWindowImpl<CComboBoxPlus, CComboBox>::SubclassWindow(hWnd);
71 
72  COMBOBOXINFO info{ 0 };
73  int size = sizeof(COMBOBOXINFO);
74  info.cbSize = size;
75 
76  GetComboBoxInfo(&info);
77 
78  m_wndControl = info.hwndItem;
79  m_wndListBox = info.hwndList;
80 
81  if (::IsWindow(m_wndListBox))
82  m_ListBox.Attach(m_wndListBox);
83 
84  if (::IsWindow(m_wndControl))
85  m_ControlWnd.Attach(m_wndControl);
86 
87  UpdateBrushes();
88 
89  }
90  //----------------------------------------------------------------------------------------------
97  {
98  if (m_ListBox.IsWindow())
99  m_ListBox.Detach();
100 
101  if (m_ControlWnd.IsWindow())
102  m_ControlWnd.Detach();
103 
104  UnsubclassWindow();
105 
106  DeleteBrushes();
107 
108  }
109  //----------------------------------------------------------------------------------------------
110  //----------------------------------------------------------------------------------------------
125  {
126 
127  if (IsWindow())
128  {
129  if (m_bAutoAdjustDropWidth == true)
130  {
131 
132  int max = 0;
133  HFONT font = GetFont();
134  for (int i = 0; i < GetCount(); i++)
135  {
136 
137  CString item;
138 
139  int cchLen = GetLBTextLen(i);
140 
141  LPTSTR lpstr = item.GetBufferSetLength(cchLen);
142  if(lpstr != NULL)
143  GetLBText(i, lpstr);
144 
145  item.ReleaseBuffer();
146  int w = GetTextLength( item, font);
147 
148  if (w > max)
149  max = w;
150  }
151 
152  if (max > m_iMaxDropDownWidth)
153  max = m_iMaxDropDownWidth;
154 
155  RECT window_rect;
156  m_ControlWnd.GetWindowRect(&window_rect);
157  int w_w = window_rect.right - window_rect.left;
158 
159  if (max < w_w)
160  max = w_w;
161 
162  SetDroppedWidth(max);
163  }
164  }
165  else
166  {
167  RECT window_rect;
168  m_ControlWnd.GetWindowRect(&window_rect);
169  SetDroppedWidth(window_rect.right - window_rect.left);
170  }
171  }
172  //----------------------------------------------------------------------------------------------
173  //----------------------------------------------------------------------------------------------
185  {
186  return(m_bAutoAdjustDropWidth);
187  }
188  //----------------------------------------------------------------------------------------------
200  {
201  m_bAutoAdjustDropWidth = value;
203  }
204  //----------------------------------------------------------------------------------------------
205  //----------------------------------------------------------------------------------------------
215  {
216  return(m_iMaxDropDownWidth);
217  }
218  //----------------------------------------------------------------------------------------------
229  {
230  m_iMaxDropDownWidth = value;
232  }
233  //----------------------------------------------------------------------------------------------
234  //----------------------------------------------------------------------------------------------
244  {
245  return(m_ControlBackColor);
246  }
247  //----------------------------------------------------------------------------------------------
258  {
259  if (m_ControlBackColor != value)
260  {
261  m_ControlBackColor = value;
262  UpdateBrushes();
263  }
264  }
265  //----------------------------------------------------------------------------------------------
266  //----------------------------------------------------------------------------------------------
276  {
277  return(m_ControlTextColor);
278  }
279  //----------------------------------------------------------------------------------------------
290  {
291  m_ControlTextColor = value;
292  }
293  //----------------------------------------------------------------------------------------------
294  //----------------------------------------------------------------------------------------------
304  {
305  return(m_ControlFocusedBackColor);
306  }
307  //----------------------------------------------------------------------------------------------
318  {
319  if (m_ControlFocusedBackColor != value)
320  {
321  m_ControlFocusedBackColor = value;
322  UpdateBrushes();
323  }
324  }
325  //----------------------------------------------------------------------------------------------
326  //----------------------------------------------------------------------------------------------
336  {
337  return(m_ControlFocusedTextColor);
338  }
339  //----------------------------------------------------------------------------------------------
351  {
352  m_ControlFocusedTextColor = value;
353  }
354  //----------------------------------------------------------------------------------------------
355  //----------------------------------------------------------------------------------------------
365  {
366  return(m_ListBoxBackColor);
367  }
368  //----------------------------------------------------------------------------------------------
379  {
380  if (m_ListBoxBackColor != value)
381  {
382  m_ListBoxBackColor = value;
383  UpdateBrushes();
384  }
385  }
386  //----------------------------------------------------------------------------------------------
387  //----------------------------------------------------------------------------------------------
397  {
398  return(m_ListBoxTextColor);
399  }
400  //----------------------------------------------------------------------------------------------
411  {
412  m_ListBoxTextColor = value;
413  }
414  //----------------------------------------------------------------------------------------------
424  {
425  return (m_bReadOnly);
426  }
427  //----------------------------------------------------------------------------------------------
440  void CComboBoxPlus::SetReadOnly(bool value)
441  {
442  if (m_bReadOnly != value)
443  {
444  m_bReadOnly = value;
445 
446 
447  DWORD combostyle = GetStyle();
448 
449  if (((combostyle & CBS_DROPDOWN) != 0) || ((combostyle & CBS_SIMPLE) != 0))
450  {
451 
452 
453  if (m_bReadOnly == true)
454  {
455  if (::IsWindow(m_wndControl) == TRUE)
456  {
457  DWORD style = m_ControlWnd.GetStyle();
458 
459  if ((style & ES_READONLY) != 0) // already read-only
460  return;
461 
462  else
463  {
464  m_ControlWnd.Detach();
465 
466  CEdit ed;
467  ed.Attach(m_wndControl);
468  ed.SetReadOnly();
469  ed.Detach();
470 
471  m_ControlWnd.Attach(m_wndControl);
472  }
473  }
474  }
475  else
476  {
477  if (::IsWindow(m_ControlWnd))
478  {
479  DWORD style = m_ControlWnd.GetStyle();
480 
481  if ((style & ES_READONLY) == 0) // already not read-only
482  return;
483 
484  else
485  {
486  m_ControlWnd.Detach();
487 
488  CEdit ed;
489  ed.Attach(m_wndControl);
490  ed.SetReadOnly(FALSE);
491  ed.Detach();
492 
493  m_ControlWnd.Attach(m_wndControl);
494 
495  }
496  }
497  }
498  }
499  }
500  }
501  //----------------------------------------------------------------------------------------------
502  //----------------------------------------------------------------------------------------------
503  //
504  // Private Methods
505  //
506  //----------------------------------------------------------------------------------------------
507  //----------------------------------------------------------------------------------------------
508  void CComboBoxPlus::UpdateBrushes()
509  {
510 
511  DeleteBrushes();
512 
513  m_ControlBackBrush = ::CreateSolidBrush(m_ControlBackColor);
514 
515  m_ListBoxBackBrush = ::CreateSolidBrush(m_ListBoxBackColor);
516 
517  m_ControlFocusedBackBrush = ::CreateSolidBrush(m_ControlFocusedBackColor);
518 
519  }
520  //----------------------------------------------------------------------------------------------
521  void CComboBoxPlus::DeleteBrushes()
522  {
523 
524  if (m_ControlBackBrush != NULL)
525  {
526  ::DeleteObject(m_ControlBackBrush);
527  m_ControlBackBrush = NULL;
528  }
529 
530  if (m_ControlFocusedBackBrush != NULL)
531  {
532  ::DeleteObject(m_ControlFocusedBackBrush);
533  m_ControlFocusedBackBrush = NULL;
534  }
535 
536  if (m_ListBoxBackBrush != NULL)
537  {
538  ::DeleteObject(m_ListBoxBackBrush);
539  m_ListBoxBackBrush = NULL;
540  }
541  }
542  //----------------------------------------------------------------------------------------------
543  //----------------------------------------------------------------------------------------------
544  //
545  // Message Handlers
546  //
547  //----------------------------------------------------------------------------------------------
548  //----------------------------------------------------------------------------------------------
549  LRESULT CComboBoxPlus::OnCtlColorControl(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
550  {
551 
552  if (((HWND)lParam) == m_ControlWnd.m_hWnd)
553  {
554  bHandled = TRUE;
555 
556  HDC dc = (HDC)wParam;
557  ::SetBkMode(dc, TRANSPARENT);
558 
559  if (GetFocus() == m_ControlWnd.m_hWnd)
560  {
561  ::SetTextColor(dc, m_ControlFocusedTextColor);
562  ::SetBkColor(dc, m_ControlFocusedBackColor);
563 
564  return((long)m_ControlFocusedBackBrush);
565  }
566  else
567  {
568  ::SetTextColor(dc, m_ControlTextColor);
569  ::SetBkColor(dc, m_ControlBackColor);
570 
571  return((long)m_ControlBackBrush);
572  }
573  }
574  else
575  {
576  bHandled = FALSE;
577  return(0);
578  }
579  }
580  //----------------------------------------------------------------------------------------------
581  HBRUSH CComboBoxPlus::OnCtlColorListBox(CDCHandle dc, CListBox listBox)
582  {
583  if (listBox.m_hWnd == m_ListBox.m_hWnd)
584  {
585  dc.SetTextColor(m_ListBoxTextColor);
586  dc.SetBkColor(m_ListBoxBackColor);
587  dc.SetBkMode(TRANSPARENT);
588 
589  return(m_ListBoxBackBrush);
590  }
591  else
592  {
593  SetMsgHandled(FALSE);
594  return(0);
595  }
596 
597  }
598  //----------------------------------------------------------------------------------------------
599 
600 
601 };
void SetMaxDropDownWidth(int value)
Sets the upper limit to the width of the dropdown list, if it is being automatically adjusted...
void SetListBoxBackColor(COLORREF value)
Set a new background color for the listbox portion of the combobox.
void SetListBoxTextColor(COLORREF value)
Set a new text color for the listbox portion of the combobox.
COLORREF GetListBoxBackColor() const
Default: COLOR_WINDOW.
void SubclassComboBox(HWND hWnd)
Subclass a combobox control and create GDI brushes.
void UnSubclassComboBox()
Un-subclass main and component windows; free GDI resources.
COLORREF GetControlFocusedBackColor() const
Default: COLOR_WINDOW.
int GetTextLength(CString str, HFONT font)
Measure the length, in pixels, of a string using a given font.
#define CLR_RED
Definition: WTL_Controls.h:136
bool GetAutoAdjustDropRectWidth() const
Default: true.
void SetAutoAdjustDropRectWidth(bool value)
Change the AutoAdjustRectWidth state.
COLORREF GetControlBackColor() const
Default: COLOR_WINDOW.
int GetMaxDropDownWidth() const
Default: 400 pixels.
void SetControlFocusedTextColor(COLORREF value)
Set a new text color for the control portion of the combobox when it has focus.
void UpdateDropDownWidth()
Force an update of the width of the drop down list.
bool GetReadOnly() const
Default: false.
COLORREF GetControlFocusedTextColor() const
Default: CLR_RED.
void SetControlFocusedBackColor(COLORREF value)
Set a new background color for the control portion of the combobox when it has focus.
COLORREF GetControlTextColor() const
Default: COLOR_WINDOWTEXT.
void SetControlTextColor(COLORREF value)
Set a new text color for the control portion of the combobox when it doesn&#39;t have focus...
void SetReadOnly(bool read_only=true)
Change the read-only state of the combobox.
COLORREF GetListBoxTextColor() const
Default: COLOR_WINDOWTEXT.
void SetControlBackColor(COLORREF value)
Sets a new background color for the control portion of the combobx when it doesn&#39;t have focus...