文件关联程序

当打开一个文件时,系统会自动找到打开它的程序。我一直对此有点兴趣,今天终于在自己的程序中也实现了。步骤是这样的,首先在注册表中注册我的程序和哪些文件格式(后缀名)关联,然后在程序中响应双击打开文件,因为启动程序的时候,MFC默认的是命令是FileNew。不够以前我的程序是改成FileNothing,因为我的是MDI程序。所以今天加了如果命令行传来文件路径就要把它打开。也即是cmdInfo.m_strFileName不为空。可是刚开始遇到一点小问题,因为路径会截断空格,比如"C:/Documents and Settings/桌面/draw1.sch"变成了"C:/Documents"空格之后被截断了。后来我用::GetCommandLine()自己获取命令行中的文件路径。那么就是在程序中添加如下语句。

1.注册关联文件

BOOL CDesignSysApp::InitApplication()
{

        /*..............

 

          你的语句

 

        ................*/

 

        // 注册文件关联
       // strExe: 要检测的扩展名(例如: ".txt")
       // strAppName: 要关联的应用程序名(例如: "C:/MyApp/MyApp.exe")
       // strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
       // strDefaultIcon: 扩展名为strAppName的图标文件(例如: "C:/MyApp/MyApp.exe,0")
       // strDescribe: 文件类型描述

       char strAppName[MAXLEN];
       ::GetModuleFileName(NULL, strAppName, MAXLEN);
       char *strExt = ".sch";
       char *strAppKey = "schfile";
       char strDefaultIcon[MAXLEN];

       // char *strDescribe = "sch";
       HKEY hKey;
       char strTemp[1024];

       if(ERROR_SUCCESS == RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey))
      {
              RegSetValue(hKey,"",REG_SZ,strAppKey,strlen(strAppKey)+1);
              RegCloseKey(hKey);
      }
      sprintf(strTemp,"%s//DefaultIcon",strAppKey);
      if(ERROR_SUCCESS == RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey))
      {
              sprintf(strDefaultIcon, "%s%s", strAppName, ", 0");
              RegSetValue(hKey,"",REG_SZ,strDefaultIcon,strlen(strDefaultIcon)+1);
              RegCloseKey(hKey);
      }
   
      sprintf(strTemp,"%s//Shell",strAppKey);
      if(ERROR_SUCCESS == RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey))
      {
              RegSetValue(hKey,"",REG_SZ,"Open",strlen("Open")+1);
              RegCloseKey(hKey);
      }
   
      sprintf(strTemp,"%s//Shell//Open//Command",strAppKey);
      if(ERROR_SUCCESS == RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey))
     {
             sprintf(strTemp,"%s %s",strAppName, "%1");
             RegSetValue(hKey,"",REG_SZ,strTemp,strlen(strTemp)+1);
             RegCloseKey(hKey);
     }

}

2.打开文件

BOOL CDesignSysApp::InitInstance()

{

          /*.............

 

          你的程序

 

          ..............*/     

          CCommandLineInfo cmdInfo;

          cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
          ParseCommandLine(cmdInfo);

          if(cmdInfo.m_strFileName != _T(""))
          {
                   CString str = ::GetCommandLine();

                   int nPos = str.ReverseFind('//');
                   if(nPos != -1)
                  {
                          str = str.Right(str.GetLength() - nPos - 1);
                  }
                   if(str != _T(""))
                  {
                          cmdInfo.m_strFileName = str;
                          cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen;
                  }
          }
          if (!ProcessShellCommand(cmdInfo))
          return FALSE;

}

posted @ 2013-06-09 15:33  FREE小宝  阅读(331)  评论(0编辑  收藏  举报