コード例 #1
0
ファイル: Form1.cs プロジェクト: FIX94/MIDIPassthrough
        public static Icon GetIcon(ShellIconSize size)
        {
            SHFILEINFO shinfo = new SHFILEINFO();

            SHGetFileInfo(Application.ExecutablePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (uint)size);
            return(Icon.FromHandle(shinfo.hIcon));
        }
コード例 #2
0
        public static Icon GetIconForExtension(string extension, ShellIconSize size)
        {
            RegistryKey keyForExt = Registry.ClassesRoot.OpenSubKey(extension);
            if (keyForExt == null) return null;

            string className = Convert.ToString(keyForExt.GetValue(null));
            RegistryKey keyForClass = Registry.ClassesRoot.OpenSubKey(className);
            if (keyForClass == null) return null;

            RegistryKey keyForIcon = keyForClass.OpenSubKey("DefaultIcon");
            if (keyForIcon == null)
            {
                RegistryKey keyForCLSID = keyForClass.OpenSubKey("CLSID");
                if (keyForCLSID == null) return null;

                string clsid = "CLSID\\"
                    + Convert.ToString(keyForCLSID.GetValue(null))
                    + "\\DefaultIcon";
                keyForIcon = Registry.ClassesRoot.OpenSubKey(clsid);
                if (keyForIcon == null) return null;
            }

            string[] defaultIcon = Convert.ToString(keyForIcon.GetValue(null)).Split(',');
            int index = (defaultIcon.Length > 1) ? Int32.Parse(defaultIcon[1]) : 0;

            IntPtr[] handles = new IntPtr[1];
            if (ExtractIconEx(defaultIcon[0], index,
                (size == ShellIconSize.LargeIcon) ? handles : null,
                (size == ShellIconSize.SmallIcon) ? handles : null, 1) > 0)
                return Icon.FromHandle(handles[0]);
            else
                return null;
        }
コード例 #3
0
 public static extern IntPtr SHGetFileInfo(
     string pszPath,
     uint dwFileAttributes,
     ref SHFILEINFO psfi,
     uint cbSizeFileInfo,
     ShellIconSize uFlags
     );
コード例 #4
0
ファイル: FileIcon.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Returns an icon representation of the specified file.
        /// </summary>
        /// <param name="filename">The path to the file.</param>
        /// <param name="size">The desired size of the icon.</param>
        /// <returns>An icon that represents the file.</returns>
        public static Icon FromFile(string filename, ShellIconSize size)
        {
            SHFILEINFO psfi = new SHFILEINFO();

            SHGetFileInfo(filename, 0, ref psfi, (uint)Marshal.SizeOf(psfi), size);
            return(Icon.FromHandle(psfi.hIcon));
        }
コード例 #5
0
        /// <summary>
        /// Returns an icon representation of the specified file.
        /// </summary>
        /// <param name="filename">The path to the file.</param>
        /// <param name="size">The desired size of the icon.</param>
        /// <returns>An icon that represents the file.</returns>
        public static Icon GetIconForFile(string filename, ShellIconSize size)
        {
            SHFILEINFO shinfo = new SHFILEINFO();

            SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), size);
            return(Icon.FromHandle(shinfo.hIcon));
        }
