Подтвердить что ты не робот

Можно ли изменить цвет строки в виртуальном дереве строк?

Я хочу изменить цвет текста в определенной строке виртуального дерева строк. возможно ли это?

4b9b3361

Ответ 1

Использовать событие OnBeforeCellPaint:

procedure TForm1.VirtualStringTree1BeforeCellPaint(Sender: TBaseVirtualTree;
  TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
begin
  if Node.Index mod 2 = 0 then
  begin
    TargetCanvas.Brush.Color := clFuchsia;
    TargetCanvas.FillRect(CellRect);
  end;
end;

Это изменит фон на каждой другой строке (если строки находятся на одном уровне).

Ответ 2

Чтобы управлять цветом текста в определенной строке, используйте событие OnPaintText и установите TargetCanvas.Font.Color.

procedure TForm.TreePaintText(Sender: TBaseVirtualTree; const TargetCanvas: 
  TCanvas; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType);
var
  YourRecord: PYourRecord;

begin
  YourRecord := Sender.GetNodeData(Node);

  // an example for checking the content of a specific record field
  if YourRecord.Text = 'SampleText' then 
    TargetCanvas.Font.Color := clRed;
end;

Обратите внимание, что этот метод вызывается для каждой ячейки в TreeView. Указатель Node одинаковый в каждой ячейке строки. Поэтому, если у вас есть несколько столбцов и вы хотите установить цвет для целой строки, относящейся к содержимому определенного столбца, вы можете использовать заданный Node, как в примере кода.

Ответ 3

Чтобы изменить цвет текста в определенной строке, можно использовать событие OnDrawText, в котором вы изменяете текущее свойство TargetCanvas.Font.Color.

Нижеприведенный код работает с Delphi XE 1 и виртуальным деревом 5.5.2 (http://virtual-treeview.googlecode.com/svn/branches/V5_stable/)

type
  TFileVirtualNode = packed record
    filePath: String;
    exists: Boolean;
  end;

  PTFileVirtualNode  = ^TFileVirtualNode ;

procedure TForm.TVirtualStringTree_OnDrawText(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
  Column: TColumnIndex; const Text: UnicodeString; const CellRect: TRect; var DefaultDraw: Boolean);
var
  pileVirtualNode: PTFileVirtualNode;
begin
  pileVirtualNode:= Sender.GetNodeData(Node);

  if not pileVirtualNode^.exists then 
  begin
    TargetCanvas.Font.Color := clGrayText;
  end;
end;