BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 


Sunday, September 16, 2007
A little FileIconInit for the System Image list

I can hear the theme for Welcome Back Kotter as I write this. It seems that things have been quiet on here for quite sometime, however those that know me can attest to the fact that life away from here has been anything but that. Thankfully, things have returned to a point where a lot more content should find its way onto this world.

As things progress, I am doing a substantial amount of development using C# and the .NET Framework. I still continue to develop new utilities that satisfy my specific needs. I also try to update some of the available utilities. The most recent update has been to ASysIcon (this application was redeveloped using the .NET Framework). I've done quite a bit of work with icons in the past (also take a look at AIconExtract) and ASysIcon is a utility that displays the images found in the system image list. The system image list contains the icons that are associated with the different registered file types on a computer.

The retrieval of the system image list is a straight forward process. There are a few unmanaged API calls that work quite well for this task. One thing that may not be so apparent is that the the images retrieved will be those images that are cached in the system image list. If the system hasn't had a need to load them, well, they won't be in the list. This could pose some concern if you would like to retrieve the entire list. The trick is to get all the system images cached. Actually, this isn't really a trick. With little searching the FileIconInit function surfaces. This function will initialize or reinitialize this image list. FileIconInit has a boolean parameter determines if it should restore the image cache from disk. Passing true as this parameter will load all of the images, not just the recently cached ones. The following sample code shows how to display the system image list in a ListView:


1using System;
2using System.Windows.Forms;
3
4 using System.Runtime.InteropServices; // http://support.microsoft.com/kb/319350
5
6namespace SysIcons1
7{
8 public partial class Form1 : Form
9 {
10 [StructLayout(LayoutKind.Sequential)]
11 public struct SHFILEINFO
12 {
13 public IntPtr hIcon;
14 public IntPtr iIcon;
15 public uint dwAttributes;
16 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
17 public string szDisplayName;
18 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
19 public string szTypeName;
20 }
;
21 //Retrieve the handle to the icon that represents the file and the index of the icon within the system image list
22 public const uint SHGFI_ICON = 0x100;
23
24 // Retrieve the index of a system image list icon.
25 public const uint SHGFI_SYSICONINDEX = 0x4000;
26
27 // Large icon
28 public const uint SHGFI_LARGEICON = 0x0;
29
30 // Small icon
31 public const uint SHGFI_SMALLICON = 0x1;
32
33 public const uint LVM_FIRST = 4096;
34 public const uint LVSIL_NORMAL = 0;
35 public const uint LVSIL_SMALL = 1;
36 public const uint LVM_SETIMAGELIST = LVM_FIRST + 3;
37 // need this style on Windows 9x
38 public const uint LVS_SHAREIMAGELISTS = 0x0040;
39
40 // Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.
41 [DllImport("shell32.dll")]
42 public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
43
44 // The SendMessage function sends the specified message to a window or windows.
45 [DllImport("user32.dll")]
46 private static extern UInt32 SendMessage(IntPtr hWnd, UInt32 Msg,
47 UInt32 wParam, UInt32 lParam);
48
49 // Destroys an icon and frees any memory the icon occupied.
50 [DllImport("User32.dll")]
51 private static extern int DestroyIcon(System.IntPtr hIcon);
52
53 // Initializes or reinitializes the system image list.
54 // FileIconInit is not included in a header file. You must call it directly from Shell32.dll, using ordinal 660.
55 [DllImport( "Shell32.dll", EntryPoint = "#660")]
56 private static extern bool FileIconInit( bool fRestoreCache );
57
58 // Retrieves the number of images in an image list.
59 [DllImport( "comctl32.dll")]
60 private static extern int ImageList_GetImageCount( IntPtr himl);
61
62 public Form1()
63 {
64 InitializeComponent();
65 }

66
67 private void Form1_Load(object sender, EventArgs e)
68 {
69 IntPtr hImgSmall; //handle to the small system image list
70 IntPtr hImgLarge; //handle to the large system image list
71 SHFILEINFO shinfo = new SHFILEINFO();
72
73 FileIconInit(true);
74
75 //Small Icons
76 hImgSmall = SHGetFileInfo("", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX SHGFI_SMALLICON);
77
78 //Large Icons
79 hImgLarge = SHGetFileInfo("", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX SHGFI_LARGEICON);
80
81 // Set Icon Images
82 SendMessage(listView1.Handle, LVM_SETIMAGELIST, LVSIL_SMALL, (uint)hImgSmall.ToInt32());
83 SendMessage(listView1.Handle, LVM_SETIMAGELIST, LVSIL_NORMAL, (uint)hImgLarge.ToInt32());
84
85 for ( int i = 0; i < ImageList_GetImageCount(hImgLarge) ; i++ )
86 {
87 listView1.Items.Add(i.ToString(),i);
88 }

89 }

90
91 private int Get_Image_Index(string filename)
92 {
93 IntPtr hImgSmall;
94 SHFILEINFO shinfo = new SHFILEINFO();
95
96 hImgSmall = SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo.GetType()), SHGFI_ICON SHGFI_SMALLICON);
97 DestroyIcon(shinfo.hIcon);
98 return (int)shinfo.iIcon;
99 }

100 }

Labels: , ,

posted by Brad Prendergast at 9:55:00 PM
Comments:
Links to this post:

Create a Link

Recent Posts
 Code Monkey
 A DataGridView, DataGridViewCheckBoxColumn, DataGr...
 Those Commercials
 System Information and the PerformanceCounter Clas...
 Cryptography hash and a class - to go
 C# LocalAdapterInformation
 PInvoke WIN32 and .NET
 ASP.NET upload a file HtmlInputFile Control
 PPCTL.DLL is damaged and could not be repaired.
 Google Mobile SMS


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger