无论如何要控制win32中COMBOBOX下拉列表的宽度?
答案 0 :(得分:4)
我的应用程序类中有一个公共方法:
void CSoundRotaApp::UpdateComboDroppedWidth(CComboBox& rCombo)
{
int iWidth = theApp.GetRequiredComboDroppedWidth(rCombo);
if (iWidth > rCombo.GetDroppedWidth())
rCombo.SetDroppedWidth(iWidth);
}
调用此方法:
int CSoundRotaApp::GetRequiredComboDroppedWidth(CComboBox& rCombo)
{
CString str;
CSize sz;
int dx = 0;
TEXTMETRIC tm;
CDC* pDC = rCombo.GetDC();
CFont* pFont = rCombo.GetFont();
// Select the listbox font, save the old font
CFont* pOldFont = pDC->SelectObject(pFont);
// Get the text metrics for avg char width
pDC->GetTextMetrics(&tm);
for (int i = 0; i < rCombo.GetCount(); i++)
{
rCombo.GetLBText(i, str);
sz = pDC->GetTextExtent(str);
// Add the avg width to prevent clipping
sz.cx += tm.tmAveCharWidth;
if (sz.cx > dx)
dx = sz.cx;
}
// Select the old font back into the DC
pDC->SelectObject(pOldFont);
rCombo.ReleaseDC(pDC);
// Adjust the width for the vertical scroll bar and the left and right border.
dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE);
return dx;
}
希望这有帮助。