bool CPPageBase::FillComboToolTip(CComboBox& comboBox, TOOLTIPTEXT* pTTT) { bool bNeedTooltip = false; CDC* pDC = comboBox.GetDC(); CFont* pFont = comboBox.GetFont(); CFont* pOldFont = pDC->SelectObject(pFont); TEXTMETRIC tm; pDC->GetTextMetrics(&tm); CRect comboBoxRect; comboBox.GetWindowRect(comboBoxRect); comboBoxRect.right -= GetSystemMetrics(SM_CXVSCROLL) + 2 * GetSystemMetrics(SM_CXEDGE); int i = comboBox.GetCurSel(); CString str; comboBox.GetLBText(i, str); CSize textSize; textSize = pDC->GetTextExtent(str); pDC->SelectObject(pOldFont); comboBox.ReleaseDC(pDC); textSize.cx += tm.tmAveCharWidth; if (textSize.cx > comboBoxRect.Width()) { bNeedTooltip = true; if (str.GetLength() > _countof(pTTT->szText) - 1) { str.Truncate(_countof(pTTT->szText) - 1); } _tcscpy_s(pTTT->szText, str); pTTT->hinst = nullptr; } return bNeedTooltip; }
void UpdateHorzExtent(CComboBox &rctlComboBox, int iIconWidth) { int iItemCount = rctlComboBox.GetCount(); if (iItemCount > 0) { CDC *pDC = rctlComboBox.GetDC(); if (pDC != NULL) { // *** To get *ACCURATE* results from 'GetOutputTextExtent' one *MUST* // *** explicitly set the font! CFont *pOldFont = pDC->SelectObject(rctlComboBox.GetFont()); CString strItem; int iMaxWidth = 0; for (int i = 0; i < iItemCount; i++) { rctlComboBox.GetLBText(i, strItem); int iItemWidth = pDC->GetOutputTextExtent(strItem, strItem.GetLength()).cx; if (iItemWidth > iMaxWidth) iMaxWidth = iItemWidth; } pDC->SelectObject(pOldFont); rctlComboBox.ReleaseDC(pDC); // Depending on the string (lot of "M" or lot of "i") sometime the // width is just a few pixels too small! iMaxWidth += 4; if (iIconWidth) iMaxWidth += 2 + iIconWidth + 2; rctlComboBox.SetHorizontalExtent(iMaxWidth); if (rctlComboBox.GetDroppedWidth() < iMaxWidth) rctlComboBox.SetDroppedWidth(iMaxWidth); } } else rctlComboBox.SetHorizontalExtent(0); }