public static Icon GetIcon( IntPtr pidl, bool selected, ImageList imageList ) { SH.SHFILEINFO shfi = new SH.SHFILEINFO(); int cbFileInfo = Marshal.SizeOf(shfi); SH.SHGFI flags = SH.SHGFI.PIDL; if (!selected) flags = flags | SH.SHGFI.ICON | SH.SHGFI.SMALLICON; else flags = flags | SH.SHGFI.ICON | SH.SHGFI.SMALLICON | SH.SHGFI.OPENICON; Icon icon = null; try { SH.SHGetFileInfo( pidl, 0, ref shfi, (uint)cbFileInfo, flags ); if( shfi.hIcon == IntPtr.Zero ) return null; icon = (Icon)Icon.FromHandle( shfi.hIcon ).Clone( ); Api.DestroyIcon( shfi.hIcon ); } catch { } return icon; }
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 = SH.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: SH.SHGetImageListHandle( (int)_size, ref iidImageList, ref _hIml ); } else { // Prepare flags: SH.SHGFI dwFlags = SH.SHGFI.USEFILEATTRIBUTES | SH.SHGFI.SYSICONINDEX; if( _size == SystemImageListSize.SmallIcons ) { dwFlags |= SH.SHGFI.SMALLICON; } // Get image list SH.SHFILEINFO shfi = new SH.SHFILEINFO( ); uint shfiSize = (uint)Marshal.SizeOf( shfi.GetType( ) ); // Call SHGetFileInfo to get the image list handle // using an arbitrary file: _hIml = SH.SHGetFileInfo( ".txt", SH.FILE_ATTRIBUTE_NORMAL, ref shfi, shfiSize, dwFlags ); Debug.Assert( ( _hIml != IntPtr.Zero ), "Failed to create Image List" ); } }
/// <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 ) { uint dwAttr = 0; SH.SHGFI dwFlags = SH.SHGFI.SYSICONINDEX; if (_size == SystemImageListSize.SmallIcons) { dwFlags |= SH.SHGFI.SMALLICON; } // ディスクアクセスするか選択できます。 // ディスクをヒットしないなら、アイコンがキャッシュされていないとおかしなアイコンを得ます。 // これはファイルについてだけ機能します。 if (!forceLoadFromDisk) { dwAttr = SH.FILE_ATTRIBUTE_NORMAL; dwFlags |= SH.SHGFI.USEFILEATTRIBUTES; } // 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" SH.SHFILEINFO shfi = new SH.SHFILEINFO(); uint shfiSize = (uint) Marshal.SizeOf(shfi.GetType()); IntPtr retVal = SH.SHGetFileInfo( fileName, dwAttr, ref shfi, shfiSize, (SH.SHGFI)((uint) (dwFlags) | (uint) iconState)); if (retVal.Equals(IntPtr.Zero)) { Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index"); return 0; } else { return shfi.iIcon; } }