How do I get the dropped height of a combo box?


Via the Suggestion Box, commenter Twisted Combo responds to an old blog entry on why the size of a combo box includes the height of the drop-down by asking, But how do I *get* the dropped down height?"

By using the deviously-named CB_GETDROPPEDCONTROLRECT message, which the windowsx.h header file wraps inside the ComboBox_GetDroppedControlRect macro.

Start with the scratch program and make these changes:

BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
  g_hwndChild = CreateWindow(
      TEXT("combobox"), NULL, WS_CHILD | WS_VISIBLE |
      WS_TABSTOP | CBS_DROPDOWN,
      0, 0, 500, 500, hwnd, (HMENU)1, g_hinst, 0);

  ComboBox_AddString(g_hwndChild, TEXT("First"));
  ComboBox_AddString(g_hwndChild, TEXT("Second"));
  ComboBox_AddString(g_hwndChild, TEXT("Third"));
  ComboBox_AddString(g_hwndChild, TEXT("Fourth"));

  TCHAR szBuf[200];
  RECT rcWindow;
  GetWindowRect(g_hwndChild, &rcWindow);
  RECT rcDrop;
  ComboBox_GetDroppedControlRect(g_hwndChild, &rcDrop);
  wsprintf(szBuf, TEXT("window height %d, dropdown height %d"),
    rcWindow.bottom - rcWindow.top, rcDrop.bottom - rcDrop.top);
  SetWindowText(hwnd, szBuf);

  return TRUE;
}

The actual results will naturally vary depending on your system configuration, but when I ran this program, the window caption said "24 / 500".

Comments (3)
  1. Alex Grigoriev says:

    As a reminder, when you edit your dialog layout, make sure to expand the dropbox size. The default size is like 1 line.

  2. asf says:

    IIRC the dialog height issue does not apply when visual styles are enabled

    [See linked article. -Raymond]
  3. jamome says:

    What is the correct programmatic way to set the height?  There is no ComboBox_SetDroppedControlRect

Comments are closed.

Skip to main content