Enables extraction of icons for any file type from the Shell.
Exemple #1
0
        private Icon getIcon(bool large)
        {
            // Get icon index and path:
            int           iconIndex = 0;
            StringBuilder iconPath  = new StringBuilder(260, 260);

            if (_linkA == null)
            {
                _linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
            }
            else
            {
                _linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
            }
            string iconFile = iconPath.ToString();

            // If there are no details set for the icon, then we must use
            // the shell to get the icon for the target:
            if (iconFile.Length == 0)
            {
                // Use the FileIcon object to get the icon:
                FileInfoOptions flags = FileInfoOptions.Icon | FileInfoOptions.Attributes;

                if (large)
                {
                    flags = flags | FileInfoOptions.LargeIcon;
                }
                else
                {
                    flags = flags | FileInfoOptions.SmallIcon;
                }
                FileIcon fileIcon = new FileIcon(Target, flags);
                return(fileIcon.ShellIcon);
            }
            else
            {
                // Use ExtractIconEx to get the icon:
                IntPtr[] hIconEx = new IntPtr[1] {
                    IntPtr.Zero
                };
                int iconCount;
                if (large)
                {
                    iconCount = NativeMethods.ExtractIconEx(
                        iconFile,
                        iconIndex,
                        hIconEx,
                        null,
                        1);
                }
                else
                {
                    iconCount = NativeMethods.ExtractIconEx(
                        iconFile,
                        iconIndex,
                        null,
                        hIconEx,
                        1);
                }
                // If success then return as a GDI+ object
                Icon icon = null;
                if (hIconEx[0] != IntPtr.Zero)
                {
                    icon = Icon.FromHandle(hIconEx[0]);
                    //UnManagedMethods.DestroyIcon(hIconEx[0]);
                }
                return(icon);
            }
        }
Exemple #2
0
        private Icon getIcon(bool large)
        {
            // Get icon index and path:
            int iconIndex = 0;
            StringBuilder iconPath = new StringBuilder(260, 260);
            if (_linkA == null)
            {
                _linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
            }
            else
            {
                _linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
            }
            string iconFile = iconPath.ToString();

            // If there are no details set for the icon, then we must use
            // the shell to get the icon for the target:
            if (iconFile.Length == 0)
            {
                // Use the FileIcon object to get the icon:
                FileInfoOptions flags = FileInfoOptions.Icon | FileInfoOptions.Attributes;

                if (large)
                {
                    flags = flags | FileInfoOptions.LargeIcon;
                }
                else
                {
                    flags = flags | FileInfoOptions.SmallIcon;
                }
                FileIcon fileIcon = new FileIcon(Target, flags);
                return fileIcon.ShellIcon;
            }
            else
            {
                // Use ExtractIconEx to get the icon:
                IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero };
                int iconCount;
                if (large)
                {
                    iconCount = NativeMethods.ExtractIconEx(
                        iconFile,
                        iconIndex,
                        hIconEx,
                        null,
                        1);
                }
                else
                {
                    iconCount = NativeMethods.ExtractIconEx(
                        iconFile,
                        iconIndex,
                        null,
                        hIconEx,
                        1);
                }
                // If success then return as a GDI+ object
                Icon icon = null;
                if (hIconEx[0] != IntPtr.Zero)
                {
                    icon = Icon.FromHandle(hIconEx[0]);
                    //UnManagedMethods.DestroyIcon(hIconEx[0]);
                }
                return icon;
            }
        }