【发布时间】:2017-04-26 13:37:20
【问题描述】:
win32中是否有控制COMBOBOX下拉列表宽度的方法?
【问题讨论】:
-
只是补充一点,我最终做了 SendDlgItemMessage(m_hWnd,IDC_MYCOMBO,CB_SETDROPPEDWITH,width,0);这里的宽度以像素为单位。
-
@MyDeveloperDay 应该是 CB_SETDROPPEDWIDTH
win32中是否有控制COMBOBOX下拉列表宽度的方法?
【问题讨论】:
我的应用程序类中有一个公共方法:
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;
}
希望这会有所帮助。
【讨论】:
UpdateComboDroppedWidth,你可以让它发生在下拉事件处理程序中。