Adding icons to TShellListView lazarus / 31-LCL ---------------------------------------------------------------------- funk247 · 2013-08-29 11:27 I am producing an application that creates files and saves them to disk, an essential part of the app is a file browser, I have managed to reproduce the required functionality using the Misc component TShellListTreeView and TShellListView but I have no idea how to add icons to the lists! I've added an imageList and inserted bmp icons to it, linked it in to the smallImages field and checked that the view style is correct (vsSmallIcon) but nothing! I'm presuming I need to loop through the list items programmatically and add the icons but have been unable to do so. I have managed to programmatically add a single item with an icon. But what I want to do is simply apply icons to pre-existing files. What other properties support the imageindex type? Has anyone got some code lying around that would accomplish this? Failing that a link to an actual tutorial would be lovely, if I get it working I'll even update the wiki page so that no-one else has to spend fruitless hours searching for something that possibly doesn't exist. Cheers! ---------------------------------------------------------------------- funk247 · 2013-08-29 12:01 Ooosh no worries I got it which usefully also lets me off updating the wiki. I got the info from the Delphi forums this code seems to do the trick Code: [Select]procedure TForm1.FormShow(Sender: TObject); var t:integer; begin for t:= 0 to form1.ShellListView1.Items.Count-1 do BEGIN  form1.ShellListView1.Items[t].ImageIndex := 0; END; end; ---------------------------------------------------------------------- Mike.Cornflake · 2013-08-29 12:10 Thanks for posting your solution.  I thought you were talking about Shell Icons, and was looking forward to the Wiki article :-) ---------------------------------------------------------------------- funk247 · 2013-08-29 12:41 No worries, I'll always try to post a solution when I find it just to help others out in the future. I have another question though, how would I go about applying different icons to folders and files in the component? Does it differentiate at all? ---------------------------------------------------------------------- Mike.Cornflake · 2013-08-29 13:23 QuoteI have another question though, how would I go about applying different icons to folders and files in the component? Does it differentiate at all?Dunno :-(  Part of the answer you've already worked out.  Just add different images to your TImageList, then set appropriate values when setting .ImageIndex (at the moment, you're setting everything to 0). What I don't know is if there is an easy way to differentiate files and folders in the TShellListView.  (I know the long way If FileExists or If FolderExists, but that'll just slow your code) The fact that there is a .ObjectTypes property is a good start, but I see Items only holds TListItems, so I'm not sure that'll have enough information for you... UPDATE: Hmmm, checking https://github.com/alrieckert/lazarus/blob/master/lcl/shellctrls.pas (but writing blind, I'm away from compiler) It has the following code in it... Code: [Select]    for i := 0 to Files.Count - 1 do     begin       NewNode := Items.AddChildObject(ANode, Files.Strings[i], nil); //@Files.Strings[i]);       NewNode.HasChildren := Files.Objects[i] <> nil; // This marks if the node is a directory     end; Which means, if I'm understanding this right, the following should work for you (after you add a second image to your TImageList... Code: [Select]for t:= 0 to form1.ShellListView1.Items.Count-1 do BEGIN   If form1.ShellListView1.Items[t].HasChildren  Then      form1.ShellListView1.Items[t].ImageIndex := 0 // ImageIndex for Folders   Else      form1.ShellListView1.Items[t].ImageIndex := 1; // ImageIndex for Files END; end; Absolutely no idea if this is correct, or if it will compile...  Good luck... ---------------------------------------------------------------------- funk247 · 2013-08-29 13:30 I'll give it a go shortly, fingers crossed, thanks for the help E2a: No go, it fails on the HasChildren reporting that the selector doesnt exist, Im pretty sure your working on the right lines though so I'll keep digging once I've got the other parts of the app in place. Thanks again! ---------------------------------------------------------------------- Mike.Cornflake · 2013-08-29 15:55 Bleh, playing with code through a webbrowser when I should be working... Near as I can make out .HasChildren should be public, so you should have access to it.  If somehow or other it really is hidden, then you might be able to resurface it with the following Hack.  Still no guarantees as to compilability or runability... Code: [Select]Type   THackListItem = Class(TListITem)   Public      Property HasChildren;   End; for t:= 0 to form1.ShellListView1.Items.Count-1 do BEGIN   If THackListItem(form1.ShellListView1.Items[t]).HasChildren  Then      form1.ShellListView1.Items[t].ImageIndex := 0 // ImageIndex for Folders   Else      form1.ShellListView1.Items[t].ImageIndex := 1; // ImageIndex for Files END; ---------------------------------------------------------------------- bitsnaps · 2016-08-08 18:27 I think a better way to add image to TShellTreeView is to use OnGetImageIndex event handler as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ShellTreeView1GetImageIndex(Sender: TObject; Node: TTreeNode);begin  if TShellTreeNode(Node).IsDirectory then     Node.ImageIndex := 1  else    Node.ImageIndex:= 2;end; and for TShellListView I used: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ShellListView1FileAdded(Sender: TObject; Item: TListItem);begin  if Assigned(Item) then //just to avoid any misbehavior      Item.ImageIndex:= 2;end;    Hope this helps someone.