我在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;
}
我们如何考虑左侧的图标?