コード例 #6
0
        /// <summary>
        /// Returns the default icon representation for files with the specified extension.
        /// </summary>
        /// <param name="extension">File extension (including the leading period).</param>
        /// <param name="size">The desired size of the icon.</param>
        /// <returns>The default icon for files with the specified extension.</returns>
        public static Icon GetIconForExtension(string extension, ShellIconSize size)
        {
            // locate the key corresponding to the file extension
            RegistryKey keyForExt = Registry.ClassesRoot.OpenSubKey(extension);

            if (keyForExt == null)
            {
                return(null);
            }

            // the extension will point to a class name, leading to another key
            string      className   = Convert.ToString(keyForExt.GetValue(null));
            RegistryKey keyForClass = Registry.ClassesRoot.OpenSubKey(className);

            if (keyForClass == null)
            {
                return(null);
            }

            // this key may have a DefaultIcon subkey
            RegistryKey keyForIcon = keyForClass.OpenSubKey("DefaultIcon");

            if (keyForIcon == null)
            {
                // if not, see if it has a CLSID subkey
                RegistryKey keyForCLSID = keyForClass.OpenSubKey("CLSID");
                if (keyForCLSID == null)
                {
                    return(null);
                }

                // the clsid value leads to another key that might contain DefaultIcon
                string clsid = "CLSID\\" + Convert.ToString(keyForCLSID.GetValue(null));
                keyForIcon = Registry.ClassesRoot.OpenSubKey(clsid + "\\DefaultIcon");
                if (keyForIcon == null)
                {
                    return(null);
                }
            }

            // the value of DefaultIcon will either be a path only or a path with a resource index
            string[] defaultIcon = Convert.ToString(keyForIcon.GetValue(null)).Split(',');
            int      index       = (defaultIcon.Length > 1) ? Int32.Parse(defaultIcon[1]) : 0;

            // get the requested icon
            IntPtr[] handles = new IntPtr[1];
            if (ExtractIconEx(defaultIcon[0],
                              index,
                              (size == ShellIconSize.LargeIcon) ? handles : null,
                              (size == ShellIconSize.SmallIcon) ? handles : null,
                              1) > 0)
            {
                return(Icon.FromHandle(handles[0]));
            }
            else
            {
                return(null);
            }
        }
コード例 #7
0
        public static Icon GetIconForFile(string filename, ShellIconSize size)
        {
            SHFILEINFO shinfo = new SHFILEINFO();

            IntPtr hImgSmall = SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (uint)size);

            Icon icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();

            DestroyIcon(shinfo.hIcon);
            return(icon);

            //SHFILEINFO shinfo = new SHFILEINFO();
            //SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (uint)size);
            //return Icon.FromHandle(shinfo.hIcon);
        }
コード例 #8
0
ファイル: ShellIcon.cs プロジェクト: chcg/Rail
        private static ImageSource GetIcon(string path, ShellIconSize shellIconSize)
        {
            NativeMethods.SHFILEINFO shinfo = new NativeMethods.SHFILEINFO();
            NativeMethods.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | (shellIconSize == ShellIconSize.Small ? SHGFI_SMALLICON : SHGFI_LARGEICON));
            IntPtr iconHandle = shinfo.hIcon;

            if (IntPtr.Zero == iconHandle)
            {
                return(null);
            }
            ImageSource img = Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            NativeMethods.DestroyIcon(iconHandle);
            return(img);
        }
コード例 #9
0
ファイル: IconTools.cs プロジェクト: JWSingleton/SolidEdgeSpy
        /// <summary>
        /// Returns the default icon representation for files with the specified extension.
        /// </summary>
        /// <param name="extension">File extension (including the leading period).</param>
        /// <param name="size">The desired size of the icon.</param>
        /// <returns>The default icon for files with the specified extension.</returns>
        public static Icon GetIconForExtension(string extension, ShellIconSize size)
        {
            // locate the key corresponding to the file extension
            using (RegistryKey keyForExt = Registry.ClassesRoot.OpenSubKey(extension))
            {
                if (keyForExt == null) return null;

                // the extension will point to a class name, leading to another key
                string className = Convert.ToString(keyForExt.GetValue(null));
                using (RegistryKey keyForClass = Registry.ClassesRoot.OpenSubKey(className))
                {
                if (keyForClass == null) return null;

                // this key may have a DefaultIcon subkey
                RegistryKey keyForIcon = keyForClass.OpenSubKey("DefaultIcon");
                if (keyForIcon == null)
                {
                    // if not, see if it has a CLSID subkey
                    RegistryKey keyForCLSID = keyForClass.OpenSubKey("CLSID");
                    if (keyForCLSID == null) return null;

                    // the clsid value leads to another key that might contain DefaultIcon
                    string clsid = "CLSID\\" + Convert.ToString(keyForCLSID.GetValue(null));
                    keyForIcon = Registry.ClassesRoot.OpenSubKey(clsid + "\\DefaultIcon");
                    if (keyForIcon == null) return null;
                }

                // the value of DefaultIcon will either be a path only or a path with a resource index
                string[] defaultIcon = Convert.ToString(keyForIcon.GetValue(null)).Split(',');
                int index = (defaultIcon.Length > 1) ? Int32.Parse(defaultIcon[1]) : 0;

                keyForIcon.Dispose();

                // get the requested icon
                IntPtr[] handles = new IntPtr[1];
                if (ExtractIconEx(defaultIcon[0],
                    index,
                    (size == ShellIconSize.LargeIcon) ? handles : null,
                    (size == ShellIconSize.SmallIcon) ? handles : null,
                    1) > 0)
                    return Icon.FromHandle(handles[0]);
                else
                    return null;
                    }
            }
        }
