【问题标题】:How a Combobox with the csOwnerDrawFixed Style can behave like the csDropDown style?具有 csOwnerDrawFixed 样式的 Combobox 如何表现得像 csDropDown 样式?
【发布时间】:2012-04-05 13:53:29
【问题描述】:
我正在使用 TComboBox 组件,其样式属性设置为 csOwnerDrawFixed,我实现了 OnDrawItem 一切正常,现在我想要哪个组合框表现得就像使用 csDropDown 样式时一样(使用 csOwnerDrawFixed 样式的行为类似于 csDropDownList 样式),我的意思是使用内部编辑器。这可能吗?
【问题讨论】:
标签:
delphi
delphi-xe
ownerdrawn
tcombobox
【解决方案1】:
Delphi 的 TComboBox 包装器不支持所有者绘制可编辑样式,但底层 Windows 控件支持,并且很容易启用它。
像这样创建一个新的后代类:
TComboBox = class(StdCtrls.TComboBox)
public
procedure CreateParams(var Params: TCreateParams); override;
end;
procedure TComboBox.CreateParams(var Params: TCreateParams);
begin
inherited;
if Assigned(OnDrawItem) then
Params.Style := Params.Style or CBS_OWNERDRAWFIXED
end;
将Style 设置为csDropDown 并分配OnDrawItem,就像你已经在做的那样。
【解决方案2】:
所有 OwnerDraw 样式都不支持在 TComboBox 中存在编辑框。您将不得不使用单独的TEdit。