【问题标题】:add small icon to virtualtreeview将小图标添加到 virtualtreeview
【发布时间】:2023-03-20 02:49:02
【问题描述】:

我正在尝试在 delphi2010 中向 VirtualTreeview 添加小图标 我使用属性图像将 ImageList 附加到 VirtualTreeview

procedure TMainFrm.VSTGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
var
  FileInfo: PFileInfoRec;
begin
  if Kind in [ikNormal , ikSelected] then
  begin
    if Column = 0 then
    ImageIndex :=ImageList1.AddIcon(FileInfo.FileIco);
  end;
end;

但添加图标后看起来太暗了:

FileInfo Strucutre (Record with methods) 当我加载文件时填充 我需要的只是将 fileico 从 fileinfo 添加到 imagelist 并显示在树视图中

type
  PFileInfoRec= ^TFileInfoRec;
  TFileInfoRec = record
  strict private
    vFullPath: string;
      .
      .
      .
    vFileIco : TIcon;
  public
    constructor Create(const FilePath: string);
    property FullPath: string read vFullPath;
      .
      .
      .
    property FileIco : TIcon  read vFileIco;
  end;

构造函数:

constructor TFileInfoRec.Create(const FilePath: string);
var
  FileInfo: SHFILEINFO;
begin
  vFullPath := FilePath;
    .
    .
    .
  vFileIco        := TIcon.Create;
  vFileIco.Handle := FileInfo.hIcon;
//  vFileIco.Free;
end;

那么问题在哪里? !谢谢

【问题讨论】:

  • 看起来像部分透明度的问题。或许你需要将图片列表ColorDepth设置为cd32Bit
  • @DavidHeffernan 谢谢,但还是不行
  • 好的,你到底做了什么?您究竟是什么时候更改了ColorDepth?创建图像列表后立即。另外,您的真实代码是什么样的?大概真正的代码分配FileInfo。如果真实代码这样做,那么您发布虚假代码会令人失望。如果这是您的真实代码,那么不给 FileInfo 分配任何东西显然是个问题。
  • 并且你不能每次树视图请求图像索引时都添加一个新图标。添加一次图标,每次返回相同的图片索引。
  • 大卫说得对,在使用 Virtual TreeView 之前,您必须了解虚拟范式。

标签: delphi icons virtualtreeview


【解决方案1】:

让我们有一个图像列表ImageList1 并将其分配给VirtualStringTree1.Images 属性。然后加入之前的评论者,在你使用FileInfo 之前,给它分配一些东西,比如:FileInfo := Sender.GetNodeData(Node),而不是你可以使用FileInfo.FileIco。但是您应该将您的图标添加到图像列表而不是OnGetImageIndex。您应该在 OnInitNode 中执行此操作(如果您遵循虚拟范例,您应该做什么),而不是将添加的图标的索引存储在 FileInfo 中。示例:

procedure TForm1.VirtualStringTree1InitNode(Sender: TBaseVirtualTree;
  ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
var
  FileInfo: PFileInfoRec;
begin
  FileInfo := Sender.GetNodeData(Node);
  //...
  FileInfo.FileIcoIndex := ImageList1.AddIcon(FileInfo.FileIco);

end;

onGetImageIndex:

procedure TMainFrm.VSTGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
var
  FileInfo: PFileInfoRec;
begin
  FileInfo := Sender.GetNodeData(Node);
  if Kind in [ikNormal , ikSelected] then
  begin
    if Column = 0 then
    ImageIndex :=FileInfo.FileIcoIndex;
  end;
end;

如果还不够,请发布更多示例代码,让我们了解您的问题。

【讨论】:

  • @S.FATEH 您已经接受了您的问题的答案。您添加的代码对我们没有任何改变。
猜你喜欢
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
相关资源
最近更新 更多