简体   繁体   English

Firemonkey TListBox 在运行时改变背景颜色

[英]Firemonkey TListBox changing background color at runtime

I there a way, at runtime, other than using styles, to change the background color of a TListBox?除了使用 styles,我有办法在运行时更改 TListBox 的背景颜色吗? Can I use the OnPaint event?我可以使用 OnPaint 事件吗?

Because the TListbox doesn't have a property to change the background color, I can only think of the following, which is based on combining two components, of which one (the TListBox ) uses a built-in style.因为TListbox没有更改背景颜色的属性,所以我只能想到以下内容,它基于组合两个组件,其中一个( TListBox )使用内置样式。 Note however, that this is not depending on TStyleBook nor any of the style files supplied with Delphi Firemonkey.但是请注意,这不依赖于TStyleBook或 Delphi Firemonkey 提供的任何样式文件。

Place a TRectangle as a background for the TListBox .放置一个TRectangle作为TListBox的背景。 Set its Fill - Color property to a color you like.将其Fill - Color属性设置为您喜欢的颜色。 (I used "Cornsilk" in the example). (我在示例中使用了“Cornsilk”)。

Place the TListBox on the rectangle as a child of the rectangle.TListBox作为矩形的子元素放置在矩形上。 In the "Object Inspector" locate the StyleLookup property and change its value to transparentlistboxstyle .在“对象检查器”中找到StyleLookup属性并将其值更改为transparentlistboxstyle This makes the listbox transparent and the rectangle and its fill color to shine through.这使得列表框透明,矩形及其填充颜色闪耀。

If you make the TListBox one pixel smaller than the rectangle on each side, you can use the Sides property to provide a thin frame around the listbox.如果使TListBox每边都比矩形小一个像素,则可以使用Sides属性在列表框周围提供一个细框。 Or you can choose to make them equally sized and not show any frame.或者您可以选择使它们大小相同而不显示任何框架。

My test result looks like this:我的测试结果如下所示:

在此处输入图像描述

The TRectangle and the TListbox properties from the .fmx file: .fmx文件中的TRectangleTListbox属性:

  object Rectangle1: TRectangle
    Anchors = [akLeft, akTop, akBottom]
    Fill.Color = claCornsilk
    Position.X = 7.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 361.000000000000000000
    Size.Height = 219.000000000000000000
    Size.PlatformDefault = False
    object ListBox1: TListBox
      Anchors = [akLeft, akTop, akRight, akBottom]
      Position.X = 1.000000000000000000
      Position.Y = 1.000000000000000000
      Size.Width = 359.000000000000000000
      Size.Height = 217.000000000000000000
      Size.PlatformDefault = False
      StyleLookup = 'transparentlistboxstyle'
      TabOrder = 0
      ParentShowHint = False
      ShowHint = False
      DisableFocusEffect = True
      ItemHeight = 48.000000000000000000
      DefaultItemStyles.ItemStyle = 'listboxitemrightdetail'
      DefaultItemStyles.GroupHeaderStyle = ''
      DefaultItemStyles.GroupFooterStyle = ''
      Viewport.Width = 359.000000000000000000
      Viewport.Height = 217.000000000000000000
    end
  end

To change the color of ListBox1 , you actually change the color of the TRectangle :要更改ListBox1的颜色,您实际上更改了TRectangle的颜色:

procedure TForm5.ColorListBox1ItemClick(const Sender: TCustomListBox;
  const Item: TListBoxItem);
begin
  Rectangle1.Fill.Color := TColorListBox(Sender).Color;
end;

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

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