勤能补拙,厚积薄发

合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下
随笔 - 19, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

[转载]VC/MFC 设置程序与文件关联与双击文件获取文件路径

本文转载自: http://blog.sina.com.cn/s/blog_4b44e1c00100bb0s.html
如何设置程序与文件关联并且双击关联文件时获取文件的路径呢?

一般来说可以通过写注册表的方式实现,在函数入口处实现功能。

例如在VC/MFC中,可以在应用程序的C**App.cpp文件中InitInstance()函数中实现该功能。

  1 #include  < string >
  2 using   namespace  std;
  3 // 关联文件的后缀名,如"txt"、"doc"等
  4 string   m_csExtension;
  5 string   m_csShellOpenCommand;
  6 string   m_csDocumentShellOpenCommand;
  7 // 注册表中文件夹类名
  8 string   m_csDocumentClassName;
  9 // 关联文件的默认图标
 10 string   m_csDocumentDefaultIcon;
 11
 12 ////// /赋值函数 //////
 13 void  SetExtension( LPCTSTR szExtension )
 14 {
 15     m_csExtension  =  szExtension;
 16 }

 17 void  SetShellOpenCommand( LPCTSTR szShellOpenCommand )
 18 {
 19     m_csShellOpenCommand  =  szShellOpenCommand;
 20 }

 21 void  SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
 22 {
 23     m_csDocumentShellOpenCommand  =  szDocumentShellOpenCommand;
 24 }

 25 void  SetDocumentClassName( LPCTSTR szDocumentClassName )
 26 {
 27     m_csDocumentClassName  =  szDocumentClassName;
 28 }

 29 void  SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
 30 {
 31     m_csDocumentDefaultIcon  =  szDocumentDefaultIcon;
 32 }

 33 ////// /赋值函数 //////
 34
 35 ////// 关键函数:实现写注册表的函数 ////// //

 36 BOOL SetRegistryValue(
 37         HKEY hOpenKey,
 38         LPCTSTR szKey,
 39         LPCTSTR szValue,
 40         LPCTSTR szData
 41         ) {
 42      //  validate input
 43      if ! hOpenKey  ||   ! szKey  ||   ! szKey[ 0 ||
 44              ! szValue  ||   ! szData ) {
 45         ::SetLastError(E_INVALIDARG);
 46          return  FALSE;
 47     }

 48     BOOL  bRetVal  =  FALSE;
 49     DWORD dwDisposition;
 50     DWORD dwReserved  =   0 ;
 51     HKEY   hTempKey  =  (HKEY) 0 ;
 52      //  length specifier is in bytes, and some TCHAR
 53      //  are more than 1 byte each
 54     DWORD dwBufferLength  =  lstrlen(szData)  *   sizeof (TCHAR);
 55      //  Open key of interest
 56      //  Assume all access is okay and that all keys will be stored to file
 57      //  Utilize the default security attributes
 58      if ( ERROR_SUCCESS  ==  ::RegCreateKeyEx(hOpenKey, szKey, dwReserved,
 59                 (LPTSTR) 0 , REG_OPTION_NON_VOLATILE, KEY_SET_VALUE,  0 ,
 60                  & hTempKey,  & dwDisposition) ) {
 61
 62          //  dwBufferLength must include size of terminating nul
 63          //  character when using REG_SZ with RegSetValueEx function
 64         dwBufferLength  +=   sizeof (TCHAR);
 65
 66          if ( ERROR_SUCCESS  ==  ::RegSetValueEx(hTempKey, (LPTSTR)szValue,
 67                     dwReserved, REG_SZ, (LPBYTE)szData, dwBufferLength) ) {
 68             bRetVal  =  TRUE;
 69         }

 70     }

 71      //  close opened key
 72      if ( hTempKey ) {
 73         ::RegCloseKey(hTempKey);
 74     }

 75      return  bRetVal;
 76 }

 77
 78 BOOL RegSetExtension( void )
 79 {
 80      if ( m_csExtension.empty() )
 81      {
 82          return  FALSE;
 83     }

 84     std:: string  csKey  =   " . "   +  m_csExtension;
 85     SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(),  "" , m_csDocumentClassName.c_str());
 86      if ! m_csShellOpenCommand.empty() )
 87      {
 88         csKey  +=   " \\shell\\open\\command " ;
 89         SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(),  "" , m_csShellOpenCommand.c_str());
 90     }

 91      return  TRUE;
 92 }

 93
 94 BOOL RegSetDocumentType( void )
 95 {
 96      if ( m_csDocumentClassName.empty())
 97      {
 98          return  FALSE;
 99     }

100     std:: string  csKey  =  m_csDocumentClassName;
101     SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(),  "" , m_csDocumentDescription.c_str());
102      //  DefaultIcon
103      if ! m_csDocumentDefaultIcon.empty() )
104      {
105         csKey   =  m_csDocumentClassName;
106         csKey  +=   " \\DefaultIcon " ;
107         SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(),  "" , m_csDocumentDefaultIcon.c_str());
108     }

