我给自己序列化出的文件设置了自己的后缀名,现在想为我的文件设置一个文件图标,该怎么做呢?
最好是在通过编程的方式完成,网上大多是delphi的例子,我想用vc实现。
望高手指点!

解决方案 »

  1.   

    操作注册表.注册你的文件后缀名.
    //strExt :扩展名 ,nTypeIcon:图标 strFileTypeDescribe 类型描述
    BOOL RegisterFileType(string strExt, int nTypeIcon, CString &strFileTypeDescribe)
    {
    CString strExtentName = strExt.c_str(); // 要检测的扩展名(例如: ".txt")
    CString strAppKey = TEXT("abc"); // 扩展名在注册表中的键值(例如: "abc.txtfile")
    strAppKey += strExtentName + TEXT("file");
    CString strAppName; // 要关联的应用程序名(例如: "C:\MyApp\MyApp.exe")
    CString strDefaultIcon; // 扩展名为strAppName的图标文件(例如: "C:\MyApp\MyApp.exe,0")
    CString strDescribe; // 文件类型描述
    HKEY hKey; strDescribe = strFileTypeDescribe; // 获取程序路径
    TCHAR szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, MAX_PATH);
    strAppName = szPath; #pragma region<</* 更新HKEY_CLASSES_ROOT下的文件类型相关选中项目 */>>

    RegCreateKey(HKEY_CLASSES_ROOT, strExtentName, &hKey);
    RegSetValue(hKey, TEXT(""), REG_SZ, strAppKey, strAppKey.GetLength());
    RegCloseKey(hKey); RegCreateKey(HKEY_CLASSES_ROOT, strAppKey, &hKey);
    RegSetValue(hKey, TEXT(""), REG_SZ, strDescribe, strDescribe.GetLength());
    RegCloseKey(hKey); strDefaultIcon = strAppName;
    CString strTemp;
    strTemp.Format(TEXT(",%d"), nTypeIcon )
    strDefaultIcon += strTemp;
    strTemp = strAppKey + TEXT("\\DefaultIcon");
    RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
    RegSetValue(hKey, TEXT(""), REG_SZ, strDefaultIcon, strDefaultIcon.GetLength());
    RegCloseKey(hKey); CString strCmd = TEXT("Open");
    strTemp = strAppKey + TEXT("\\Shell");
    RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
    RegSetValue(hKey, TEXT(""), REG_SZ, strCmd, strCmd.GetLength());
    RegCloseKey(hKey); strTemp = strAppKey + TEXT("\\Shell\\Open\\Command");
    RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
    strCmd = strAppName + TEXT(" \"open %1\"");
    RegSetValue(hKey, TEXT(""), REG_SZ, strCmd, strCmd.GetLength());
    RegCloseKey(hKey); #pragma endregion #pragma region<</* 更新HKEY_CURRENT_USER */>> CString strNewKey;
    strNewKey = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\");
    strNewKey += strExtentName;
    RegCreateKey(HKEY_CURRENT_USER, strNewKey, &hKey);
    strTemp = TEXT("Progid");
    #ifdef UNICODE
    RegSetValueEx(hKey, strTemp, 0, REG_SZ, (BYTE*)strAppKey.GetBuffer(), strAppKey.GetLength()*2);
    #else
    RegSetValueEx(hKey, strTemp, 0, REG_SZ, (BYTE*)strAppKey.GetBuffer(), strAppKey.GetLength());
    #endif
    RegCloseKey(hKey); strNewKey += TEXT("\\OpenWithProgids");
    RegCreateKey(HKEY_CURRENT_USER, strNewKey, &hKey);
    strTemp = TEXT("");
    RegSetValue(hKey, strAppKey, REG_NONE, strTemp, strTemp.GetLength());
    RegCloseKey(hKey); #pragma endregion
     
    return TRUE;
    }
      

  2.   

    修改注册表吧,把你的文件名字添加到注册表中,具体怎么做的忘记了,以前做过把特定文件名关联自己写的程序代码中用到过!!就像TXT文件关联记事本,双击就可以调用记事本打开他。。
      

  3.   

    具体原理:
    以后缀.aaa文件为例:
     
    Windows Registry Editor Version 5.00
     
    [HKEY_CLASSES_ROOT\.aaa]
    @="aaafile"
     
    [HKEY_CLASSES_ROOT\aaafile]
    @="PHP Script"
     
    [HKEY_CLASSES_ROOT\aaafile\DefaultIcon]
    @="D:\\Program Files\\PHP Designer 2007 - Professional\\phpdesigner2007.exe,0"
     
    [HKEY_CLASSES_ROOT\aaafile\shell\open]
    @="PHP Designer 2007"
     
    [HKEY_CLASSES_ROOT\aaafile\shell\open\command]
    @="D:\\Program Files\\PHP Designer 2007 - Professional\\phpdesigner2007.exe %1"