Recalculates the optimal horizontal scrolling extent.
C
void WINAPI SftBox_RecalcHorizontalExtent(HWND hwndCtl, LONG maximum);
C++
void CSftBox::RecalcHorizontalExtent(LONG maximum);
hwndCtl
The window handle of the combo box control.
maximum
Defines the maximum number of items to be considered for the width calculation. Specify a number less than or equal to 0 to consider all items. If a combo box contains many items, scanning all items may be slow, as each cell's width needs to be calculated. Using an application defined maximum number, the calculation can be limited to maximum items. This results in better response time, yet some items which are not within the number of scanned items may still be clipped.
The RecalcHorizontalExtent function recalculates the optimal horizontal scrolling extent.
The horizontal scrolling extent defines the total width that can be scrolled horizontally. An application uses RecalcHorizontalExtent to let the combo box calculate the extent or SetHorizontalExtent to define a specific value.
Only items that are visible are considered. Items which are not visible because their parent items are collapsed are not included in the width calculation.
Based on the definition of the last column, different algorithms are used to calculate the optimal scrolling extent. Using SetOpenEnded, the last column can be defined as open-ended. An open-ended last column displays the complete text specified for the last (or only) column and never truncates any data. A fixed-width last column is defined with a specified width and any data which doesn't fit is truncated.
Open-ended Last Column
Recalculating the best horizontal scrolling extent can be a costly operation (in terms of elapsed time). When updating a combo box, it is best to delay using RecalcHorizontalExtent as much as possible. It is best done after all items have been added, their levels have been set and all necessary pictures and attributes have been defined, because most changes to the combo box can invalidate the optimal horizontal scrolling extent calculated.
When calculating the optimal scrolling extent, the width of the last column is calculated for each item using the item's cell text, cell picture and level. The widest item determines the horizontal scrolling extent.
Fixed-Width Last Column
Recalculating the best horizontal scrolling extent is a very quick operation. The width of all columns and some initial overhead (based on the highest level number found) is calculated to determine the horizontal scrolling extent. No item text is analyzed.
for (i = 0 ; i < COLOR_COUNT ; ++i) {
index = SftBox_AddString(hwndBox, m_aColors[i].lpszName);
SftBox_SetText(hwndBox, index, 1, m_aColors[i].lpszDesc);
SftBox_SetItemData(hwndBox, index, m_aColors[i].color);
SetColorSample(hwndBox, index, m_aColors[i].color);
}
SftBox_MakeColumnOptimal(hwndBox, -1, 0); // Make all columns optimal
SftBox_RecalcHorizontalExtent(hwndBox, 0);
// Select the item with the requested color, searching after the custom color
index = SftBox_FindItem(hwndBox, CUSTOM_INDEX + 1, selected);
SftBox_SetCurSel(hwndBox, index >= 0 ? index : CUSTOM_INDEX);
}
static COLORREF GetSelectedColor(HWND hwndBox)
for (int i = 0 ; i < COLOR_COUNT ; ++i) {
index = box.AddString(m_aColors[i].lpszName);
box.SetText(index, 1, m_aColors[i].lpszDesc);
box.SetItemData(index, m_aColors[i].color);
SetColorSample(box, index, m_aColors[i].color);
}
box.MakeColumnOptimal(-1, 0); // Make all columns optimal
box.RecalcHorizontalExtent(0);
// Select the item with the requested color, searching after the custom color
index = box.FindItem(CUSTOM_INDEX + 1, selected);
box.SetCurSel(index >= 0 ? index : CUSTOM_INDEX);
}
COLORREF CMainDlg::GetSelectedColor(const CSftBox& box) constSee Also C/C++ API | Categories | Notifications