TTreeNode闪烁。我怎么做到的?

时间:2011-07-14 09:05:53

标签: delphi treeview

我喜欢闪烁的节点。我该怎么做?

2 个答案:

答案 0 :(得分:4)

以下内容绝不是一个完整的解决方案,而是大卫答案的一个简单例子:

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Timer1: TTimer;
    Button1: TButton;
    Button2: TButton;
    procedure TreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
      State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FHighTime: Boolean;
    FHighNode: TTreeNode;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
var
  R: TRect;
begin
  if Assigned(FHighNode) then begin
    FHighTime := not FHighTime;
    FHighNode.MakeVisible;
    R := FHighNode.DisplayRect(True);
    InvalidateRect(TreeView1.Handle, @R, True);
  end else begin
    if FHighTime then
      TreeView1.Invalidate;
    FHighTime := False;
    Timer1.Enabled := False;
  end;
end;

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
const
  Colors: array [Boolean] of TColor = (clWindowText, clWindow);
  HiColors: array [Boolean] of TColor = (clHighlightText, clHighlight);
begin
  if Node = FHighNode then begin
    if cdsSelected in State then
      TreeView1.Canvas.Font.Color := HiColors[FHighTime]
    else
      TreeView1.Canvas.Font.Color := Colors[FHighTime];
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  // test
  FHighNode := TreeView1.Items[2];
  Timer1.Enabled := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FHighNode := nil;
end;

您可能希望更改设计,以便设置突出显示节点就像调用setter等。但无论如何,您明白了......

答案 1 :(得分:1)

实现这一目标并没有内置于通用控件中,所以你需要自己动手。

使用TTimer并在每次触发时,更改所需的节点属性以实现闪烁效果。