Recent

Author Topic: Converting HImagelist to TImageList  (Read 3555 times)

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Converting HImagelist to TImageList
« on: February 02, 2017, 05:23:47 am »
After 2 years of too much work and some idle sabatic months, I'm back at using Lazarus and my fav Object Pascal. I'm doing a project and I'm trying to do things the right way.

After some research I got a handle to an image list containing the icons used in the Windows Explorer shell for drives, folders, computer, etc.:

Code: Pascal  [Select][+][-]
  1.   handle := SHGetFileInfo('', 0, shelldriveinfo, SizeOf(shelldriveinfo), SHGFI_ICON or SHGFI_SMALLICON or SHGFI_SYSICONINDEX)

Later I'll get the index of the icon of each path or drive, but I am not sure how to access the icon data without making it somewhat cumbersome, and for some efficiency and simplicity, my components (combobox, treeview, etc.) would rather use a TImageList.

So, how can I somehow convert the handle (HImageList) and use it as the component.ImageList property? Direct assigment would be preferred, but copying the shell icons to my own created TImageList would also be cool.

(Btw tried a simple TImageList(handle) and got a SigSegv with VirtualTreeView)
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

balazsszekely

  • Guest
Re: Converting HImagelist to TImageList
« Reply #1 on: February 02, 2017, 07:01:29 am »

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Converting HImagelist to TImageList
« Reply #2 on: February 03, 2017, 12:48:33 am »
I've tried several stuff I googled, including one I found in these forums, but none worked. However I didn't see that thread! Thanks for pointing it, I'll mark this as solved as soon as I can try the methods in there, Groundhog Day messed up my schelude ^_^
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

ASerge

  • Hero Member
  • *****
  • Posts: 2510
Re: Converting HImagelist to TImageList
« Reply #3 on: February 04, 2017, 08:44:01 am »
So, how can I somehow convert the handle (HImageList) and use it as the component.ImageList property?
You can skip component TImageList. For example, put TShellTreeView and TShellListView on form. Set list property ShellTreeView to tree. Assign to list OnFileAdded event:
Code: Pascal  [Select][+][-]
  1. uses Windows, CommCtrl, ShellApi;
  2. //...
  3. procedure TForm1.TShellListView1FileAdded(Sender: TObject; Item: TListItem);
  4. var
  5.   FullName: UnicodeString;
  6.   R: TSHFILEINFOW;
  7.   SysImageHandle: DWORD_PTR;
  8.   ListHandle: HWND;
  9. begin
  10.   FullName := UTF8Decode(TShellTreeView1.Path) + UTF8Decode(Item.Caption);
  11.   SysImageHandle := SHGetFileInfoW(PWideChar(FullName), 0, R, SizeOf(R),
  12.     SHGFI_SMALLICON or SHGFI_SYSICONINDEX);
  13.   if SysImageHandle = 0 then
  14.     Exit;
  15.   ListHandle := TShellListView1.Handle;
  16.   if ListView_GetImageList(ListHandle, LVSIL_SMALL) = 0 then
  17.   begin
  18.     SetWindowLong(ListHandle, GWL_STYLE,
  19.       GetWindowLong(ListHandle, GWL_STYLE) or LVS_SHAREIMAGELISTS);
  20.     ListView_SetImageList(ListHandle, SysImageHandle, LVSIL_SMALL);
  21.   end;
  22.   Item.ImageIndex := R.iIcon;
  23. end;

 

TinyPortal © 2005-2018