September 20th, 2010

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”.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.