virtualtreeview添加图标失败 [英] virtualtreeview add icon fail

查看:243
本文介绍了virtualtreeview添加图标失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我向virtualtreeview添加文件名及其相关图标的简单代码

here my simple code to add filename and their associated icon to virtualtreeview

  PFileInfoRec = ^TFileInfoRec;
  TFileInfoRec = record
    strict private
      vFullPath: string;
      vFileName: string;
      vFileIcon: hIcon;
    public
      constructor Create(const FullPath: string);
      property FullPath: string read vFullPath;
      property FileNam : string read vFileName;
      property FileIcon: hIcon read vFileIcon;
  end;

我使用shGetFileInfo Api获得图标句柄

after i got the icon handle using shGetFileInfo Api

procedure TMainFrm.VSTGetImageIndex(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
  var Ghosted: Boolean; var ImageIndex: Integer);
var
  FileInfo: PFileInfoRec;
  Icon: TIcon;
begin
  FileInfo := Sender.GetNodeData(Node);
  Icon := TIcon.Create;
  try
    Icon.Handle := FileInfo.FileIcon;
      if Kind in [ikNormal , ikSelected] then
      begin
        if Column = 1 then ImageIndex := ImageList.AddIcon(Icon);
      end;
  finally
    Icon.Free; //here the probelme
  end;
end;

什么让我感到困惑,除去Icon.Free;代码工作正常文件添加图标
但是当免费TIcon对象添加图标失败!任何人向我解释
这段代码有什么问题??

what confuse me whene remove Icon.Free; the code work fine File added with icons but when free TIcon Object the addition of the icon fail!! any one explain to me What's wrong with this code ??

感谢提前帮助......

Thanks for the help in advance...

推荐答案

首先,你必须在 OnGetImageIndex ImageList.AddIcon C $ C>。我不会详细重复这些建议,只是简单地向您推荐您之前的问题

First of all, you must stop calling ImageList.AddIcon in OnGetImageIndex. I won't repeat the advice in detail but simply refer you to your previous question.

至于这里发生了什么,这是正在发生的事情:

As for what's happening here, this is what is occurring:


  1. 你创建一个图标句柄, vFileIcon

  2. 然后将该图标的所有权传递给 TIcon instance。

  3. 释放图标实例,然后删除图标句柄。

  4. 下次调用 OnGetImageIndex ,图标句柄 vFileIcon 指的是一个已被销毁的句柄,因此你尝试使用该图标会失败。

  1. You create an icon handle, vFileIcon.
  2. You then pass ownership of that icon to the TIcon instance.
  3. You free the icon instance which in turn deletes the icon handle.
  4. The next time you call OnGetImageIndex, the icon handle vFileIcon refers to a handle that has been destroyed and so naturally your attempts to use that icon fail.

将此图标添加到图像列表的最简单方法实际上是使用 ImageList_AddIcon 并且无需创建 TIcon 实例。

The easiest way to add this icon to the image list is in fact to use ImageList_AddIcon and not bother creating a TIcon instance.

FileInfo := Sender.GetNodeData(Node);
ImageList_AddIcon(ImageList.Handle, FileInfo.FileIcon);






可以使用 TIcon 并且在销毁 TIcon 实例时没有销毁句柄。请致电 ReleaseHandle 告诉 TIcon 实例,它不再拥有图标句柄。


It is possible to use a TIcon and not have the handle destroyed when the TIcon instance is destroyed. Call ReleaseHandle to tell the TIcon instance that it no longer owns the icon handle.

Icon := TIcon.Create;
try
  Icon.Handle := SomeIconHandle;
  SomeImageIndex := ImageList.AddIcon(Icon);
  Icon.ReleaseHandle;
finally
  //because ReleaseHandle was called, this no longer destroys the icon handle
  Icon.Free;
end;

这篇关于virtualtreeview添加图标失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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