现在的位置: 首页 > 综合 > 正文

自定义文件格式注册和图标设置

2013年06月18日 ⁄ 综合 ⁄ 共 3614字 ⁄ 字号 评论关闭

我们很多时候可能会有这样的一种需求,即要生成自己的文件格式,然后将这种文件格式注册到操作系统中,双击就会使用特定的程序来打开,并且我们还想为这种文件格式设置一个图标和缩略。本文章解决的是注册文件格式和设置文件的图标,至于缩略图的设置请看我空间的另外一篇文章。
 注册文件需要用到下面两个函数:
BOOL RegeditYourFile(string ExtName)
{
    CString csKey = ExtName.c_str();
    CString m_csDocumentClassName = "YourFileExtName.file";

    ::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));//本执行程序的路径,用来在命令模式下也可以打开该
    //类型的文件

    CString csTempText;

    // just pass file path in quotes on command line
    csTempText  = szProgPath;
    csTempText += " /"%1/"";

    CString m_csShellOpenCommand = csTempText;
    SetRegistryValue(HKEY_CLASSES_ROOT, csKey, "", m_csDocumentClassName);//注册一个.VTemplate后缀的主键

    if( !m_csShellOpenCommand.IsEmpty() )
    {//注册命令模式下打开该类型文件的执行程序
        csKey += "//shell//open//command";
        SetRegistryValue(HKEY_CLASSES_ROOT, csKey, "", m_csShellOpenCommand);
    }

    csKey = m_csDocumentClassName;

    CString m_csDocumentDescription = "";
   
    csTempText  = szProgPath;

    csTempText += ",1";
    CString m_csDocumentDefaultIcon = csTempText;

    SetRegistryValue(HKEY_CLASSES_ROOT, csKey, "", m_csDocumentDescription);

    // DefaultIcon
    if( !m_csDocumentDefaultIcon.IsEmpty() )
    {
        csKey  = m_csDocumentClassName;
        csKey += "//DefaultIcon";
        SetRegistryValue(HKEY_CLASSES_ROOT, csKey, "", m_csDocumentDefaultIcon);
    }

    // shell/open/command
    if( !m_csShellOpenCommand.IsEmpty() )
    {
        csKey  = m_csDocumentClassName;
        csKey += "//shell//open//command";
        SetRegistryValue(HKEY_CLASSES_ROOT, csKey, "", m_csShellOpenCommand);
    }
   return TRUE;
}

BOOL SetRegistryValue(HKEY hOpenKey,LPCTSTR szKey,LPCTSTR szValue,LPCTSTR szData)
{
    // validate input
    if( !hOpenKey || !szKey || !szKey[0] ||
       !szValue || !szData )
    {
        ::SetLastError(E_INVALIDARG);
        return FALSE;
    }

    BOOL     bRetVal = FALSE;
    DWORD    dwDisposition;
    DWORD    dwReserved = 0;
    HKEY      hTempKey = (HKEY)0;

            // length specifier is in bytes, and some TCHAR
            // are more than 1 byte each
    DWORD    dwBufferLength = lstrlen(szData) * sizeof(TCHAR);

    // Open key of interest
    // Assume all access is okay and that all keys will be stored to file
    // Utilize the default security attributes
    if( ERROR_SUCCESS == ::RegCreateKeyEx(hOpenKey, szKey, dwReserved,
                                 (LPTSTR)0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0,
                                 &hTempKey, &dwDisposition) )
    {

    // dwBufferLength must include size of terminating nul
    // character when using REG_SZ with RegSetValueEx function
        dwBufferLength += sizeof(TCHAR);

    if( ERROR_SUCCESS == ::RegSetValueEx(hTempKey, (LPTSTR)szValue,
                                         dwReserved, REG_SZ, (LPBYTE)szData,
                                         dwBufferLength) )
    {
                            bRetVal = TRUE;
    }
    }

    // close opened key
    if( hTempKey )
    {
        ::RegCloseKey(hTempKey);
    }

    return bRetVal;其中里面的原理主要是操作注册表,具体自己看。
比如,你想为自己的自定义文件格式.why注册,则可以这样子调用上面的函数:

RegeditYourFile(.why);注册完文件格式以后,要给这种文件格式设置一个图标,这要用到下面的函数:

BOOL  ModifyIcon(LPCSTR   ExtName,   LPCSTR   IconFile)
{
        BOOL   ret; 
          LONG   nLen; 
          char   Key[65]; 
          char   buf[MAX_PATH]; 
  
          nLen   =   sizeof(Key); 
          if   (RegQueryValue(HKEY_CLASSES_ROOT,ExtName,Key, 
                  &nLen)   !=   ERROR_SUCCESS) 
                  return   FALSE; 
          if   (Key[0]=='/0')   return   FALSE; 
          strcat(Key,   "//DefaultIcon"); 
          strcpy(buf,   IconFile);     strcat(buf,   ",   0"); 
          ret   =   RegSetValue(HKEY_CLASSES_ROOT,   Key, 
                  REG_SZ,   buf,   sizeof(buf)+1)   ==   ERROR_SUCCESS; 
          SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT, 
                  0,   0); 
          return   ret; 
}然后你可以这样子调用:

ModifyIcon(".why","C://myicon.ico");搞定。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Feisy/archive/2008/07/24/2705628.aspx

抱歉!评论已关闭.