TVirtualDrawTree - 如何将节点排成一行?
TVirtualDrawTree - How to place nodes in one row?
这是我另一个问题 的跟进。
因为我在评论中被建议提出关于这个主题的新问题
有人建议我在 Row 中绘制不同的图像。我从一开始的目标就是并排插入节点,有人告诉我这不能用 VDT 完成,它不是为此目的而制作的。但是是什么让我确定有办法,因为我看到一个在线项目使用相同的 VDT
这里是这个项目的屏幕截图
通过使用像 PE 资源管理器这样的资源查看器,我发现了这种形式的数据
object VDT: TVirtualDrawTree
AlignWithMargins = True
Left = 5
Top = 5
Width = 457
Height = 227
Margins.Left = 5
Margins.Top = 5
Margins.Right = 5
Margins.Bottom = 5
Align = alClient
BevelInner = bvNone
BevelOuter = bvNone
DefaultNodeHeight = 55
Header.AutoSizeIndex = 0
Header.Font.Charset = DEFAULT_CHARSET
Header.Font.Color = clWindowText
Header.Font.Height = -11
Header.Font.Name = 'Tahoma'
Header.Font.Style = []
HotCursor = crHandPoint
TabOrder = 0
TreeOptions.PaintOptions = [toHideFocusRect, toHideSelection, toHotTrack, toShowButtons, toShowDropmark, toThemeAware, toUseBlendedImages, toAlwaysHideSelection, toUseBlendedSelection]
TreeOptions.SelectionOptions = [toExtendedFocus, toMiddleClickSelect, toRightClickSelect]
OnBeforeCellPaint = VDTBeforeCellPaint
OnGetNodeWidth = VDTGetNodeWidth
OnMouseUp = VDTMouseUp
ExplicitLeft = 3
ExplicitTop = 3
Columns = <
item
Position = 0
Width = 54
WideText = '55'
end
item
Position = 1
Width = 54
WideText = '55'
end
item
Position = 2
Width = 54
WideText = '55'
end
item
Position = 3
Width = 54
WideText = '55'
end
item
Position = 4
Width = 54
WideText = '55'
end
item
Position = 5
Width = 54
WideText = '55'
end
item
Position = 6
Width = 54
WideText = '55'
end
item
Position = 7
Width = 54
WideText = '55'
end>
end
end
所以我告诉自己我必须使用 Tviruaildrawtree
来达到相同的目标然后我开始创建数据
type
TAnmiClass = class
private
Fanmigraphic : TGifImage;
public
property anmigraphic: TGifImage read Fanmigraphic write Fanmigraphic;
public
constructor Create;
destructor Destroy; override;
end;
type
PAnimeData = ^TAnimeData;
TAnimeData = record
FObject: TAnmiClass;
end;
因为我想我必须为节点创建图像对象,因为我将从 url 下载一些图像列表,然后将它们添加到节点,如下所示,所以下面的代码是从下载图像stringlist 到桌面,然后将其加载到节点 Tgifimage
For i := 0 To animationimages.Count-1 do
begin
Animaturl := animationimages.Strings[i];
URI := TIdURI.Create(Animaturl);
try
ImageName := URI.Document;
finally
FreeAndNil(URI);
end;
if (ExtractFileExt(ImageName) = '.gif') then
begin
addanimation(Animaturl);
end;
end;
procedure TForm2.addanimation(AAnimationUrl: String);
var
AnmiClass: TAnmiClass;
path: string;
begin
VDTAni.BeginUpdate;
try
AnmiClass := TAnmiClass.Create;
path := AAnimationUrl;
if fileexists(path) then
begin
AnmiClass.anmigraphic.LoadFromFile(path);
AnmiClass.anmigraphic.Animate := True;
AnmiClass.anmigraphic.Transparent := True;
end;
AddAnmiToVD(VDTAni, nil, AnmiClass);
finally
VDTAni.EndUpdate;
end;
这里是我如何在 VDT 中绘制节点
procedure TForm2.VDTAniBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: PAnimeData;
NewRect: TRect;
R: TRect;
begin
//
if not Assigned(Node) then
begin
exit;
end;
Data := VDTAni.GetNodeData(Node);
case Column of
0 :
begin
NewRect := ContentRect;
NewRect.Left := NewRect.Left +2;
NewRect.Width := 55;
NewRect.Height := 55;
NewRect.Top := NewRect.Top + 2;
NewRect.Bottom := NewRect.Bottom;
TargetCanvas.StretchDraw( NewRect, Data.FObject.anmigraphic);
end;
end;
end;
但是我不能像上面显示的那样排列节点
而且它似乎不能在 onbeforecellpanit
中制作。
在我的另一个问题中 Tom Brunberg 建议将图像分成 10 个节点,如果添加的图像是 80 并且每行需要 8 个,例如每行有 8 个图像和每个图像显示在自己的列中。但我不知道如何在编码中做到这一点,也不知道从哪里开始。
当前代码的问题
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VirtualTrees, Gifimg, Vcl.StdCtrls,
Vcl.Grids, Vcl.ExtCtrls;
type
TImageOBJArr = array of TGifimage;
type
TaniDataclass = class
ImageOBJArr: TImageOBJArr;
private
FAnirefrence: String;
FAniIMage: TGifimage;
public
property Anirefrence: String read FAnirefrence write FAnirefrence;
property AniIMage: TGifImage read FAniIMage write FAniIMage;
public
constructor Create;
destructor Destroy; override;
end;
type
Panidata = ^Tanidata;
Tanidata = record
FObject: TaniDataclass;
end;
type
TForm1 = class(TForm)
VDTani: TVirtualStringTree;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure VDTaniBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
procedure FormCreate(Sender: TObject);
procedure VDTaniFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
procedure VDTaniGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
private
{ Private declarations }
ImageOBJArr: TImageOBJArr; // Main storage of images
public
{ Public declarations }
Dimagelist : Tstringlist;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TaniDataclass }
constructor TaniDataclass.Create;
begin
FAniIMage := TGifImage.Create;
end;
destructor TaniDataclass.Destroy;
begin
FAniIMage.Free;
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Node: PVirtualNode;
Data: Panidata;
i, row, col: integer;
fn: String;
begin
// Load images to main store ImgArr
SetLength(ImageOBJArr, Dimagelist.Count);
for i := 0 to Dimagelist.Count -1 do
begin
fn := Dimagelist[I];
ImageOBJArr[i] := TGifimage.Create;
ImageOBJArr[i].LoadFromFile(fn);
end;
// Setup vdt nodes and assign images eight in a row
// hardcoded for now. You may want to add dynamics
// for varying window and image sizes
row := 0;
while row <= (Dimagelist.Count div 8) do
begin
Node := VDTani.AddChild(nil);
Data := VDTani.GetNodeData(Node);
SetLength(Data.FObject.ImageOBJArr, 8);
for col := 0 to 7 do
Data.FObject.ImageOBJArr[col] := ImageOBJArr[row * 8 + col];
inc(row);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Dimagelist := Tstringlist.Create;
VDtAni.NodeDataSize := SizeOf(Tanidata);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(Dimagelist) then
begin
FreeAndNil(Dimagelist);
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Dimagelist.Add('1mm.gif');
Dimagelist.Add('2mm.gif');
Dimagelist.Add('3mm.gif');
Dimagelist.Add('4mm.gif');
Dimagelist.Add('5mm.gif');
Dimagelist.Add('6mm.gif');
end;
procedure TForm1.VDTaniBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: Panidata;
begin
if not Assigned(Node) then
begin
exit;
end;
Data := VDTani.GetNodeData(Node);
Sender.NodeHeight[Node] := 54;
CellRect.Height := 54;
TargetCanvas.StretchDraw( CellRect, Data.FObject.ImageOBJArr[Column]);
end;
procedure TForm1.VDTaniFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
Data: Panidata;
begin
Data := VDTani.GetNodeData(Node);
if Assigned(Data) then
Data.FObject.Free;
end;
procedure TForm1.VDTaniGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
begin
NodeDataSize := SizeOf(Tanidata);
end;
end.
我在以下代码中遇到异常,我向字符串列表添加了 6 个图像路径,然后尝试在每一列上绘制
row := 0;
while row <= (Dimagelist.Count div 2) do
begin
Node := VDTani.AddChild(nil);
Data := VDTani.GetNodeData(Node);
SetLength(Data.FObject.ImageOBJArr, 2);
for col := 0 to 7 do
Data.FObject.ImageOBJArr[col] := ImageOBJArr[row * 2 + col];
inc(row);
end;
这是我建议的实现。
type
TImgArr = array of TBitmap;
TVdtData = record
FObject: TimgArr;
end;
PVdtData = ^TVdtData;
TForm2 = class(TForm)
Vdt: TVirtualDrawTree;
...
private
{ Private declarations }
ImgArr: TImgArr; // Main storage of images
implementation
procedure TForm2.Button1Click(Sender: TObject);
var
Node: PVirtualNode;
Data: PVdtData;
p: pointer;
i, row, col: integer;
fn: TFileName;
begin
// Load images to main store ImgArr
SetLength(ImgArr, 100);
for i := 0 to 99 do
begin
fn := Format('c:\tmp\nums\%.2d.bmp',[i]);
ImgArr[i] := TBitmap.Create;
ImgArr[i].LoadFromFile(fn);
end;
// Setup vdt nodes and assign images eight in a row
// hardcoded for now. You may want to add dynamics
// for varying window and image sizes
row := 0;
while row <= (100 div 8) do
begin
Node := Vdt.AddChild(nil);
p := Node.GetData;
Data := Vdt.GetNodeData(Node);
// SetLength(Data.FObject, 8);
SetLength(Data.FObject, Vdt.Header.Columns.Count);
for col := 0 to 7 do
Data.FObject[col] := ImgArr[row * 8 + col];
inc(row);
end;
end;
procedure TForm2.VdtBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: PVdtData;
begin
if not Assigned(Node) then exit;
Data := Vdt.GetNodeData(Node);
Sender.NodeHeight[Node] := 64;
CellRect.Height := 64;
if Assigned(Data.FObject[Column]) then
TargetCanvas.StretchDraw( CellRect, Data.FObject[Column]);
end;
不保证不携带错误。
结果
但是说真的,正如其他人指出的那样,只使用 TDrawGrid
或 TStringGrid
会容易得多。当然是你的决定。
这是我另一个问题
因为我在评论中被建议提出关于这个主题的新问题
有人建议我在 Row 中绘制不同的图像。我从一开始的目标就是并排插入节点,有人告诉我这不能用 VDT 完成,它不是为此目的而制作的。但是是什么让我确定有办法,因为我看到一个在线项目使用相同的 VDT
这里是这个项目的屏幕截图
通过使用像 PE 资源管理器这样的资源查看器,我发现了这种形式的数据
object VDT: TVirtualDrawTree
AlignWithMargins = True
Left = 5
Top = 5
Width = 457
Height = 227
Margins.Left = 5
Margins.Top = 5
Margins.Right = 5
Margins.Bottom = 5
Align = alClient
BevelInner = bvNone
BevelOuter = bvNone
DefaultNodeHeight = 55
Header.AutoSizeIndex = 0
Header.Font.Charset = DEFAULT_CHARSET
Header.Font.Color = clWindowText
Header.Font.Height = -11
Header.Font.Name = 'Tahoma'
Header.Font.Style = []
HotCursor = crHandPoint
TabOrder = 0
TreeOptions.PaintOptions = [toHideFocusRect, toHideSelection, toHotTrack, toShowButtons, toShowDropmark, toThemeAware, toUseBlendedImages, toAlwaysHideSelection, toUseBlendedSelection]
TreeOptions.SelectionOptions = [toExtendedFocus, toMiddleClickSelect, toRightClickSelect]
OnBeforeCellPaint = VDTBeforeCellPaint
OnGetNodeWidth = VDTGetNodeWidth
OnMouseUp = VDTMouseUp
ExplicitLeft = 3
ExplicitTop = 3
Columns = <
item
Position = 0
Width = 54
WideText = '55'
end
item
Position = 1
Width = 54
WideText = '55'
end
item
Position = 2
Width = 54
WideText = '55'
end
item
Position = 3
Width = 54
WideText = '55'
end
item
Position = 4
Width = 54
WideText = '55'
end
item
Position = 5
Width = 54
WideText = '55'
end
item
Position = 6
Width = 54
WideText = '55'
end
item
Position = 7
Width = 54
WideText = '55'
end>
end
end
所以我告诉自己我必须使用 Tviruaildrawtree
来达到相同的目标然后我开始创建数据
type
TAnmiClass = class
private
Fanmigraphic : TGifImage;
public
property anmigraphic: TGifImage read Fanmigraphic write Fanmigraphic;
public
constructor Create;
destructor Destroy; override;
end;
type
PAnimeData = ^TAnimeData;
TAnimeData = record
FObject: TAnmiClass;
end;
因为我想我必须为节点创建图像对象,因为我将从 url 下载一些图像列表,然后将它们添加到节点,如下所示,所以下面的代码是从下载图像stringlist 到桌面,然后将其加载到节点 Tgifimage
For i := 0 To animationimages.Count-1 do
begin
Animaturl := animationimages.Strings[i];
URI := TIdURI.Create(Animaturl);
try
ImageName := URI.Document;
finally
FreeAndNil(URI);
end;
if (ExtractFileExt(ImageName) = '.gif') then
begin
addanimation(Animaturl);
end;
end;
procedure TForm2.addanimation(AAnimationUrl: String);
var
AnmiClass: TAnmiClass;
path: string;
begin
VDTAni.BeginUpdate;
try
AnmiClass := TAnmiClass.Create;
path := AAnimationUrl;
if fileexists(path) then
begin
AnmiClass.anmigraphic.LoadFromFile(path);
AnmiClass.anmigraphic.Animate := True;
AnmiClass.anmigraphic.Transparent := True;
end;
AddAnmiToVD(VDTAni, nil, AnmiClass);
finally
VDTAni.EndUpdate;
end;
这里是我如何在 VDT 中绘制节点
procedure TForm2.VDTAniBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: PAnimeData;
NewRect: TRect;
R: TRect;
begin
//
if not Assigned(Node) then
begin
exit;
end;
Data := VDTAni.GetNodeData(Node);
case Column of
0 :
begin
NewRect := ContentRect;
NewRect.Left := NewRect.Left +2;
NewRect.Width := 55;
NewRect.Height := 55;
NewRect.Top := NewRect.Top + 2;
NewRect.Bottom := NewRect.Bottom;
TargetCanvas.StretchDraw( NewRect, Data.FObject.anmigraphic);
end;
end;
end;
但是我不能像上面显示的那样排列节点
而且它似乎不能在 onbeforecellpanit
中制作。
在我的另一个问题中 Tom Brunberg 建议将图像分成 10 个节点,如果添加的图像是 80 并且每行需要 8 个,例如每行有 8 个图像和每个图像显示在自己的列中。但我不知道如何在编码中做到这一点,也不知道从哪里开始。
当前代码的问题
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VirtualTrees, Gifimg, Vcl.StdCtrls,
Vcl.Grids, Vcl.ExtCtrls;
type
TImageOBJArr = array of TGifimage;
type
TaniDataclass = class
ImageOBJArr: TImageOBJArr;
private
FAnirefrence: String;
FAniIMage: TGifimage;
public
property Anirefrence: String read FAnirefrence write FAnirefrence;
property AniIMage: TGifImage read FAniIMage write FAniIMage;
public
constructor Create;
destructor Destroy; override;
end;
type
Panidata = ^Tanidata;
Tanidata = record
FObject: TaniDataclass;
end;
type
TForm1 = class(TForm)
VDTani: TVirtualStringTree;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure VDTaniBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
procedure FormCreate(Sender: TObject);
procedure VDTaniFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
procedure VDTaniGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
private
{ Private declarations }
ImageOBJArr: TImageOBJArr; // Main storage of images
public
{ Public declarations }
Dimagelist : Tstringlist;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TaniDataclass }
constructor TaniDataclass.Create;
begin
FAniIMage := TGifImage.Create;
end;
destructor TaniDataclass.Destroy;
begin
FAniIMage.Free;
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Node: PVirtualNode;
Data: Panidata;
i, row, col: integer;
fn: String;
begin
// Load images to main store ImgArr
SetLength(ImageOBJArr, Dimagelist.Count);
for i := 0 to Dimagelist.Count -1 do
begin
fn := Dimagelist[I];
ImageOBJArr[i] := TGifimage.Create;
ImageOBJArr[i].LoadFromFile(fn);
end;
// Setup vdt nodes and assign images eight in a row
// hardcoded for now. You may want to add dynamics
// for varying window and image sizes
row := 0;
while row <= (Dimagelist.Count div 8) do
begin
Node := VDTani.AddChild(nil);
Data := VDTani.GetNodeData(Node);
SetLength(Data.FObject.ImageOBJArr, 8);
for col := 0 to 7 do
Data.FObject.ImageOBJArr[col] := ImageOBJArr[row * 8 + col];
inc(row);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Dimagelist := Tstringlist.Create;
VDtAni.NodeDataSize := SizeOf(Tanidata);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(Dimagelist) then
begin
FreeAndNil(Dimagelist);
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Dimagelist.Add('1mm.gif');
Dimagelist.Add('2mm.gif');
Dimagelist.Add('3mm.gif');
Dimagelist.Add('4mm.gif');
Dimagelist.Add('5mm.gif');
Dimagelist.Add('6mm.gif');
end;
procedure TForm1.VDTaniBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: Panidata;
begin
if not Assigned(Node) then
begin
exit;
end;
Data := VDTani.GetNodeData(Node);
Sender.NodeHeight[Node] := 54;
CellRect.Height := 54;
TargetCanvas.StretchDraw( CellRect, Data.FObject.ImageOBJArr[Column]);
end;
procedure TForm1.VDTaniFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
Data: Panidata;
begin
Data := VDTani.GetNodeData(Node);
if Assigned(Data) then
Data.FObject.Free;
end;
procedure TForm1.VDTaniGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
begin
NodeDataSize := SizeOf(Tanidata);
end;
end.
我在以下代码中遇到异常,我向字符串列表添加了 6 个图像路径,然后尝试在每一列上绘制
row := 0;
while row <= (Dimagelist.Count div 2) do
begin
Node := VDTani.AddChild(nil);
Data := VDTani.GetNodeData(Node);
SetLength(Data.FObject.ImageOBJArr, 2);
for col := 0 to 7 do
Data.FObject.ImageOBJArr[col] := ImageOBJArr[row * 2 + col];
inc(row);
end;
这是我建议的实现。
type
TImgArr = array of TBitmap;
TVdtData = record
FObject: TimgArr;
end;
PVdtData = ^TVdtData;
TForm2 = class(TForm)
Vdt: TVirtualDrawTree;
...
private
{ Private declarations }
ImgArr: TImgArr; // Main storage of images
implementation
procedure TForm2.Button1Click(Sender: TObject);
var
Node: PVirtualNode;
Data: PVdtData;
p: pointer;
i, row, col: integer;
fn: TFileName;
begin
// Load images to main store ImgArr
SetLength(ImgArr, 100);
for i := 0 to 99 do
begin
fn := Format('c:\tmp\nums\%.2d.bmp',[i]);
ImgArr[i] := TBitmap.Create;
ImgArr[i].LoadFromFile(fn);
end;
// Setup vdt nodes and assign images eight in a row
// hardcoded for now. You may want to add dynamics
// for varying window and image sizes
row := 0;
while row <= (100 div 8) do
begin
Node := Vdt.AddChild(nil);
p := Node.GetData;
Data := Vdt.GetNodeData(Node);
// SetLength(Data.FObject, 8);
SetLength(Data.FObject, Vdt.Header.Columns.Count);
for col := 0 to 7 do
Data.FObject[col] := ImgArr[row * 8 + col];
inc(row);
end;
end;
procedure TForm2.VdtBeforeCellPaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect);
var
Data: PVdtData;
begin
if not Assigned(Node) then exit;
Data := Vdt.GetNodeData(Node);
Sender.NodeHeight[Node] := 64;
CellRect.Height := 64;
if Assigned(Data.FObject[Column]) then
TargetCanvas.StretchDraw( CellRect, Data.FObject[Column]);
end;
不保证不携带错误。
结果
但是说真的,正如其他人指出的那样,只使用 TDrawGrid
或 TStringGrid
会容易得多。当然是你的决定。