/// <summary> /// Creates the SystemImageList /// </summary> private void create() { // forget last image list if any: hIml = IntPtr.Zero; if (isXpOrAbove()) { // Get the System IImageList object from the Shell: Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); int ret = Shell32.SHGetImageList((int)size, ref iidImageList, ref iImageList); // the image list handle is the IUnknown pointer, but // using Marshal.GetIUnknownForObject doesn't return // the right value. It really doesn't hurt to make // a second call to get the handle: Shell32.SHGetImageListHandle((int)size, ref iidImageList, ref hIml); } else { // Prepare flags: SHGFI dwFlags = SHGFI.UseFileAttributes | SHGFI.SysIconIndex; if (size == SysImageListSize.smallIcons) { dwFlags |= SHGFI.SmallIcon; } // Get image list ShFileInfo shfi = new ShFileInfo(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); // Call SHGetFileInfo to get the image list handle // using an arbitrary file: hIml = Shell32.SHGetFileInfo(".txt", FileAttributes.Normal, ref shfi, (int)shfiSize, dwFlags); System.Diagnostics.Debug.Assert((hIml != IntPtr.Zero), "Failed to create Image List"); } }
public Form1() { InitializeComponent(); // var fileInfo2 = new ShFileInfo(); var largeImageList = Shell32.SHGetFileInfo(".txt", FileAttributes.Normal, ref fileInfo2, Marshal.SizeOf(fileInfo2), SHGFI.UseFileAttributes | SHGFI.SysIconIndex | SHGFI.LargeIcon | SHGFI.OverlayIndex | SHGFI.AddOverlays); var fileInfo3 = new ShFileInfo(); var smallImageList = Shell32.SHGetFileInfo(".txt", FileAttributes.Normal, ref fileInfo3, Marshal.SizeOf(fileInfo3), SHGFI.UseFileAttributes | SHGFI.SysIconIndex | SHGFI.OverlayIndex | SHGFI.SmallIcon | SHGFI.AddOverlays); //Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); //IntPtr h = IntPtr.Zero; //Shell32.SHGetImageList((int)SysImageListSize.extraLargeIcons, ref iidImageList, ref h); //SysImageList list = new SysImageList(SysImageListSize.extraLargeIcons); //User32.SendMessage(listView1.Handle, WM.LvmSetImageList, TVSIL_NORMAL, list.Handle); User32.SendMessage(listView1.Handle, WM.LvmSetImageList, TLVSIL_SMALL, smallImageList); User32.SendMessage(listView1.Handle, WM.LvmSetImageList, TVSIL_NORMAL, largeImageList); //User32.SendMessage(listView1.Handle, WM.LvmSetImageList, 3, smallImageList); // //this.listView1.LargeImageList = this.imageList2; //this.listView1.SmallImageList = this.imageList1; var fileInfo = new ShFileInfo(); Shell32.SHGetFileInfo(@"D:\Projects\Commander.Net\", 0, ref fileInfo, Marshal.SizeOf(fileInfo), SHGFI.Icon | SHGFI.OverlayIndex | SHGFI.LargeIcon); var fileInfo0 = new ShFileInfo(); Shell32.SHGetFileInfo(@"D:\Projects\Commander.Net\Microsoft.WindowsAPICodePack.ShellExtensions.dll", 0, ref fileInfo0, Marshal.SizeOf(fileInfo0), SHGFI.Icon | SHGFI.LargeIcon | SHGFI.AddOverlays); Image image = ImageHelper.IconToBitmap(fileInfo0.IconHandle); pictureBox1.Image = ImageHelper.IconToBitmap(fileInfo0.IconHandle); Shell32.DestroyIcon(fileInfo.IconHandle); Shell32.DestroyIcon(fileInfo0.IconHandle); fileInfo = new ShFileInfo(); Shell32.SHGetFileInfo(@"D:\Projects\Commander.Net\Microsoft.WindowsAPICodePack.ShellExtensions.dll", 0, ref fileInfo, Marshal.SizeOf(fileInfo), SHGFI.Icon | SHGFI.OverlayIndex | SHGFI.LargeIcon); //int i = Shell32.SHGetIconOverlayIndex(@"D:\Projects\Commander.Net", fileInfo.IconIndex); //var ico = list.Icon(5); //if (ico != null) //{ // pictureBox1.Image = ImageHelper.IconToBitmap(ico, ico.Size); //} //listView1.Items.Add("Commander.Net", fileInfo.IconIndex); listView1.Items.Add("Commander.Net", fileInfo.IconIndex); listView1.Items.Add("Commander.Net", fileInfo.IconIndex + 1); ////--- //fileInfo = new ShFileInfo(); //Shell32.SHGetFileInfo(@"D:\Projects\ConsoleApplication1", 0, ref fileInfo, Marshal.SizeOf(fileInfo), SHGFI.ShellIconSize | SHGFI.Icon | SHGFI.OverlayIndex | SHGFI.LargeIcon | SHGFI.AddOverlays); ////imageList2.Images.Add(Icon.FromHandle(fileInfo.IconHandle)); //listView1.Items.Add("ConsoleApplication1", fileInfo.IconIndex); SysImageList list = new SysImageList(SysImageListSize.largeIcons); var ico = list.Icon(fileInfo.IconIndex >> 24); //pictureBox1.Image = ImageHelper.IconToBitmap(ico, ico.Size); ThreadPool.QueueUserWorkItem(delegate { try { for (int i = 0; i < 30; i++) { Thread.Sleep(500); this.Invoke((MethodInvoker)delegate { var ico2 = list.Icon(i); if (ico2 != null) { pictureBox1.Image = ImageHelper.IconToBitmap(ico2, ico2.Size); } }); } } catch (Exception) { } }); }
/// <summary> /// Returns the index of the icon for the specified file /// </summary> /// <param name="fileName">Filename to get icon for</param> /// <param name="forceLoadFromDisk">If True, then hit the disk to get the icon, /// otherwise only hit the disk if no cached icon is available.</param> /// <param name="iconState">Flags specifying the state of the icon /// returned.</param> /// <returns>Index of the icon</returns> public int IconIndex(string fileName, bool forceLoadFromDisk, ShellIconStateConstants iconState) { SHGFI dwFlags = SHGFI.SysIconIndex; FileAttributes dwAttr; if (size == SysImageListSize.smallIcons) { dwFlags |= SHGFI.SmallIcon; } // We can choose whether to access the disk or not. If you don't // hit the disk, you may get the wrong icon if the icon is // not cached. Also only works for files. if (!forceLoadFromDisk) { dwFlags |= SHGFI.UseFileAttributes; dwAttr = FileAttributes.Normal; } else { dwAttr = 0; } // sFileSpec can be any file. You can specify a // file that does not exist and still get the // icon, for example sFileSpec = "C:\PANTS.DOC" ShFileInfo shfi = new ShFileInfo(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); IntPtr retVal = Shell32.SHGetFileInfo(fileName, dwAttr, ref shfi, (int)shfiSize, (SHGFI)((uint)(dwFlags) | (uint)iconState)); if (retVal.Equals(IntPtr.Zero)) { System.Diagnostics.Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index"); return 0; } else { return shfi.IconIndex; } }
public static extern IntPtr SHGetFileInfo(string path, FileAttributes fileAttributes, ref ShFileInfo psfi, int fileInfo, SHGFI flags);