Katy -> Visual C++ Stuff -> Resize a combo box at run-time


This code snippet uses MFC classes to resize a dropdown combo box at run-time. It should be easy to adapt to plain API code if you need to. It should work on any version of Windows.

You can resize at design-time by clicking on the drop-down arrow in developer studio.

void set_DropDownSize(CComboBox& box, UINT LinesToDisplay)
/*--------------------------------------------------------------------------
 * 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. The window must exist before calling this function.
 *  This function should be called when the combo box is created, and when
 *  the font of the combo box changes. (e.g. WM_SETTINGCHANGE)
 *  Testing needed:
 *    Are there cases where SM_CYBORDER should be used instead of SM_CYEDGE?
 *    owner-draw variable height combo box
 *    Subclassed combo box with horizontal scroll-bar
 * Returns:       nothing
 * Author:        KTM
 *--------------------------------------------------------------------------*/
{
    ASSERT(IsWindow(box));	// Window must exist or SetWindowPos won't work

    CRect cbSize;			// current size of combo box
    int Height;            // new height for drop-down portion of combo box

    box.GetClientRect(cbSize);
    Height = box.GetItemHeight(-1);      // start with size of the edit-box portion
    Height += box.GetItemHeight(0) * LinesToDisplay;	// add height of lines of text

    // Note: The use of SM_CYEDGE assumes that we're using Windows '95
    // Now add on the height of the border of the edit box
    Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

    // The height of the border of the drop-down box
    Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

    // now set the size of the window
    box.SetWindowPos(NULL,            // not relative to any other windows
        0, 0,                         // TopLeft corner doesn't change
        cbSize.right, Height,         // existing width, new height
        SWP_NOMOVE | SWP_NOZORDER     // don't move box or change z-ordering.
        );
}

 

 


No solicitation beyond this point.
Don't even think of adding me to your mailing list. Put up a web page I'm interested in, and let me ask you for information.
Comments? Questions? Send mail to Katy

This page was last modified on 1998/08/23