在运行时的ListView色彩项目 [英] ListView color items at runtime

查看:146
本文介绍了在运行时的ListView色彩项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以设置自定义颜色的项目,当我将它们添加到使用OnDraw的事件列表中,但我想在某一个点来改变物品的颜色,他们已经在列表中了。

I know that i can set custom colors to items when i add them to the list using OnDraw Events but i want to change colors of the items at a certain point after they are already in the list.

有没有办法做到这一点?

Is there a way to do this ?

推荐答案

要重绘只有某些项目使用<$c$c>UpdateItems方法。它有两个输入参数,你可以指定要重绘项目的范围。如果你要重绘只有一个项目,则只需指定一个项目指标为范围。

To redraw only certain items use the UpdateItems method. It has two input parameters where you can specify the range of the items to be redrawn. If you are going to redraw only one item, then just specify that one item index as a range.

在这个例子中,我存储项目的颜色到<一个href=\"http://docwiki.embarcadero.com/Libraries/en/Vcl.ComCtrls.TListItem.Data\"><$c$c>TListItem.Data财产和褪色这种颜色在计时器的事件。更改该值后我称之为<一个href=\"http://docwiki.embarcadero.com/Libraries/en/Vcl.ComCtrls.TCustomListView.UpdateItems\"><$c$c>UpdateItems函数强制平局项事件触发。是的,没有<一个href=\"http://docwiki.embarcadero.com/Libraries/en/Vcl.ComCtrls.TListView.DoubleBuffered\"><$c$c>DoubleBuffered集,它闪烁(即使你如设置定时器的间隔500毫秒)。

In this example I'm storing the color of the item into the TListItem.Data property and fading this color in the timer's event. After changing the value I call the UpdateItems function which force the draw item event to fire. And yes, without DoubleBuffered set, it flickers (even when you set the timer's interval e.g. to 500ms).

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListView1.AddItem('Item 1', TObject(clWhite));
  ListView1.AddItem('Item 2', TObject(clWhite));
  ListView1.AddItem('Item 3', TObject(clWhite));
  Timer1.Enabled := True;
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  ListView1.Canvas.Brush.Color := TColor(Item.Data);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  C: Byte;
  I: TColor;

  procedure ChangeItemColor;
  begin
    I := TColor(ListView1.Items[0].Data);
    C := GetRValue(I);
    if C < 150 then C := 255 else Dec(C);
    I := RGB(C, C, C);
    ListView1.Items[0].Data := TObject(I);
  end;

begin
  // color change
  ChangeItemColor;
  // repaint of the item with index 1
  ListView1.UpdateItems(1, 1);
end;

这篇关于在运行时的ListView色彩项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