109      //  shell\open\command
110      if ! m_csShellOpenCommand.empty() )
111      {
112         csKey   =  m_csDocumentClassName;
113         csKey  +=   " \\shell\\open\\command " ;
114         SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(),  "" , m_csShellOpenCommand.c_str());
115     }

116      return  TRUE;
117 }

118
119 BOOL RegSetAllInfo( void )
120 {
121     RegSetExtension();
122     RegSetDocumentType();
123      return  TRUE;
124 }

125
126 // 注册程序与文件后缀名的关联
127 void  RegisterFileAndProgram()
128 {
129      /// /一个应用程序与多个文件后缀关联 /// /
130 #define  strExternsionLength 4
131     LPCTSTR strExtension[]  =
132      {
133          " bmp " ,
134          " jpg " ,
135          " jpe " ,
136          " jpeg "
137     }
;
138     CGCFileTypeAccess TheFTA;
139     TCHAR     szProgPath[MAX_PATH  *   2 ];
140      // 获取程序路径
141     ::GetModuleFileName(NULL, szProgPath,  sizeof (szProgPath) / sizeof (TCHAR));
142     CString csTempText;
143      for ( int  i  =   0 ; i  <  strExternsionLength;  ++ i)
144      {
145          // 设置程序需要关联的后缀名,如"txt" "doc" 等
146         SetExtension(strExtension[i]);
147         csTempText.Format( "" % s "  %s " ,szProgPath, "" % 1 "" );
148         SetShellOpenCommand(csTempText);
149         SetDocumentShellOpenCommand(csTempText);
150          // 设置注册表中文件类的别名,例如可以是程序名称:**.exe
151         SetDocumentClassName( " ** " );
152
153          //  use first icon in program
154         csTempText   =  szProgPath;
155         csTempText  +=   " ,0 " ;
156         SetDocumentDefaultIcon(csTempText);
157         RegSetAllInfo();
158     }

159 }

160
161 // 入口函数:初始化所需的操作
162 BOOL C ** App::InitInstance()
163 {
164      ///////// /
165      // ***Code***
166      ////// //
167      // 注册程序与文件后缀名的关联
168     RegisterFileAndProgram();
169      //  分析标准外壳命令、DDE、打开文件操作的命令行
170     CCommandLineInfo cmdInfo;
171     ParseCommandLine(cmdInfo);
172      // 获取命令行传过来的参数:双击打开的文件的文件路径名称
173     CString strFilePathName  =  cmdInfo.m_strFileName;
174      //  调度在命令行中指定的命令。如果
175      //  用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
176      if  ( ! ProcessShellCommand(cmdInfo))
177          return  FALSE;
178      ///////// /
179      // 通过获取的strFilePathName文件名称实现相关操作 //
180      // Add code here //
181      ////// //
182      return  TRUE;
183 }

184
185


posted on 2011-12-01 09:16 lee007 阅读(2619) 评论(1)  编辑 收藏 引用 所属分类: 转载

评论

# re: [转载]VC/MFC 设置程序与文件关联与双击文件获取文件路径  回复  更多评论   

CGCFileTypeAccess TheFTA;
此话怎讲?
2012-09-27 15:41 | yiruirui

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理