zoukankan      html  css  js  c++  java
  • VC++ 实现文件与应用程序关联

     

    日常工作中,doc文件直接双击后,就能启动word软件,并读取该文档的内容在软件中显示,这都得益于注册表的配置,我们的软件也需要实现这样的功能,该如何写注册表以及写入哪些内容呢?下面的两个函数就能实现这个功能。CheckFileRelation是检查注册表中是否已经将我们期待的文件格式与相应软件关联了;RegisterFileRelation是直接往注册表中写入相关的key和value。

    /****************************************************
    * 检测文件关联情况
    * strExt: 要检测的扩展名(例如: ".txt")
    * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
    * 返回TRUE: 表示已关联,FALSE: 表示未关联
    
    ******************************************************/
    
    BOOL CheckFileRelation(const char *strExt, const char *strAppKey)
    {
        int nRet=FALSE;
        HKEY hExtKey;
        char szPath[_MAX_PATH]; 
        DWORD dwSize=sizeof(szPath); 
        if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
        {
            RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
            if(_stricmp(szPath,strAppKey)==0)
            {
                nRet=TRUE;
            }
            RegCloseKey(hExtKey);
            return nRet;
        }
        return nRet;
    }
    /****************************************************
    
    * 注册文件关联
    * strExe: 要检测的扩展名(例如: ".txt")
    * strAppName: 要关联的应用程序名(例如: "C:/MyApp/MyApp.exe")
    * strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
    * strDefaultIcon: 扩展名为strAppName的图标文件(例如: *"C:/MyApp/MyApp.exe,0")
    * strDescribe: 文件类型描述
    
    ****************************************************/
    
    void RegisterFileRelation(char *strExt, char *strAppName, char *strAppKey, char *strDefaultIcon, char *strDescribe)
    {
        char strTemp[_MAX_PATH];
        HKEY hKey;
    
        RegCreateKey(HKEY_CLASSES_ROOT, strExt, &hKey);
        RegSetValue(hKey, "", REG_SZ, strAppKey, strlen(strAppKey) + 1);
        RegCloseKey(hKey);
    
        RegCreateKey(HKEY_CLASSES_ROOT, strAppKey, &hKey);
        RegSetValue(hKey, "", REG_SZ, strDescribe, strlen(strDescribe) + 1);
        RegCloseKey(hKey);
    
        sprintf_s(strTemp, "%s\DefaultIcon", strAppKey);
        RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
        RegSetValue(hKey, "", REG_SZ, strDefaultIcon, strlen(strDefaultIcon) + 1);
        RegCloseKey(hKey);
    
        sprintf_s(strTemp, "%s\Shell", strAppKey);
        RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
        RegSetValue(hKey, "", REG_SZ, "Open", strlen("Open") + 1);
        RegCloseKey(hKey);
    
        sprintf_s(strTemp, "%s\Shell\Open\Command", strAppKey);
        RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
        sprintf_s(strTemp, "%s %%1", strAppName);
        RegSetValue(hKey, "", REG_SZ, strTemp, strlen(strTemp) + 1);
        RegCloseKey(hKey);
    }

    有了这两个函数后,可以实现文档和软件的关联了,但是双击文档后,又是如何读取文档的内容的呢?这里主要是用到了命令行参数,我们需要在CTestApp的InitInstance函数获取命令行参数,如:

    BOOL CTestApp::InitInstance()
    {
        //这里的m_lpCmdLine是CWinApp的成员变量,双击文档时,文档的路径会传给该参数
        CString pathName = m_lpCmdLine;
        if (pathName != _T(""))
        {
          //TODO:读取文件、解析文件、呈现文件
        }
    }
  • 相关阅读:
    bzoj4543 长链剖分
    tarjan算法
    uoj36 玛里苟斯 高斯消元做法
    狄利克雷卷积
    斜率优化
    将一个工作簿拆分为多个工作表
    如何制作Excel斜线表头
    逻辑函数(IF函数)
    逻辑函数(AND,OR,NOT)
    Excel中提取英文,数值和编码(LEN函数)
  • 原文地址:https://www.cnblogs.com/lujin49/p/5040513.html
Copyright © 2011-2022 走看看