コード例 #10
0
ファイル: IconTools.cs プロジェクト: Ulterius/server
        /// <summary>
        ///     Returns an icon representation of the specified file.
        /// </summary>
        /// <param name="filename">The path to the file.</param>
        /// <param name="size">The desired size of the icon.</param>
        /// <returns>An icon that represents the file.</returns>
        public static Icon GetIconForFile(string filename, ShellIconSize size)
        {
            var shinfo = new SHFILEINFO();
            NativeMethods.SHGetFileInfo(filename, 0, ref shinfo, (uint) Marshal.SizeOf(shinfo), size);

            Icon icon = null;

            if (shinfo.hIcon.ToInt32() != 0)
            {
                // create the icon from the native handle and make a managed copy of it
                icon = (Icon) Icon.FromHandle(shinfo.hIcon).Clone();

                // release the native handle
                NativeMethods.DestroyIcon(shinfo.hIcon);
            }

            return icon;
        }
コード例 #11
0
    /// <summary>
    /// Returns an icon representation of the specified file.
    /// </summary>
    /// <param name="filename">The path to the file.</param>
    /// <param name="size">The desired size of the icon.</param>
    /// <returns>An icon that represents the file.</returns>
    public static Icon GetIconForFile(string filename, ShellIconSize size)
    {
        SHFILEINFO shinfo = new SHFILEINFO();

        NativeMethods.SHGetFileInfo(filename, 256, ref shinfo, (uint)Marshal.SizeOf(shinfo), size);

        Icon icon = null;

        if (shinfo.hIcon.ToInt32() != 0)
        {
            // create the icon from the native handle and make a managed copy of it
            icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();

            // release the native handle
            NativeMethods.DestroyIcon(shinfo.hIcon);
        }

        return(icon);
    }
