简体   繁体   中英

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? Can I use the OnPaint event?

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. Note however, that this is not depending on TStyleBook nor any of the style files supplied with Delphi Firemonkey.

Place a TRectangle as a background for the TListBox . Set its Fill - Color property to a color you like. (I used "Cornsilk" in the example).

Place the TListBox on the rectangle as a child of the rectangle. In the "Object Inspector" locate the StyleLookup property and change its value to 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. 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:

  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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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