예제 #1
0
        /// <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
            )
        {
            var dwFlags = ShGetFileInfoConstants.ShgfiSysiconindex;
            var dwAttr  = 0;

            if (_size == SysImageListSize.SmallIcons)
            {
                dwFlags |= ShGetFileInfoConstants.ShgfiSmallicon;
            }

            // 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 |= ShGetFileInfoConstants.ShgfiUsefileattributes;
                dwAttr   = FileAttributeNormal;
            }
            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"
            var shfi     = new Shfileinfo();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
            var retVal   = SHGetFileInfo(
                fileName, dwAttr, ref shfi, shfiSize,
                ((uint)(dwFlags) | (uint)iconState));

            if (retVal.Equals(IntPtr.Zero))
            {
                System.Diagnostics.Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index");
                return(0);
            }
            return(shfi.iIcon);
        }
예제 #2
0
        /// <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:
                var iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
                var ret          = 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:
                SHGetImageListHandle((int)_size, ref iidImageList, ref _hIml);
            }
            else
            {
                // Prepare flags:
                var dwFlags = ShGetFileInfoConstants.ShgfiUsefileattributes | ShGetFileInfoConstants.ShgfiSysiconindex;
                if (_size == SysImageListSize.SmallIcons)
                {
                    dwFlags |= ShGetFileInfoConstants.ShgfiSmallicon;
                }
                // Get image list
                var shfi     = new Shfileinfo();
                var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());

                // Call SHGetFileInfo to get the image list handle
                // using an arbitrary file:
                _hIml = SHGetFileInfo(
                    ".txt",
                    FileAttributeNormal,
                    ref shfi,
                    shfiSize,
                    (uint)dwFlags);
                System.Diagnostics.Debug.Assert((_hIml != IntPtr.Zero), "Failed to create Image List");
            }
        }
예제 #3
0
        /// <summary>
        ///  Gets the information for the specified
        ///  file name and flags.
        /// </summary>
        public void GetInfo()
        {
            _fileIcon    = null;
            _typeName    = "";
            _displayName = "";

            Shfileinfo shfi     = new Shfileinfo();
            uint       shfiSize = (uint)Marshal.SizeOf(shfi.GetType());

            int ret = SHGetFileInfo(
                _fileName, 0, ref shfi, shfiSize, (uint)(_flags));

            if (ret != 0)
            {
                if (shfi.hIcon != IntPtr.Zero)
                {
                    _fileIcon = System.Drawing.Icon.FromHandle(shfi.hIcon);
                    // Now owned by the GDI+ object
                    //DestroyIcon(shfi.hIcon);
                }
                _typeName    = shfi.szTypeName;
                _displayName = shfi.szDisplayName;
            }
            else
            {
                int err = GetLastError();
                Console.WriteLine("Error {0}", err);
                string txtS = new string('\0', 256);
                int    len  = FormatMessage(
                    FormatMessageFromSystem | FormatMessageIgnoreInserts,
                    IntPtr.Zero, err, 0, txtS, 256, 0);
                Console.WriteLine("Len {0} text {1}", len, txtS);

                // throw exception
            }
        }