Delphi VCL自定义TCombobox下拉宽度

huangapple go评论156阅读模式
英文:

Delphi VCL Custom TCombobox Dropdown Width

问题

我有一个Delphi的VCL TCombobox的简化版本,专为一个特殊客户展示定制。它只有两个特性(分隔线、多行项目),在现成的组件中找不到。功能正常,但我无法控制下拉区域/弹出菜单的大小。我按照搜索到的内容做了如下处理:

procedure TMyownCombobox.DropDown;
begin
  inherited;
  var iDropdownHeight:=360;
  // 不起作用,因为它影响的是宽度,而不是飞出菜单的高度
  // 根据WinAPI文档,没有类似于CB_SETDROPPEDWIDTH用于高度的等价物("CB_SETDROPPEDHEIGHT")
  SendMessage(Self.Handle, CB_SETDROPPEDWIDTH, Width, MakeLParam(iDropdownHeight, 0));
end;

不幸的是,大小总是约100像素,无论有多少项目。设置DropdownCount属性没有影响。渲染项目效果很好,但用户必须滚动很多。在渲染项目时,有一个我自己实现的MeasureItem()过程,但我在飞出菜单中找不到类似的东西。

英文:

I have a simple descendant of Delphi's VCL TCombobox for a very special customer showcase. It has only 2 features (separator lines, multi-line items) that I did not find on a ready-to-use component. Works fine, but I'm unable to control the size of the dropdown area / flyout menu below. I did what I found googling that:

procedure TMyownCombobox.DropDown;
begin
inherited;
var iDropdownHeight:=360;
// does NOT work, as it affects the width, not the height of the flyout
// according to WinAPI documentation, there is no equivalent of CB_SETDROPPEDWIDTH for the height ("CB_SETDROPPEDHEIGHT")
SendMessage(Self.Handle, CB_SETDROPPEDWIDTH,Width,MakeLParam(iDropdownHeight,0));
end;

Unfortunately, size is always about 100px, no matter how many items there are. Settingt th DropdownCount property has no affect. Rendering the items works great, but users have to scroll a lot. When rendering my items, there is a MeasureItem() procedure that I implemented myself, but I could'nt find anything like that for the flyout menu.

答案1

得分: 3

CB_SETDROPPEDWIDTH消息用于设置下拉列表的宽度:

应用程序发送CB_SETDROPPEDWIDTH消息以设置具有CBS_DROPDOWNCBS_DROPDOWNLIST样式的组合框的列表框的最小允许宽度(以像素为单位)。

因此,它不会影响高度。

要设置高度,您可以使用CB_SETMINVISIBLE消息:

设置组合框下拉列表中可见项目的最小数目。

例如,

procedure TForm1.FormCreate(Sender: TObject);
begin
  SendMessage(ComboBox1.Handle, CB_SETDROPPEDWIDTH, 200, 0);
  SendMessage(ComboBox1.Handle, CB_SETMINVISIBLE, 50, 0);
end;

请注意,宽度单位为像素,而高度单位为项目。

Delphi VCL自定义TCombobox下拉宽度

但是,VCL已经为您提供了DropDownWidthDropDownCount属性,用于这些消息的接口:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.DropDownWidth := 200;
  ComboBox1.DropDownCount := 50;
end;

因此,您不需要显式发送任何消息。

如果在您的特定应用程序中不起作用,请注意您的应用程序可能存在一些奇怪的问题,我们无法看到。

导致VCL GUI故障的一个常见原因是程序员使用VCL样式(或主题)来赋予应用程序非标准外观。

英文:

The CB_SETDROPPEDWIDTH message is used to set the width of the drop down list:

> An application sends the CB_SETDROPPEDWIDTH message to set the minimum allowable width, in pixels, of the list box of a combo box with the CBS_DROPDOWN or CBS_DROPDOWNLIST style.

Hence, it will not affect the height.

To set the height, you use the CB_SETMINVISIBLE message:

> Sets the minimum number of visible items in the drop-down list of a combo box.

For example,

procedure TForm1.FormCreate(Sender: TObject);
begin
  SendMessage(ComboBox1.Handle, CB_SETDROPPEDWIDTH, 200, 0);
  SendMessage(ComboBox1.Handle, CB_SETMINVISIBLE, 50, 0);
end;

Please note that the width unit is pixels, while the height unit is items.

Delphi VCL自定义TCombobox下拉宽度

However, the VCL already provides you with an interface to these messages in its DropDownWidth and DropDownCount properties:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.DropDownWidth := 200;
  ComboBox1.DropDownCount := 50;
end;

So you don't need to send any messages explicitly yourself.

If this doesn't work for you in your particular application, then you have some strange things going on in this app, which we cannot see.

One very common cause of malfunctioning VCL GUIs is that the programmer uses VCL styles (or themes) to give the app a non-standard appearance.

huangapple
  • 本文由 发表于 2023年5月22日 13:35:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定