コード例 #1
0
        public void GetIconForParentOfSpecialFolder()
        {
            var testPath = KF_IID.ID_FOLDERID_Fonts;

            IdList parentIdList = null, relativeChild = null;
            var    retVal = PidlManager.GetParentIdListFromPath(testPath, out parentIdList, out relativeChild);

            Assert.IsTrue(retVal);

            // Child item is the desktop -> extract and return desktop icon
            if (parentIdList == null || relativeChild == null)
            {
                throw new NotImplementedException();
            }

            IntPtr parentPtr      = default(IntPtr);
            IntPtr relChildPtr    = default(IntPtr);
            IntPtr ptrShellFolder = default(IntPtr);
            IntPtr ptrExtractIcon = default(IntPtr);
            IntPtr smallHicon     = default(IntPtr);
            IntPtr largeHicon     = default(IntPtr);

            try
            {
                parentPtr   = PidlManager.IdListToPidl(parentIdList);
                relChildPtr = PidlManager.IdListToPidl(relativeChild);

                Assert.IsTrue(parentPtr != default(IntPtr));
                Assert.IsTrue(relChildPtr != default(IntPtr));

                Guid    guid = typeof(IShellFolder2).GUID;
                HRESULT hr   = NativeMethods.SHBindToParent(parentPtr, guid,
                                                            out ptrShellFolder, ref relChildPtr);

                Assert.IsTrue(hr == HRESULT.S_OK);

                using (var shellFolder = new ShellFolder(ptrShellFolder))
                {
                    Assert.IsTrue(shellFolder != null);

                    guid = typeof(IExtractIcon).GUID;
                    var pidls = new[] { relChildPtr };
                    hr = shellFolder.Obj.GetUIObjectOf(IntPtr.Zero, 1, pidls, guid,
                                                       IntPtr.Zero, out ptrExtractIcon);

                    Assert.IsTrue(hr == HRESULT.S_OK);

                    using (var extractIcon = new GenericCOMFolder <IExtractIcon>(ptrExtractIcon))
                    {
                        Assert.IsTrue(extractIcon != null);

                        var  iconFile = new StringBuilder(NativeMethods.MAX_PATH);
                        int  index    = -1;
                        uint pwFlags  = 0;
                        hr = extractIcon.Obj.GetIconLocation(0, iconFile, (uint)iconFile.Capacity,
                                                             ref index, ref pwFlags);

                        Assert.IsTrue(hr == HRESULT.S_OK);

                        Assert.IsFalse(string.IsNullOrEmpty(iconFile.ToString()));

                        hr = extractIcon.Obj.Extract(iconFile.ToString(), index,
                                                     ref smallHicon, ref largeHicon, 16);

                        Assert.IsTrue(hr == HRESULT.S_OK);
                        PngBitmapDecoder bitmapDecoder = null;
                        using (var memoryStream = new MemoryStream())
                        {
                            Icon.FromHandle(smallHicon).ToBitmap().Save(memoryStream, ImageFormat.Png);
                            memoryStream.Seek(0, SeekOrigin.Begin);

                            // Decode the icon
                            bitmapDecoder = new PngBitmapDecoder(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        }

                        Assert.IsTrue(bitmapDecoder != null);
                        Assert.IsTrue(bitmapDecoder.Frames != null);
                        Assert.IsTrue(bitmapDecoder.Frames.Count > 0);

                        Assert.IsTrue(bitmapDecoder.Frames[0] != null);

                        bitmapDecoder = null;
                        using (var memoryStream = new MemoryStream())
                        {
                            Icon.FromHandle(largeHicon).ToBitmap().Save(memoryStream, ImageFormat.Png);
                            memoryStream.Seek(0, SeekOrigin.Begin);

                            // Decode the icon
                            bitmapDecoder = new PngBitmapDecoder(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.Default);
                        }

                        Assert.IsTrue(bitmapDecoder != null);
                        Assert.IsTrue(bitmapDecoder.Frames != null);
                        Assert.IsTrue(bitmapDecoder.Frames.Count > 0);

                        Assert.IsTrue(bitmapDecoder.Frames[0] != null);
                    }
                }
            }
            finally
            {
                if (parentPtr != default(IntPtr))
                {
                    NativeMethods.ILFree(parentPtr);
                }

////                if (relChildPtr != default(IntPtr))    NOT NECESSARY
////                    NativeMethods.ILFree(relChildPtr);

                if (smallHicon != default(IntPtr))
                {
                    NativeMethods.DestroyIcon(smallHicon);
                }

                if (largeHicon != default(IntPtr))
                {
                    NativeMethods.DestroyIcon(largeHicon);
                }
            }
        }
コード例 #2
0
ファイル: IconHelper.cs プロジェクト: RaphaelK12/WSF
        /// <summary>
        /// Gets an icons reource id if available in the format:
        /// "filename, index"
        /// where the first part is a string and the 2nd part is a negativ integer number).
        ///
        /// This format is usally used by the Windows Shell libraries so we use it here
        /// to add missing ResourceIds.
        /// </summary>
        /// <param name="parentIdList"></param>
        /// <param name="filename"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        internal static bool GetIconResourceId(IdList parentIdList,
                                               out string filename,
                                               out int index)
        {
            filename = null;
            index    = -1;

            IntPtr parentPtr      = default(IntPtr);
            IntPtr relChildPtr    = default(IntPtr);
            IntPtr ptrShellFolder = default(IntPtr);
            IntPtr ptrExtractIcon = default(IntPtr);
            IntPtr smallHicon     = default(IntPtr);
            IntPtr largeHicon     = default(IntPtr);

            try
            {
                if (parentIdList.Size == 0)
                {
                    parentPtr   = PidlManager.IdListToPidl(parentIdList);
                    relChildPtr = PidlManager.IdListToPidl(IdList.Create());
                }
                else
                {
                    IdList parIdList = null, relChildIdList = null;
                    PidlManager.GetParentChildIdList(parentIdList, out parIdList, out relChildIdList);

                    parentPtr   = PidlManager.IdListToPidl(parIdList);
                    relChildPtr = PidlManager.IdListToPidl(relChildIdList);
                }

                if (parentPtr == default(IntPtr) || relChildPtr == default(IntPtr))
                {
                    return(false);
                }

                Guid    guid = typeof(IShellFolder2).GUID;
                HRESULT hr   = NativeMethods.SHBindToParent(parentPtr, guid,
                                                            out ptrShellFolder, ref relChildPtr);

                if (hr != HRESULT.S_OK)
                {
                    return(false);
                }

                using (var shellFolder = new ShellFolder(ptrShellFolder))
                {
                    if (shellFolder == null)
                    {
                        return(false);
                    }

                    guid = typeof(IExtractIcon).GUID;
                    var pidls = new IntPtr[] { relChildPtr };
                    hr = shellFolder.Obj.GetUIObjectOf(IntPtr.Zero, 1, pidls, guid,
                                                       IntPtr.Zero, out ptrExtractIcon);

                    if (hr != HRESULT.S_OK)
                    {
                        return(false);
                    }

                    using (var extractIcon = new GenericCOMFolder <IExtractIcon>(ptrExtractIcon))
                    {
                        if (extractIcon == null)
                        {
                            return(false);
                        }

                        var  iconFile = new StringBuilder(NativeMethods.MAX_PATH);
                        uint pwFlags  = 0;

                        hr = extractIcon.Obj.GetIconLocation(0, iconFile,
                                                             (uint)iconFile.Capacity,
                                                             ref index, ref pwFlags);

                        if (hr != HRESULT.S_OK)
                        {
                            return(false);
                        }

                        if (string.IsNullOrEmpty(iconFile.ToString()))
                        {
                            return(false);
                        }

                        filename = iconFile.ToString();

                        return(true);
                    }
                }
            }
            finally
            {
                if (parentPtr != default(IntPtr))
                {
                    NativeMethods.ILFree(parentPtr);
                }

                ////if (relChildPtr != default(IntPtr))
                ////    Shell32.ILFree(relChildPtr);

                if (smallHicon != default(IntPtr))
                {
                    NativeMethods.DestroyIcon(smallHicon);
                }

                if (largeHicon != default(IntPtr))
                {
                    NativeMethods.DestroyIcon(largeHicon);
                }
            }
        }