如何实现文件关联的每个文件缩略图?

时间:2013-05-17 15:41:02

标签: windows winapi thumbnails file-association

我有一个我想要与我的程序关联的文件类型。我可以使该类型的每个文件都具有相同的标准图标,如所有HTML文件看起来相同或所有txt文件,但我想要做的是让每个文件显示其缩略图的预览,更像是如何jpg, bmp和png显示该特定图像文件的缩略图。

我主要在C#工作,但我知道这样的事情可能需要一点点(或很多)C ++才能做我想做的事情。如果需要的话,我也可以。我不知道从哪里开始,因为我以前从未尝试过这个。一个小小的谷歌搜索说COM对象会这样做,但我还需要更多的东西。

修改 这是我到目前为止所做的:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices.ComTypes;

namespace APKIconHandler {

    [ComImport()]
    [Guid("000214fa-0000-0000-c000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    //http://msdn.microsoft.com/en-us/library/windows/desktop/bb761852(v=vs.85).aspx
    interface IExtractIcon {
        /// <summary>
        /// Gets the location and index of an icon.
        /// </summary>
        /// <param name="uFlags">One or more of the following values. This parameter can also be NULL.use GIL_ Consts</param>
        /// <param name="szIconFile">A pointer to a buffer that receives the icon location. The icon location is a null-terminated string that identifies the file that contains the icon.</param>
        /// <param name="cchMax">The size of the buffer, in characters, pointed to by pszIconFile.</param>
        /// <param name="piIndex">A pointer to an int that receives the index of the icon in the file pointed to by pszIconFile.</param>
        /// <param name="pwFlags">A pointer to a UINT value that receives zero or a combination of the following value</param>
        /// <returns></returns>
        ///
        [PreserveSig]
        int GetIconLocation(IExtractIconuFlags uFlags, [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 2)] StringBuilder szIconFile, int cchMax, out int piIndex, out IExtractIconpwFlags pwFlags);

        /// <summary>
        /// Extracts an icon image from the specified location.
        /// </summary>
        /// <param name="pszFile">A pointer to a null-terminated string that specifies the icon location.</param>
        /// <param name="nIconIndex">The index of the icon in the file pointed to by pszFile.</param>
        /// <param name="phiconLarge">A pointer to an HICON value that receives the handle to the large icon. This parameter may be NULL.</param>
        /// <param name="phiconSmall">A pointer to an HICON value that receives the handle to the small icon. This parameter may be NULL.</param>
        /// <param name="nIconSize">The desired size of the icon, in pixels. The low word contains the size of the large icon, and the high word contains the size of the small icon. The size specified can be the width or height. The width of an icon always equals its height.</param>
        /// <returns>
        /// Returns S_OK if the function extracted the icon, or S_FALSE if the calling application should extract the icon.
        /// </returns>
        [PreserveSig]
        int Extract([MarshalAs(UnmanagedType.LPWStr)] string pszFile, uint nIconIndex, out IntPtr phiconLarge, out IntPtr phiconSmall, uint nIconSize);
    }
    [Flags()]
    public enum IExtractIconuFlags:uint
    {
        GIL_ASYNC=0x0020,
        GIL_DEFAULTICON =0x0040,
        GIL_FORSHELL =0x0002,
        GIL_FORSHORTCUT =0x0080,
        GIL_OPENICON = 0x0001,
        GIL_CHECKSHIELD = 0x0200
    }

    [Flags()]
    public enum IExtractIconpwFlags : uint
    {
        GIL_DONTCACHE = 0x0010,
        GIL_NOTFILENAME = 0x0008,
        GIL_PERCLASS = 0x0004,
        GIL_PERINSTANCE = 0x0002,
        GIL_SIMULATEDOC = 0x0001,
        GIL_SHIELD = 0x0200,//Windows Vista only
        GIL_FORCENOSHIELD = 0x0400//Windows Vista only
    }

    [Flags]
    public enum IconHandlerReturnFlags {
        SimulateDoc = 0x1,
        PerInstance = 0x2,
        PerClass = 0x4,
        NotFilename = 0x8,
        DontCache = 0x10
    }

    public class APKHandler : IExtractIcon, IPersistFile {
        private const int S_OK = 0;
        private const int S_FALSE = 1;

        [ComRegisterFunctionAttribute]
        public void DllRegisterDll() { }

        public void GetClassID(out Guid g) {
            g = new Guid("405a310a-b439-49b9-894a-cc55ffc6e91d");
        }

        public void GetCurFile(out String str) {
            str = "CurFile";
        }

        public int IsDirty() {
            return S_OK;
        }

        public void Load(string pszFileName, int dwMode) {
            File.AppendAllText(@"C:\ApkHandler.txt", "Load :" + pszFileName + " , " + dwMode.ToString() + Environment.NewLine);
        }

        public void Save(string pszFileName, bool save) {
            File.AppendAllText(@"C:\ApkHandler.txt", "Save :" + pszFileName + " , " + save + Environment.NewLine);
        }

        public void SaveCompleted(string pszFileName) {
            File.AppendAllText(@"C:\ApkHandler.txt", "SaveCompleted :" + pszFileName + Environment.NewLine);
        }

        public int GetIconLocation(IExtractIconuFlags uFlags, StringBuilder szIconFile, int cchMax, out int piIndex, out IExtractIconpwFlags pwFlags)//Using IExtractIcon and IPersistFile.Load
        {
            piIndex = 0;
            pwFlags = 0;
            try {
                pwFlags = IExtractIconpwFlags.GIL_PERCLASS | IExtractIconpwFlags.GIL_DONTCACHE | IExtractIconpwFlags.GIL_NOTFILENAME;
                File.AppendAllText(@"C:\ApkHandler.txt", "GetIconLocation...");
                return S_OK;
            } catch (Exception e) {
                File.AppendAllText(@"C:\ApkHandler.txt", "GetIconLocation " + e.Message);
                return S_FALSE;
            }
        }


        public int Extract(string pszFile, uint nIconIndex, out IntPtr phiconLarge, out IntPtr phiconSmall, uint nIconSize)//Using IExtractIcon 
        {
            File.AppendAllText(@"C:\ApkHandler.txt", "Extract...");
            phiconSmall = phiconLarge = IntPtr.Zero;
            return S_OK;
        }
    }
}

我已将我的注册表编辑为this page指示的底部(感谢arx)。

我一遍又一遍地玩弄和调整,并且还没有调用任何函数(如ApkHandler.txt所示,从未出现过)。我禁用了UAC所以我认为在根目录中创建文件没有任何权限问题,我在调试时一直这样做。我努力不要感到沮丧,但这真的是在我的皮肤下。

0 个答案:

没有答案