コード例 #12
0
ファイル: FileIcon.cs プロジェクト: ithanshui/Common-2
        /// <summary>
        /// Returns the default icon representation for files with the specified extension.
        /// </summary>
        /// <param name="extension">File extension (including the leading period).</param>
        /// <param name="size">The desired size of the icon.</param>
        /// <returns>The default icon for files with the specified extension.</returns>
        public static Icon FromExtension(string extension, ShellIconSize size)
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);

            if (key != null)
            {
                string      name = Convert.ToString(key.GetValue(null));
                RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(name);
                if (key2 == null)
                {
                    return(null);
                }
                RegistryKey key3 = key2.OpenSubKey("DefaultIcon");
                if (key3 == null)
                {
                    RegistryKey key4 = key2.OpenSubKey("CLSID");
                    if (key4 == null)
                    {
                        return(null);
                    }
                    key3 = Registry.ClassesRoot.OpenSubKey((@"CLSID\" + Convert.ToString(key4.GetValue(null))) + @"\DefaultIcon");
                    if (key3 == null)
                    {
                        return(null);
                    }
                }
                string[] strArray  = Convert.ToString(key3.GetValue(null)).Split(new char[] { ',' });
                int      iconIndex = (strArray.Length > 1) ? int.Parse(strArray[1]) : 0;
                IntPtr[] ptrArray  = new IntPtr[1];
                if (ExtractIconEx(strArray[0], iconIndex, (size == ShellIconSize.LargeIcon) ? ptrArray : null, (size == ShellIconSize.SmallIcon) ? ptrArray : null, 1) > 0)
                {
                    return(Icon.FromHandle(ptrArray[0]));
                }
            }
            return(null);
        }
コード例 #13
0
ファイル: IconTools.cs プロジェクト: Ulterius/server
 /// <summary>
 ///     Returns the default icon representation for files with the specified extension.
 /// </summary>
 /// <param name="extension">File extension (including the leading period).</param>
 /// <param name="size">The desired size of the icon.</param>
 /// <returns>The default icon for files with the specified extension.</returns>
 public static Icon GetIconForExtension(string extension, ShellIconSize size)
 {
     // repeat the process used for files, but instruct the API not to access the file
     size |= (ShellIconSize) SHGFI_USEFILEATTRIBUTES;
     return GetIconForFile(extension, size);
 }
コード例 #14
0
ファイル: IconTools.cs プロジェクト: Ulterius/server
 public static extern IntPtr SHGetFileInfo(
     string pszPath,
     uint dwFileAttributes,
     ref SHFILEINFO psfi,
     uint cbSizeFileInfo,
     ShellIconSize uFlags
     );
コード例 #15
0
ファイル: IconTools.cs プロジェクト: jameshy/else
 /// <summary>
 /// Returns wrapped function for loading a file icon
 /// Accepts paths to executables, or folders.
 /// </summary>
 /// <param name="path">The path to the file or directory.</param>
 /// <param name="size">The icon size.</param>
 public static Lazy<BitmapSource> GetBitmapForFile(string path, ShellIconSize size=ShellIconSize.LargeIcon)
 {
     return new Lazy<BitmapSource>(() => {
         try {
             var icon = GetIconForFile(path, ShellIconSize.LargeIcon);
             // icon was not found
             if (icon == null) {
                 return null;
             }
             // convert the Icon to a BitmapImage
             var bitmapSource = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
             bitmapSource.Freeze();
             // release icon
             DestroyIcon(icon.Handle);
             return bitmapSource;
         }
         catch {
             return null;
         }
     });
 }
コード例 #16
0
 /// <summary>
 /// Returns the default icon representation for files with the specified extension.
 /// </summary>
 /// <param name="extension">File extension (including the leading period).</param>
 /// <param name="size">The desired size of the icon.</param>
 /// <returns>The default icon for files with the specified extension.</returns>
 public static Icon GetIconForExtension(string extension, ShellIconSize size)
 {
     // repeat the process used for files, but instruct the API not to access the file
     size |= (ShellIconSize)SHGFI_USEFILEATTRIBUTES;
     return(GetIconForFile(extension, size));
 }
コード例 #17
0
ファイル: Form1.cs プロジェクト: FIX94/MIDIPassthrough
 public static Icon GetIcon(ShellIconSize size)
 {
     SHFILEINFO shinfo = new SHFILEINFO();
     SHGetFileInfo(Application.ExecutablePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (uint)size);
     return Icon.FromHandle(shinfo.hIcon);
 }
コード例 #18
0
        /// <summary>
        /// Returns a fronzen ImageSource that is the shell icon of the specified path.
        /// </summary>
        /// <param name="path">The file/folder relative/abolute path. Nonexistent path is allowed. Null is not allowed.</param>
        /// <param name="shellIconSize">The icon size.</param>
        /// <param name="includeOverlay">Whether the overlay is displayed or not. It is ignored except these icon sizes: ShellIconSize.ShellSized, ShellIconSize.Small, ShellIconSize.Large.</param>
        /// <param name="includeLinkOverlay">Whether the link overlay is displayed or not. It is ignored except these icon sizes: ShellIconSize.ShellSized, ShellIconSize.Small, ShellIconSize.Large.</param>
        /// <param name="isOpen">Whether the open icon is returned or not.</param>
        /// <param name="isSelected">Whether the selected icon is returned or not.</param>
        /// <returns>A frozen ImageSource that is the shell icon.</returns>
        public static ImageSource GetIcon(string path, ShellIconSize shellIconSize, bool includeOverlay = false, bool includeLinkOverlay = false, bool isOpen = false, bool isSelected = false)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            // Creates a uFlags for the SHGetFileInfo function.
            uint uFlags = Win32.SHGFI_ICON | Win32.SHGFI_USEFILEATTRIBUTES;

            if (includeOverlay)
            {
                uFlags |= Win32.SHGFI_ADDOVERLAYS;
            }

            if (includeLinkOverlay)
            {
                uFlags |= Win32.SHGFI_LINKOVERLAY;
            }

            if (isOpen)
            {
                uFlags |= Win32.SHGFI_OPENICON;
            }

            if (isSelected)
            {
                uFlags |= Win32.SHGFI_SELECTED;
            }


            // Whether the shFileInfo structure's hIcon handle can be used to make the requested sized icon.
            bool isShFileInfoEnough = false;

            if (shellIconSize == ShellIconSize.ShellSized)
            {
                uFlags            |= Win32.SHGFI_SHELLICONSIZE;
                isShFileInfoEnough = true;
            }
            else if (shellIconSize == ShellIconSize.Small)
            {
                uFlags            |= Win32.SHGFI_SMALLICON;
                isShFileInfoEnough = true;
            }
            else if (shellIconSize == ShellIconSize.Large)
            {
                uFlags            |= Win32.SHGFI_LARGEICON;
                isShFileInfoEnough = true;
            }


            // Creates a dwFileAttributes for the SHGetFileInfo function.
            uint dwFileAttributes = Win32.FILE_ATTRIBUTE_NORMAL;

            if (Directory.Exists(path))
            {
                dwFileAttributes |= Win32.FILE_ATTRIBUTE_DIRECTORY;
            }


            // Calls the SHGetFileInfo function.
            Win32.SHFILEINFO shFileInfo = new Win32.SHFILEINFO();
            if (Win32.SHGetFileInfo(path, dwFileAttributes, ref shFileInfo, (uint)Win32.SizeOfSHFILEINFO, uFlags) == IntPtr.Zero)
            {
                throw new Exception("SHGetFileInfo function has failed.");
            }


            try
            {
                // If shFileInfo's hIcon is enough
                if (isShFileInfoEnough)
                {
                    return(ToImageSource(shFileInfo.hIcon));
                }
            }
            finally
            {
                // Releases the icon handle.
                if (shFileInfo.hIcon != IntPtr.Zero)
                {
                    if (Win32.DestroyIcon(shFileInfo.hIcon) == 0)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    shFileInfo.hIcon = IntPtr.Zero;
                }
            }


            // A iImageList for the SHGetImageList function.
            int iImageList;

            switch (shellIconSize)
            {
            case ShellIconSize.SystemSmall:
                iImageList = Win32.SHIL_SYSSMALL;
                break;

            case ShellIconSize.ExtraLarge:
                iImageList = Win32.SHIL_EXTRALARGE;
                break;

            case ShellIconSize.Jumbo:
                iImageList = Win32.SHIL_JUMBO;
                break;

            default:
                throw new InvalidOperationException(string.Format("The ShellIconSize.{0} is unknown.", shellIconSize));
            }


            // Gets a image list.
            Guid   IID_IImageList = Win32.IID_IImageList;
            IntPtr hImageList     = IntPtr.Zero;

            Marshal.ThrowExceptionForHR(Win32.SHGetImageList(iImageList, ref IID_IImageList, ref hImageList));


            // Gets an icon handle from the image list.
            IntPtr hIcon = Win32.ImageList_GetIcon(hImageList, shFileInfo.iIcon, Win32.ILD_TRANSPARENT);

            if (hIcon == IntPtr.Zero)
            {
                throw new Exception("ImageList_GetIcon function has failed.");
            }


            // TODO: Show overlay on the image list icon.


            try
            {
                // Returns the ImageSource.
                return(ToImageSource(hIcon));
            }
            finally
            {
                // Releases the icon handle.
                if (Win32.DestroyIcon(hIcon) == 0)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
        }
コード例 #19
0
ファイル: IconTools.cs プロジェクト: JWSingleton/SolidEdgeSpy
 /// <summary>
 /// Returns an icon representation of the specified file.
 /// </summary>
 /// <param name="filename">The path to the file.</param>
 /// <param name="size">The desired size of the icon.</param>
 /// <returns>An icon that represents the file.</returns>
 public static Icon GetIconForFile(string filename, ShellIconSize size)
 {
     SHFILEINFO shinfo = new SHFILEINFO();
     SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), size);
     return Icon.FromHandle(shinfo.hIcon);
 }
コード例 #20
0
ファイル: IconTools.cs プロジェクト: jameshy/else
 /// <summary>
 /// Returns an icon representation of the specified file.
 /// </summary>
 /// <param name="filename">The path to the file.</param>
 /// <param name="size">The desired size of the icon.</param>
 /// <returns>An icon that represents the file.</returns>
 public static Icon GetIconForFile(string filename, ShellIconSize size)
 {
     SHFILEINFO shinfo = new SHFILEINFO();
     IntPtr HR = SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), size);
     if (HR == IntPtr.Zero) {
         return null;
     }
     return Icon.FromHandle(shinfo.hIcon);
 }