1

我有一个要与我的程序关联的文件类型。我可以使该类型的每个文件都具有相同的标准图标,例如所有 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;
        }
    }
}

我已经按照本页底部的说明编辑了我的注册表(感谢 arx)。

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

4

0 回答 0