Window 注册程序关联后缀文件,双击运行

一般来说,很多软件都会有自定义后缀的文件,比如.cpp、.doc等,那么如果我们想把这些后缀与我们的软件关联起来,如何做呢

 

 1 #pragma once
 2 
 3 #include "StdAfx.h"
 4 
 5 class CRegExtension
 6 {
 7 public:
 8     CRegExtension();
 9     ~CRegExtension();
10     void  SetExtension( LPCTSTR szExtension );
11     void  SetShellOpenCommand( LPCTSTR szShellOpenCommand );
12     void  SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand );
13     void  SetDocumentClassName( LPCTSTR szDocumentClassName );
14     void  SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon );
15     BOOL SetRegistryValue(HKEY hOpenKey, LPCTSTR szKey, LPCTSTR szValue, LPCTSTR szData);
16     BOOL RegSetExtension(void);
17     BOOL RegSetDocumentType(void);
18     BOOL RegSetAllInfo(void);
19     void RegisterFileAndProgram();
20 
21 private:
22     CStdString m_csExtension;
23     CStdString m_csShellOpenCommand;
24     CStdString m_csDocumentShellOpenCommand;
25     CStdString m_csDocumentClassName;
26     CStdString m_csDocumentDefaultIcon;
27     CStdString m_csDocumentDescription;
28 };

 

  1 #include "StdAfx.h"
  2 #include "RegExtension.h"
  3 
  4 
  5 CRegExtension::CRegExtension()
  6 {
  7 
  8 }
  9 
 10 CRegExtension::~CRegExtension()
 11 {
 12 
 13 }
 14 
 15 void  CRegExtension::SetExtension( LPCTSTR szExtension )
 16 {
 17     m_csExtension  =  szExtension;
 18 } 
 19 void  CRegExtension::SetShellOpenCommand( LPCTSTR szShellOpenCommand )
 20 {
 21     m_csShellOpenCommand  =  szShellOpenCommand;
 22 } 
 23 void  CRegExtension::SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
 24 {
 25     m_csDocumentShellOpenCommand  =  szDocumentShellOpenCommand;
 26 } 
 27 void  CRegExtension::SetDocumentClassName( LPCTSTR szDocumentClassName )
 28 {
 29     m_csDocumentClassName  =  szDocumentClassName;
 30 }
 31 void  CRegExtension::SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
 32 {
 33     m_csDocumentDefaultIcon  =  szDocumentDefaultIcon;
 34 }
 35 
 36 BOOL CRegExtension::SetRegistryValue(HKEY hOpenKey, LPCTSTR szKey, LPCTSTR szValue, LPCTSTR szData
 37 ){
 38      // validate input
 39      if( !hOpenKey || !szKey || !szKey[0] ||
 40       !szValue || !szData ){
 41       ::SetLastError(E_INVALIDARG);
 42       return FALSE;
 43      }
 44      BOOL  bRetVal = FALSE;
 45      DWORD dwDisposition;
 46      DWORD dwReserved = 0;
 47      HKEY   hTempKey = (HKEY)0;
 48      // length specifier is in bytes, and some TCHAR
 49      // are more than 1 byte each
 50      DWORD dwBufferLength = lstrlen(szData) * sizeof(TCHAR);
 51      // Open key of interest
 52      // Assume all access is okay and that all keys will be stored to file
 53      // Utilize the default security attributes
 54      if( ERROR_SUCCESS == ::RegCreateKeyEx(hOpenKey, szKey, dwReserved,
 55       (LPTSTR)0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0,
 56       &hTempKey, &dwDisposition) ){
 57       
 58       // dwBufferLength must include size of terminating nul
 59       // character when using REG_SZ with RegSetValueEx function
 60       dwBufferLength += sizeof(TCHAR);
 61       
 62       if( ERROR_SUCCESS == ::RegSetValueEx(hTempKey, (LPTSTR)szValue,
 63        dwReserved, REG_SZ, (LPBYTE)szData, dwBufferLength) ){
 64        bRetVal = TRUE;
 65       }
 66      }
 67      // close opened key
 68      if( hTempKey ){
 69       ::RegCloseKey(hTempKey);
 70      }
 71      return bRetVal;
 72 }
 73 
 74 BOOL CRegExtension::RegSetExtension(void)
 75 {
 76     if( m_csExtension.IsEmpty() )
 77      {
 78        return FALSE;
 79      }
 80      CStdString csKey = CStdString(_T(".")) + m_csExtension;
 81      SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentClassName.GetData());
 82      if( !m_csShellOpenCommand.IsEmpty() )
 83      {
 84       csKey += _T("\\shell\\open\\command");
 85       SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csShellOpenCommand.GetData());
 86      }
 87      return TRUE;
 88 }
 89 
 90 BOOL CRegExtension::RegSetDocumentType(void)
 91 {
 92     if( m_csDocumentClassName.IsEmpty())
 93      {
 94       return FALSE;
 95      }
 96      CStdString csKey = m_csDocumentClassName;
 97      SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentDescription.GetData());
 98      // DefaultIcon
 99      if( !m_csDocumentDefaultIcon.IsEmpty() )
100      {
101       csKey  = m_csDocumentClassName;
102       csKey += _T("\\DefaultIcon");
103       SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentDefaultIcon.GetData());
104      }
105      // shell\open\command
106      if( !m_csShellOpenCommand.IsEmpty() )
107      {
108       csKey  = m_csDocumentClassName;
109       csKey += _T("\\shell\\open\\command");
110       SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csShellOpenCommand.GetData());
111      }
112       return TRUE;
113 }
114 
115 BOOL CRegExtension::RegSetAllInfo(void)
116 {
117        RegSetExtension();
118        RegSetDocumentType();
119        return TRUE;
120 }
121 
122 void CRegExtension::RegisterFileAndProgram()
123 {
124  ////一个应用程序与多个文件后缀关联////
125      #define strExternsionLength 1
126      LPCTSTR strExtension[] =
127      {
128       _T("myproc")
129      };
130      //CGCFileTypeAccess TheFTA;
131      TCHAR     szProgPath[MAX_PATH * 2];
132      //获取程序路径
133      ::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
134      CStdString csTempText;
135      for(int i = 0; i < strExternsionLength; ++i)
136      {
137          //设置程序需要关联的后缀名,如"txt" "doc" 等
138          SetExtension(strExtension[i]);
139          csTempText.Format(_T("\"%s\" %s"),szProgPath,_T("\"%1\""));
140          SetShellOpenCommand(csTempText.GetData());
141          SetDocumentShellOpenCommand(csTempText.GetData());
142         //设置注册表中文件类的别名,例如可以是程序名称:MyProc.exe
143          SetDocumentClassName(_T("MyProc"));
144 
145          // use first icon in program
146          csTempText  = szProgPath;
147          csTempText += _T(",0");
148          SetDocumentDefaultIcon(csTempText.GetData());
149          RegSetAllInfo();
150     }
151 }

 

调用:

一般是在初始化HINSTANCE 的地方:

如bool CWndShadow::Initialize(HINSTANCE hInstance)

我这里使用的是CWndShadow类,这是一个开源的阴影类,可以google一下

 

//注册程序与文件后缀名的关联
CRegExtension reg;
reg.RegisterFileAndProgram();
// 分析标准外壳命令、DDE、打开文件操作的命令行
if (__argc > 1)
{
m_csFilePath = __targv[1];//我使用这种方式获取双击的后缀文件的所在路径
}

这样就可以双击运行软件了

posted @ 2016-09-29 11:15  george_cw  阅读(988)  评论(0编辑  收藏  举报