1

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;

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;

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...

S.FATEH
  • 451
  • 8
  • 16
  • @S.FATEH, first, you don't need `class record` for VirtualTreeView, unless you have specific functionality that needs a record Constructor and Properties. A simple `packed record` is what you need. Second, load that icon in `TImageList` at *design-time* and then set only its `ImageIndex` in `OnGetImageIndex` at *run-time*. Everything else is not necessary. – LightBulb Jun 23 '12 at 15:05
  • I missed the "got the icon handle using shGetFileInfo" part. You should load required images into `TImageList` when application starts. Then set `vFileIcon` field to *Integer* and set it to point to `Index` of the image corresponding to that file. Finally, use vFileIcon inside `OnGetImageIndex` to tell VirtualTreeView which Icon to show for that file. Basically, you need to review your logic because VirtualTreeView has to be used in a specific way. – LightBulb Jun 23 '12 at 15:15
  • LightBulb This is exactly what I did the record is in another units and yes there is another functions the code i added above for illustration and sorry for my bed english :)... – S.FATEH Jun 23 '12 at 15:17

1 Answers1

1

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. 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.

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);

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;
Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490