Reputation: 2670
Is there anyway to control the width of the dropdown list of a COMBOBOX in win32?
Upvotes: 3
Views: 2156
Reputation: 19087
I have a public method in my application class:
void CSoundRotaApp::UpdateComboDroppedWidth(CComboBox& rCombo)
{
int iWidth = theApp.GetRequiredComboDroppedWidth(rCombo);
if (iWidth > rCombo.GetDroppedWidth())
rCombo.SetDroppedWidth(iWidth);
}
Which calls this method:
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;
}
Hope this helps.
Upvotes: 6