You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi 11.2 FireMonkey中如何修改ComboBox下拉项选中背景色?

修改Delphi 11.2 FireMonkey ComboBox下拉选中项背景色

方法一:通过Style Designer可视化修改

  • 右键目标ComboBox,选择Edit Custom Style打开样式设计器
  • 在样式树中定位到TListBoxItemStyle(ComboBox下拉列表本质是TListBox,列表项默认使用这个样式)
  • 展开TListBoxItemStyle,找到名为Background的TRectangle控件
  • 修改该矩形的Fill.Color属性为你需要的颜色(比如输入十六进制色值$FF2ECC71设置为绿色)
  • 点击样式设计器的Apply and Close,保存修改后的样式到项目中

方法二:代码动态修改

如果不想依赖StyleBook,可在运行时直接修改样式资源:

procedure TMainForm.FormCreate(Sender: TObject);
var
  LListBox: TListBox;
  LItemStyle: TListBoxItemStyle;
  LBackgroundRect: TRectangle;
  LTextControl: TText;
begin
  // 获取ComboBox关联的下拉列表控件
  LListBox := ComboBox1.ListBox;
  if not Assigned(LListBox) then Exit;

  // 获取列表项的默认样式模板
  LItemStyle := LListBox.ItemStyle as TListBoxItemStyle;
  if not Assigned(LItemStyle) then Exit;

  // 修改选中项背景色
  LBackgroundRect := LItemStyle.FindStyleResource('Background') as TRectangle;
  if Assigned(LBackgroundRect) then
    LBackgroundRect.Fill.Color := $FF2ECC71; // 替换为你的目标颜色

  // 可选:同时修改选中项的文本颜色
  LTextControl := LItemStyle.FindStyleResource('Text') as TText;
  if Assigned(LTextControl) then
    LTextControl.Color := $FFFFFFFF; // 白色文本配合深色背景
end;

注意:如果你的ComboBox使用了自定义样式,需要确认样式资源的名称是否与默认一致,调整代码中的查找名称即可。

内容的提问来源于stack exchange,提问作者Gustavo Galvão

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 21:30:55