简体   繁体   English

如何检索本机Windows控件的正确大小?

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

I use this line to create an EDIT control: 我用这一行来创建一个EDIT控件:

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 : 在它旁边,有一个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. 如果我减小COMBOBOX的高度(当前为200),这也会限制实际下拉列表的最大高度。 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? COMBOBOX是否应该使用超过给定高度的下拉列表?

  2. How can I make my EDIT control the same height as the text field of my COMBOBOX control? 如何使我的EDIT控件与COMBOBOX控件的文本字段的高度相同?

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? 我应该将哪个高度应用于我的COMBOBOX以允许下拉列表尽可能扩展?

  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? 我应该将哪个高度应用于我的EDIT以使COMBOBOXEDIT控件的文本字段具有相同的高度?

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(). 如果它是CBS_SIMPLE,则高度由传递给CreateWindowEx()的nHeight参数确定。 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. 但是如果它是CBS_DROPDOWN / LIST,那么nHeight设置下拉范围,它自己计算出文本框部分的高度需要是什么。 Based on the font, sending WM_SETFONT changes the height. 根据字体,发送WM_SETFONT会改变高度。

Which height should I apply to my COMBOBOX to allow the dropdown list to expand as far as necessary? 我应该将哪个高度应用于我的COMBOBOX以允许下拉列表尽可能扩展?

It is entirely up to you. 这完全取决于你。 A sane choice is to have at least ~8 items visible. 一个明智的选择是至少可以看到~8个项目。 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? 我应该将哪个高度应用于我的编辑以使COMBOBOX和EDIT控件的文本字段具有相同的高度?

This tends to drive UI designers pretty batty, you can't get the same height when you give these controls the same font. 这往往会让UI设计师变得非常蹩脚,当您为这些控件提供相同的字体时,您无法获得相同的高度。 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. 奇怪的怪癖和我没有很好的解释,30年的appcompat很难逆向工程。 I'd assume it has something to do with the space needed for the focus rectangle that is displayed in the CBS_DROPDOWNLIST style. 我假设它与CBS_DROPDOWNLIST样式中显示的焦点矩形所需的空间有关。 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. Anyhoo,使用WM_SETFONT确保组合框和文本框以相同的字体显示文本。

Is the COMBOBOX supposed not to use more than the given height for the dropdown list? COMBOBOX是否应该使用超过给定高度的下拉列表?

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. 您甚至可以添加CBS_DISABLENOSCROLL以强制滚动条始终可见。

How can I make my EDIT control the same height as the text field of my COMBOBOX control? 如何使我的EDIT控件与COMBOBOX控件的文本字段的高度相同?

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: 编辑:使用GetComboBoxInfo获取组合框的编辑部分的句柄(以及其他内容),然后使用GetWindowRect获取其矩形:

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. 现在高度是r.bottom - r.top并且在我的测试中返回24 ,这非常接近你通过实验获得的25。 That said, it's easier to use a dialgo box. 也就是说,使用拨号盒更容易。 You can create a dialog box from resource or dynamically using the CreateDialogIndirect function. 您可以使用CreateDialogIndirect函数从资源创建对话框或动态创建对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM