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

Date:September 20, 2010 / year-entry #264
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20100920-00/?p=12813
Comments:    3
Summary: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...

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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index