Я видел эту статью в CodeProject для динамической установки ширины CComboBox
.
Однако я использую CComboBoxEx
:
Как вы видите, последняя запись обрезана. Поэтому я хотел бы автоматически расширить раскрывающийся список.
Необходимо учитывать тот факт, что слева также есть место для значка. Так что этого будет недостаточно:
BOOL CMyComboBox::OnCbnDropdown()
{
// Reset the dropped width
CString str;
CRect rect;
int nWidth = 0;
int nNumEntries = GetCount();;
CClientDC dc(this);
int nSave = dc.SaveDC();
dc.SelectObject(GetFont());
for (int i = 0; i < nNumEntries; i++)
{
GetLBText(i, str);
int nLength = dc.GetTextExtent(str).cx;
if (nLength>nWidth)
nWidth = nLength;
}
nWidth += 2*::GetSystemMetrics(SM_CXEDGE) + 4;
// check if the current height is large enough for the items in the list
GetDroppedControlRect(&rect);
if (rect.Height() <= nNumEntries*GetItemHeight(0))
nWidth +=::GetSystemMetrics(SM_CXVSCROLL);
dc.RestoreDC(nSave);
SetDroppedWidth(nWidth);
return FALSE;
}
Как мы учитываем значок слева?
COMBOBOXEXITEM::iIndent
— документы. microsoft.com/en-us/windows/desktop/api/commctrl/ — количество отступов, отображаемых для элемента. Каждый отступ равен 10 пикселям. - person sergiol   schedule 14.12.2018GetItem
для получения отступа и текста? Возможно, вы можете обновить мой ответ добавлением? - person Andrew Truckle   schedule 15.12.2018iIndent
представляет интервал в ЛЕВО значка. Это полезно для иерархической организации элементов в раскрывающемся списке. - person sergiol   schedule 15.12.2018