15,979
社区成员
发帖
与我相关
我的任务
分享
void
CMainFrame::OnDropDownFindBox( void )
{
// The pointer to my combo box.
CComboBox *p = ( CComboBox * )m_findbox;
// Find the longest string in the combo box.
CString str;
CSize sz;
int dx = 0;
TEXTMETRIC tm;
CDC* pDC = p->GetDC();
CFont* pFont = p->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);
int i;
for ( i = 0; i < p->GetCount(); i++)
{
p->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);
p->ReleaseDC(pDC);
// Adjust the width for the vertical scroll bar and the left and right border.
dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE);
// If the width of the list box is too small, adjust it so that every
// item is completely visible.
if (p->GetDroppedWidth() < dx)
{
p->SetDroppedWidth(dx);
ASSERT(p->GetDroppedWidth() == dx);
}
}
自适应的combobox。