Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the correct size of native Windows controls?

I use this line to create an EDIT control:

hMyEdit = CreateWindowEx(
  WS_EX_CLIENTEDGE,
  L"EDIT",
  L"",
  WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_LEFT,
  10,
  10, 
  200,
  25,
  hParentWnd,
  (HMENU)IDC_MY_EDIT,
  hInst,
  NULL
);

Next to it, there is a COMBOBOX:

hMyCombo = CreateWindowEx(
  WS_EX_CLIENTEDGE,
  L"COMBOBOX",
  L"",
  WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | WS_VSCROLL | ES_LEFT | CBS_DROPDOWNLIST| CBS_HASSTRINGS,
  220,
  10,
  90,
  200,
  hParentWnd,
  (HMENU)IDC_MY_COMBO,
  hInst, NULL
);

There are two problems I cannot figure out:

  1. If I reduce the height (currently 200) of my COMBOBOX, this also limits the maximum height of the actual dropdown list. However, the actual height of the control without the dropdown list is not affected at all. Is the COMBOBOX supposed not to use more than the given height for the dropdown list?

  2. How can I make my EDIT control the same height as the text field of my COMBOBOX control?

I was unable to find any documentation about default sizes, but I hope there is a proper way to size controls.

To sum it up, my questions are:

  1. Which height should I apply to my COMBOBOX to allow the dropdown list to expand as far as necessary?

  2. Which height should I apply to my EDIT to have the same height for the text field of the COMBOBOX and the EDIT control?

like image 352
just.me Avatar asked Jan 15 '16 10:01

just.me


1 Answers

Behavior depends on the style you selected for the combobox. If it is CBS_SIMPLE then the height is determined by the nHeight argument you pass to CreateWindowEx(). But if it is CBS_DROPDOWN/LIST then nHeight sets the dropdown extent and it figures out by itself what the height of the textbox portion needs to be. Based on the font, sending WM_SETFONT changes the height.

Which height should I apply to my COMBOBOX to allow the dropdown list to expand as far as necessary?

It is entirely up to you. A sane choice is to have at least ~8 items visible. Consider the location of the combobox in its parent's client area. You'd normally favor the dropdown list staying inside the parent. But that's not always practical, if the combobox is near the bottom of the window then you have no option but letting it extend beyond the parent's bottom. Beware the usability problem that this causes, the list won't be completely visible if the parent window is located near the bottom of the desktop.

Which height should I apply to my EDIT to have the same height for the text field of the COMBOBOX and the EDIT control?

This tends to drive UI designers pretty batty, you can't get the same height when you give these controls the same font. The combobox will be two pixels taller. Text aligns properly however. Strange quirk and I don't have a very good explanation for that, 30 years of appcompat can be hard to reverse-engineer. I'd assume it has something to do with the space needed for the focus rectangle that is displayed in the CBS_DROPDOWNLIST style. You could tinker with the font, giving the combobox an intentionally smaller font but that does not look very good either. Anyhoo, use WM_SETFONT to ensure the combobox and textbox display text in the same font.

like image 173
Hans Passant Avatar answered Sep 30 '22 03:09

Hans Passant