Thursday, December 18, 2008

Resize a combo box at run-time

/*--------------------------------------------------------------------------------

* Purpose : Set the proper number of lines in a drop-down list or combo box.

* Description : Resizes the combo box window to fit the proper number of lines.

*-------------------------------------------------------------------------------*/

void Set_DropDownSize(CComboBox& box, UINT nLinesToDisplay)

{

CRect cbSize;

box.GetClientRect(cbSize);

int nHeight;

nHeight = box.GetItemHeight(-1); // start with size of the edit-box portion

nHeight += box.GetItemHeight(0) * nLinesToDisplay; // add height of lines of text

// Add the height of the border of the edit box

nHeight += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

// Add the height of the border of the drop-down box

nHeight += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

// Set the size of the window

box.SetWindowPos(NULL, 0, 0,

cbSize.right, nHeight, // existing width, new height

SWP_NOMOVE | SWP_NOZORDER // don't move box or change z-ordering.

);

}

No comments: