Example #1
0
        private Icon _GetIcon(bool large)
        {
            // Get icon index and path:
            int iconIndex;
            var iconPath = new StringBuilder(260, 260);

            if (linkA == null)
            {
                linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
            }
            else
            {
                linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
            }

            var 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:
                var flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES;
                if (large)
                {
                    flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON;
                }
                else
                {
                    flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON;
                }

                var fileIcon = new FileIcon(Target, flags);
                return(fileIcon.ShellIcon);
            }
            else
            {
                // Use ExtractIconEx to get the icon:
                var hIconEx = new IntPtr[1] {
                    IntPtr.Zero
                };

                if (large)
                {
                    UnManagedMethods.ExtractIconEx(iconFile, iconIndex, hIconEx, null, 1);
                }
                else
                {
                    UnManagedMethods.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]);
                }

                return(icon);
            }
        }
Example #2
0
        private Icon getIcon(bool large)
        {
            int iconIndex;
            var iconPath = new StringBuilder(260, 260);

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

            if (iconFile.Length == 0)
            {
                var flags = FileIcon.ShGetFileInfoConstants.SHGFI_ICON |
                            FileIcon.ShGetFileInfoConstants.SHGFI_ATTRIBUTES;
                if (large)
                {
                    flags = flags | FileIcon.ShGetFileInfoConstants.SHGFI_LARGEICON;
                }
                else
                {
                    flags = flags | FileIcon.ShGetFileInfoConstants.SHGFI_SMALLICON;
                }
                var fileIcon = new FileIcon(Target, flags);
                return(fileIcon.ShellIcon);
            }
            var hIconEx = new[] { IntPtr.Zero };

            if (large)
            {
                NativeMethods.ExtractIconEx(
                    iconFile,
                    iconIndex,
                    hIconEx,
                    null,
                    1);
            }
            else
            {
                NativeMethods.ExtractIconEx(
                    iconFile,
                    iconIndex,
                    null,
                    hIconEx,
                    1);
            }
            Icon icon = null;

            if (hIconEx[0] != IntPtr.Zero)
            {
                icon = Icon.FromHandle(hIconEx[0]);
                //UnManagedMethods.DestroyIcon(hIconEx[0]);
            }
            return(icon);
        }
        /// <summary>
        /// アイコンのファイルとインデックスを取得する
        /// </summary>
        /// <param name="iconFile">アイコンのファイル</param>
        /// <param name="iconIndex">アイコンのインデックス</param>
        private void GetIconLocation(out string iconFile, out int iconIndex)
        {
            StringBuilder iconFileBuffer = new StringBuilder(MAX_PATH, MAX_PATH);

            if (isUnicodeEnvironment)
            {
                shellLinkW.GetIconLocation(iconFileBuffer, iconFileBuffer.Capacity, out iconIndex);
            }
            else
            {
                shellLinkA.GetIconLocation(iconFileBuffer, iconFileBuffer.Capacity, out iconIndex);
            }

            iconFile = iconFileBuffer.ToString();
        }
Example #4
0
        public void Load(string linkFileName)
        {
            if (null == linkFileName)
            {
                throw new ArgumentNullException("linkFileName", "A name of the link file cannot be null");
            }
            if (!File.Exists(linkFileName))
            {
                throw new FileNotFoundException("Link not found", linkFileName);
            }
            new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, linkFileName).Demand();

            object sl = null;

            try
            {
                Type slType = Type.GetTypeFromCLSID(CLSID_ShellLink);
                sl = Activator.CreateInstance(slType);
                IPersistFile pf = sl as IPersistFile;
                pf.Load(linkFileName, 0);

                int           showCmd;
                StringBuilder builder    = new StringBuilder(INFOTIPSIZE);
                IShellLinkW   shellLinkW = sl as IShellLinkW;
                if (null == shellLinkW)
                {
                    IShellLinkA shellLinkA = sl as IShellLinkA;
                    if (null == shellLinkA)
                    {
                        ThrowInvalidComObjectException();
                    }
                    shellLinkA.GetArguments(builder, builder.Capacity);
                    this.arguments = builder.ToString();
                    shellLinkA.GetDescription(builder, builder.Capacity);
                    this.description = builder.ToString();
                    shellLinkA.GetHotkey(out this.hotkey);
                    shellLinkA.GetIconLocation(builder, builder.Capacity, out this.iconIndex);
                    this.iconPath = builder.ToString();
                    Win32FindDataA wfd;
                    shellLinkA.GetPath(builder, builder.Capacity, out wfd, SLGP_UNCPRIORITY);
                    this.path = builder.ToString();
                    shellLinkA.GetShowCmd(out showCmd);
                    shellLinkA.GetWorkingDirectory(builder, builder.Capacity);
                    this.workingDirectory = builder.ToString();
                }
                else
                {
                    shellLinkW.GetArguments(builder, builder.Capacity);
                    this.arguments = builder.ToString();
                    shellLinkW.GetDescription(builder, builder.Capacity);
                    this.description = builder.ToString();
                    shellLinkW.GetHotkey(out this.hotkey);
                    shellLinkW.GetIconLocation(builder, builder.Capacity, out this.iconIndex);
                    this.iconPath = builder.ToString();
                    Win32FindDataW wfd;
                    shellLinkW.GetPath(builder, builder.Capacity, out wfd, SLGP_UNCPRIORITY);
                    this.path = builder.ToString();
                    shellLinkW.GetShowCmd(out showCmd);
                    shellLinkW.GetWorkingDirectory(builder, builder.Capacity);
                    this.workingDirectory = builder.ToString();
                }
                this.showCmd = (ShowCommand)showCmd;
            }
            finally
            {
                if (null != sl)
                {
                    Marshal.ReleaseComObject(sl);
                }
            }
            // This object is not eligible for the garbage collection during this method call
            GC.KeepAlive(this);
        }
Example #5
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:
                FileIcon.ShGetFileInfoConstants flags = FileIcon.ShGetFileInfoConstants.ShgfiIcon |
                                                        FileIcon.ShGetFileInfoConstants.ShgfiAttributes;
                if (large)
                {
                    flags = flags | FileIcon.ShGetFileInfoConstants.ShgfiLargeicon;
                }
                else
                {
                    flags = flags | FileIcon.ShGetFileInfoConstants.ShgfiSmallicon;
                }
                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 = 0;
                if (large)
                {
                    iconCount = UnManagedMethods.ExtractIconEx(
                        iconFile,
                        iconIndex,
                        hIconEx,
                        null,
                        1);
                }
                else
                {
                    iconCount = UnManagedMethods.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);
            }
        }