24 m_bAutoAdjustDropWidth =
true;
25 m_iMaxDropDownWidth = 400;
27 m_ListBoxBackColor = GetSysColor(COLOR_WINDOW);
28 m_ControlBackColor = m_ListBoxBackColor;
29 m_ControlFocusedBackColor = m_ListBoxBackColor;
31 m_ListBoxTextColor = ::GetSysColor(COLOR_WINDOWTEXT);
32 m_ControlTextColor = m_ListBoxTextColor;
33 m_ControlFocusedTextColor =
CLR_RED;
35 m_ControlBackBrush = NULL;
36 m_ListBoxBackBrush = NULL;
37 m_ControlFocusedBackBrush = NULL;
49 ATLASSERT(m_hWnd == NULL);
70 CWindowImpl<CComboBoxPlus, CComboBox>::SubclassWindow(hWnd);
72 COMBOBOXINFO info{ 0 };
73 int size =
sizeof(COMBOBOXINFO);
76 GetComboBoxInfo(&info);
78 m_wndControl = info.hwndItem;
79 m_wndListBox = info.hwndList;
81 if (::IsWindow(m_wndListBox))
82 m_ListBox.Attach(m_wndListBox);
84 if (::IsWindow(m_wndControl))
85 m_ControlWnd.Attach(m_wndControl);
98 if (m_ListBox.IsWindow())
101 if (m_ControlWnd.IsWindow())
102 m_ControlWnd.Detach();
129 if (m_bAutoAdjustDropWidth ==
true)
133 HFONT font = GetFont();
134 for (
int i = 0; i < GetCount(); i++)
139 int cchLen = GetLBTextLen(i);
141 LPTSTR lpstr = item.GetBufferSetLength(cchLen);
145 item.ReleaseBuffer();
152 if (max > m_iMaxDropDownWidth)
153 max = m_iMaxDropDownWidth;
156 m_ControlWnd.GetWindowRect(&window_rect);
157 int w_w = window_rect.right - window_rect.left;
162 SetDroppedWidth(max);
168 m_ControlWnd.GetWindowRect(&window_rect);
169 SetDroppedWidth(window_rect.right - window_rect.left);
186 return(m_bAutoAdjustDropWidth);
201 m_bAutoAdjustDropWidth = value;
216 return(m_iMaxDropDownWidth);
230 m_iMaxDropDownWidth = value;
245 return(m_ControlBackColor);
259 if (m_ControlBackColor != value)
261 m_ControlBackColor = value;
277 return(m_ControlTextColor);
291 m_ControlTextColor = value;
305 return(m_ControlFocusedBackColor);
319 if (m_ControlFocusedBackColor != value)
321 m_ControlFocusedBackColor = value;
337 return(m_ControlFocusedTextColor);
352 m_ControlFocusedTextColor = value;
366 return(m_ListBoxBackColor);
380 if (m_ListBoxBackColor != value)
382 m_ListBoxBackColor = value;
398 return(m_ListBoxTextColor);
412 m_ListBoxTextColor = value;
425 return (m_bReadOnly);
442 if (m_bReadOnly != value)
447 DWORD combostyle = GetStyle();
449 if (((combostyle & CBS_DROPDOWN) != 0) || ((combostyle & CBS_SIMPLE) != 0))
453 if (m_bReadOnly ==
true)
455 if (::IsWindow(m_wndControl) == TRUE)
457 DWORD style = m_ControlWnd.GetStyle();
459 if ((style & ES_READONLY) != 0)
464 m_ControlWnd.Detach();
467 ed.Attach(m_wndControl);
471 m_ControlWnd.Attach(m_wndControl);
477 if (::IsWindow(m_ControlWnd))
479 DWORD style = m_ControlWnd.GetStyle();
481 if ((style & ES_READONLY) == 0)
486 m_ControlWnd.Detach();
489 ed.Attach(m_wndControl);
490 ed.SetReadOnly(FALSE);
493 m_ControlWnd.Attach(m_wndControl);
508 void CComboBoxPlus::UpdateBrushes()
513 m_ControlBackBrush = ::CreateSolidBrush(m_ControlBackColor);
515 m_ListBoxBackBrush = ::CreateSolidBrush(m_ListBoxBackColor);
517 m_ControlFocusedBackBrush = ::CreateSolidBrush(m_ControlFocusedBackColor);
521 void CComboBoxPlus::DeleteBrushes()
524 if (m_ControlBackBrush != NULL)
526 ::DeleteObject(m_ControlBackBrush);
527 m_ControlBackBrush = NULL;
530 if (m_ControlFocusedBackBrush != NULL)
532 ::DeleteObject(m_ControlFocusedBackBrush);
533 m_ControlFocusedBackBrush = NULL;
536 if (m_ListBoxBackBrush != NULL)
538 ::DeleteObject(m_ListBoxBackBrush);
539 m_ListBoxBackBrush = NULL;
549 LRESULT CComboBoxPlus::OnCtlColorControl(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
552 if (((HWND)lParam) == m_ControlWnd.m_hWnd)
556 HDC dc = (HDC)wParam;
557 ::SetBkMode(dc, TRANSPARENT);
559 if (GetFocus() == m_ControlWnd.m_hWnd)
561 ::SetTextColor(dc, m_ControlFocusedTextColor);
562 ::SetBkColor(dc, m_ControlFocusedBackColor);
564 return((
long)m_ControlFocusedBackBrush);
568 ::SetTextColor(dc, m_ControlTextColor);
569 ::SetBkColor(dc, m_ControlBackColor);
571 return((
long)m_ControlBackBrush);
581 HBRUSH CComboBoxPlus::OnCtlColorListBox(CDCHandle dc, CListBox listBox)
583 if (listBox.m_hWnd == m_ListBox.m_hWnd)
585 dc.SetTextColor(m_ListBoxTextColor);
586 dc.SetBkColor(m_ListBoxBackColor);
587 dc.SetBkMode(TRANSPARENT);
589 return(m_ListBoxBackBrush);
593 SetMsgHandled(FALSE);
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.
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'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't have focus...