22 CODComboBox::CODComboItem::CODComboItem() noexcept
25 m_BackColor = ::GetSysColor(COLOR_WINDOW);
26 m_TextColor = ::GetSysColor(COLOR_WINDOWTEXT);
28 m_iItemHeight = DEFAULT_COMBO_ITEM_HEIGHT;
34 m_bUseEllipsis =
true;
40 CODComboBox::CODComboItem::CODComboItem(std::basic_string<TCHAR> text, COLORREF text_color,
41 COLORREF back_color, HFONT font,
int height,
43 bool wrap,
bool use_ellipsis)
46 m_Text = text.c_str();
47 m_BackColor = back_color;
48 m_TextColor = text_color;
50 m_iItemHeight = height;
56 m_bUseEllipsis = use_ellipsis;
62 CODComboBox::CODComboItem::~CODComboItem()
66 ::DeleteObject(m_Brush);
71 void CODComboBox::CODComboItem::UpdateBrush()
74 ::DeleteObject(m_Brush);
76 m_Brush = ::CreateSolidBrush(m_BackColor);
93 m_bAutoAdjustDropWidth =
true;
94 m_iMaxDropDownWidth = 400;
99 m_bMsgHandled = FALSE;
105 ATLASSERT(m_hWnd == NULL);
124 CWindowImpl<CODComboBox, CComboBox>::SubclassWindow(hWnd);
127 info.cbSize =
sizeof(COMBOBOXINFO);
129 GetComboBoxInfo(&info);
131 m_wndControl = info.hwndItem;
132 m_wndListBox = info.hwndList;
134 if (::IsWindow(m_wndListBox))
135 m_ListBox.Attach(m_wndListBox);
137 if (::IsWindow(m_wndControl))
138 m_ControlWnd.Attach(m_wndControl);
150 if (m_ListBox.IsWindow())
153 if (m_ControlWnd.IsWindow())
154 m_ControlWnd.Detach();
156 int items = GetCount();
158 for (
int i = items - 1; i >= 0; i--)
177 return(m_bAutoAdjustDropWidth);
192 m_bAutoAdjustDropWidth = value;
207 return(m_iMaxDropDownWidth);
221 m_iMaxDropDownWidth = value;
238 CODComboItem* item =
new CODComboItem();
240 item->m_Font = GetFont();
241 item->m_iItemHeight = GetItemHeight(-1);
243 long result = AddString((LPCTSTR)item);
248 return((result != CB_ERR)&&(result != CB_ERRSPACE));
272 bool wrap,
bool use_ellipsis)
275 CODComboItem* item =
new CODComboItem();
277 item->m_Text = text.c_str();
278 item->m_BackColor = back_color;
279 item->m_TextColor = text_color;
281 item->m_iItemHeight = height;
282 item->m_HorzAlign = horz;
283 item->m_VertAlign = vert;
284 item->m_bWrap = wrap;
285 item->m_bUseEllipsis = use_ellipsis;
289 long result = AddString((LPCTSTR)item);
294 return((result != CB_ERR)&&(result != CB_ERRSPACE));
314 CODComboItem* item =
new CODComboItem();
316 item->m_Font = GetFont();
317 item->m_iItemHeight = DEFAULT_COMBO_ITEM_HEIGHT;
319 long result = InsertString(index, (LPCTSTR)item);
324 return((result != CB_ERR)&&(result != CB_ERRSPACE));
350 bool wrap,
bool use_ellipsis)
353 CODComboItem* item =
new CODComboItem();
355 item->m_Text = text.c_str();
356 item->m_BackColor = back_color;
357 item->m_TextColor = text_color;
359 item->m_iItemHeight = height;
360 item->m_HorzAlign = horz;
361 item->m_VertAlign = vert;
362 item->m_bWrap = wrap;
363 item->m_bUseEllipsis = use_ellipsis;
367 long result = InsertString(index, (LPCTSTR) item);
372 return((result != CB_ERR)&&(result != CB_ERRSPACE));
391 LRESULT result = DeleteString(index);
393 return(result != CB_ERR);
409 CODComboItem* item = GetAndVerifyItemData(index);
410 CString result = item->m_Text;
412 return((LPCTSTR)result);
428 CODComboItem* item = GetAndVerifyItemData(index);
429 item->m_Text = text.c_str();
447 CODComboItem* item = GetAndVerifyItemData(index);
448 COLORREF result = item->m_BackColor;
466 CODComboItem* item = GetAndVerifyItemData(index);
467 item->m_BackColor = color;
484 CODComboItem* item = GetAndVerifyItemData(index);
485 COLORREF result = item->m_TextColor;
503 CODComboItem* item = GetAndVerifyItemData(index);
504 item->m_TextColor = color;
520 CODComboItem* item = GetAndVerifyItemData(index);
521 HFONT result = item->m_Font;
539 CODComboItem* item = GetAndVerifyItemData(index);
558 CODComboItem* item = GetAndVerifyItemData(index);
559 int result = item->m_iItemHeight;
577 CODComboItem* item = GetAndVerifyItemData(index);
578 item->m_iItemHeight = height;
594 CODComboItem* item = GetAndVerifyItemData(index);
613 CODComboItem* item = GetAndVerifyItemData(index);
614 item->m_HorzAlign = value;
630 CODComboItem* item = GetAndVerifyItemData(index);
649 CODComboItem* item = GetAndVerifyItemData(index);
650 item->m_VertAlign = value;
666 CODComboItem* item = GetAndVerifyItemData(index);
667 bool result = item->m_bWrap;
687 CODComboItem* item = GetAndVerifyItemData(index);
688 item->m_bWrap = wrap;
704 CODComboItem* item = GetAndVerifyItemData(index);
705 bool result = item->m_bUseEllipsis;
724 CODComboItem* item = GetAndVerifyItemData(index);
725 item->m_bUseEllipsis = ellipsis;
746 if (m_bAutoAdjustDropWidth ==
true)
750 for (
int i = 0; i < GetCount(); i++)
752 CODComboItem* item = (CODComboItem*)GetItemDataPtr(i);
760 if (max > m_iMaxDropDownWidth)
761 max = m_iMaxDropDownWidth;
764 GetWindowRect(&window_rect);
765 int w_w = window_rect.right - window_rect.left;
770 SetDroppedWidth(max);
789 int total_height = 0;
791 for (
int i = 0; i < GetCount(); i++)
793 CODComboItem* item = (CODComboItem*)GetItemDataPtr(i);
794 total_height += item->m_iItemHeight;
797 RECT control_client_rect;
802 GetWindowRect(&parent_rect);
803 m_ListBox.GetWindowRect(&list_rect);
805 m_ControlWnd.GetWindowRect(&control_rect);
806 m_ControlWnd.GetClientRect(&control_client_rect);
808 total_height += (control_client_rect.bottom - control_client_rect.top);
810 total_height += (control_rect.bottom - control_rect.top)
811 - (control_client_rect.bottom - control_client_rect.top);
813 total_height += 2 * (control_rect.top - parent_rect.top);
815 int edge = GetSystemMetrics(SM_CXBORDER) * 2;
816 total_height += edge;
818 parent_rect.bottom = parent_rect.top + total_height;
820 SetWindowPos(GetParent(), &parent_rect, SWP_NOMOVE | SWP_NOZORDER);
831 CODComboBox::CODComboItem* CODComboBox::GetAndVerifyItemData(
int index)
const 833 LRESULT lr = GetItemData(index);
835 ATLASSERT (lr != CB_ERR);
837 CODComboItem* item = (CODComboItem*) lr;
842 UINT CODComboBox::GetTextFormat(CODComboItem* item)
844 UINT format = DT_EDITCONTROL;
846 if (item->m_bWrap ==
true)
847 format |= DT_WORDBREAK;
849 format |= DT_SINGLELINE;
851 if (item->m_bUseEllipsis ==
true)
852 format |= DT_END_ELLIPSIS;
854 switch(item->m_HorzAlign)
869 switch (item->m_VertAlign)
876 format |= DT_VCENTER;
895 void CODComboBox::DeleteItem(LPDELETEITEMSTRUCT lpDeleteItemStruct)
898 CODComboItem* item = (CODComboItem*) lpDeleteItemStruct->itemData;
903 int CODComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
907 CODComboItem* item1 = (CODComboItem*)lpCompareItemStruct->itemData1;
908 CODComboItem* item2 = (CODComboItem*)lpCompareItemStruct->itemData2;
910 int comp = item1->m_Text.Compare(item2->m_Text);
920 void CODComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpStruct)
922 int index = lpStruct->itemID;
924 if ((index == -1) || (index == ((UINT)-1)))
925 lpStruct->itemHeight = GetItemHeight(-1);
927 lpStruct->itemHeight = ((CODComboItem*)lpStruct->itemData)->m_iItemHeight;
932 void CODComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
935 HDC dc = lpDrawItemStruct->hDC;
936 CODComboItem* item = (CODComboItem*)lpDrawItemStruct->itemData;
938 HFONT old_font = (HFONT)::SelectObject(dc, item->m_Font);
940 UINT format = GetTextFormat(item);
943 if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
944 (lpDrawItemStruct->itemState & ODS_SELECTED)&&
945 ( ! (lpDrawItemStruct->itemState & ODS_COMBOBOXEDIT)))
947 ::FillRect(dc, &lpDrawItemStruct->rcItem, ::GetSysColorBrush(COLOR_HIGHLIGHT));
949 ::SetTextColor(dc, ::GetSysColor(COLOR_HIGHLIGHTTEXT));
950 ::SetBkColor(dc, ::GetSysColor(COLOR_HIGHLIGHT));
955 FillRect(dc, &lpDrawItemStruct->rcItem, item->m_Brush);
957 ::SetTextColor(dc, item->m_TextColor);
958 ::SetBkColor(dc, item->m_BackColor);
962 ::SetBkMode(dc, TRANSPARENT);
964 ::DrawText(dc, item->m_Text, item->m_Text.GetLength(), &lpDrawItemStruct->rcItem, format);
966 ::SelectObject(dc, old_font);
968 if (lpDrawItemStruct->itemState & ODS_FOCUS)
969 ::DrawFocusRect(dc, &lpDrawItemStruct->rcItem);
bool GetAutoAdjustDropRectWidth() const
Default: true.
int GetODItemHeight(int index) const
Default: The height of the control portion of the combobox at the time the CODComboItem is instantiat...
void SetODItemUseEllipsis(int index, bool ellipsis)
Modify the UseEllipsis state of the selected combobox item.
CVertAlignment GetODItemVertAlignment(int index) const
DefaultL: vaMiddle.
void SetODItemBackColor(int index, COLORREF color)
Modify the background color of the selected combobox item.
HFONT GetODItemFont(int index) const
Default: The font assigned to the combo box when the CODComboItem object is instantiated.
void SetMaxDropDownWidth(int value)
Sets the upper limit to the width of the dropdown list, if it is being automatically adjusted...
CHorzAlignment GetODItemHorzAlignment(int index) const
Default: haLeft.
CVertAlignment
Control vertical placement of text in control window.
int GetTextLength(CString str, HFONT font)
Measure the length, in pixels, of a string using a given font.
void SetODItemHeight(int index, int height)
Modify the height of the selected combobox item.
bool ODAddItem()
Add new item to the end of the list box.
bool GetODItemWordWrap(int index) const
Default: false.
void SubclassComboBox(HWND wnd)
Subclass a combobox control.
void SetODItemFont(int index, HFONT font)
Modify the font assigned to the selected combobox item.
COLORREF GetODItemBackColor(int index) const
Default: COLOR_WINDOW.
void UpdateDropDownWidth()
Force an update of the width of the drop down list.
void SetODItemVertAlignment(int index, CVertAlignment value)
Modify the vertical text aligment of the selected combobox item.
bool ODInsertItem(int index)
Insert an item.
void SetODItemText(int index, std::basic_string< TCHAR > text)
Modify the text associated with a combobox item.
int GetMaxDropDownWidth() const
Default: 400 pixels.
void SetODItemHorzAlignment(int index, CHorzAlignment value)
Modify the horizontal text alignment of the selected combobox item.
CHorzAlignment
Control horizontal placement of text in control window.
void UnSubclassComboBox()
Un-subclass main and component windows; free GDI resources.
void SetODItemWordWrap(int index, bool wrap)
Modify the WordWrap state of the selected combobox item.
COLORREF GetODItemTextColor(int index) const
Default: COLOR_WINDOWTEXT.
std::basic_string< TCHAR > GetODItemText(int index) const
Default: "".
bool ODRemoveItem(int index)
Deletes an item from the drop-down list.
bool GetODItemUseEllipsis(int index) const
Default: true.
void UpdateControlHeight()
Update the combobox height to accomodate all content.
void SetAutoAdjustDropRectWidth(bool value)
Change the AutoAdjustRectWidth state.
void SetODItemTextColor(int index, COLORREF color)
Modify the text color of the selected combobox item.