Reputation: 2285
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:
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?
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:
Which height should I apply to my COMBOBOX
to allow the dropdown list to expand as far as necessary?
Which height should I apply to my EDIT
to have the same height for the text field of the COMBOBOX
and the EDIT
control?
Upvotes: 9
Views: 244
Reputation: 1736
Is the
COMBOBOX
supposed not to use more than the given height for the dropdown list?
No, a scrollbar will appear when the content does not fit inside the listbox part. And you can even add CBS_DISABLENOSCROLL
to force the scrollbar to be always visible.
How can I make my
EDIT
control the same height as the text field of myCOMBOBOX
control?
EDIT: Use GetComboBoxInfo to get the handle of the edit part of the combo box (among other things), then use GetWindowRect to gets its rectangle:
COMBOBOXINFO cbi;
cbi.cbSize = sizeof(cbi);
GetComboBoxInfo(hMyCombo, &cbi);
GetWindowRect(cbi.hwndCombo, &r);
Now the height is r.bottom - r.top
and that returned 24
on my test, which is very close to the 25 you got experimentally.
That said, it's easier to use a dialgo box. You can create a dialog box from resource or dynamically using the CreateDialogIndirect function.
Upvotes: 0
Reputation: 942089
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.
Upvotes: 5