一、效果
二、关键逻辑
继承CComboBox,在OnCbnDropdown里实现根据下拉框内容调整下拉框高度
void CComboBoxDropEx::OnCbnDropdown()
{
// TODO: 在此添加控件通知处理程序代码
CDC* pDC = GetDC();
if (pDC != NULL)
{
CString strText;
CRect rc;
GetClientRect(rc);
int nMaxWidth = rc.Width();
const int nCount = GetCount();
for (int i = 0; i < nCount; i++)
{
GetLBText(i, strText);
CSize size = pDC->GetTextExtent(strText, strText.GetLength());
if (size.cx > nMaxWidth)
{
nMaxWidth = size.cx;
}
}
SetDroppedWidth(nMaxWidth * 0.9);
ReleaseDC(pDC);
}
}