一般来说,很多软件都会有自定义后缀的文件,比如.cpp、.doc等,那么如果我们想把这些后缀与我们的软件关联起来,如何做呢
#pragma once #include "StdAfx.h" class CRegExtension
{
public:
CRegExtension();
~CRegExtension();
void SetExtension( LPCTSTR szExtension );
void SetShellOpenCommand( LPCTSTR szShellOpenCommand );
void SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand );
void SetDocumentClassName( LPCTSTR szDocumentClassName );
void SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon );
BOOL SetRegistryValue(HKEY hOpenKey, LPCTSTR szKey, LPCTSTR szValue, LPCTSTR szData);
BOOL RegSetExtension(void);
BOOL RegSetDocumentType(void);
BOOL RegSetAllInfo(void);
void RegisterFileAndProgram(); private:
CStdString m_csExtension;
CStdString m_csShellOpenCommand;
CStdString m_csDocumentShellOpenCommand;
CStdString m_csDocumentClassName;
CStdString m_csDocumentDefaultIcon;
CStdString m_csDocumentDescription;
};
#include "StdAfx.h"
#include "RegExtension.h" CRegExtension::CRegExtension()
{ } CRegExtension::~CRegExtension()
{ } void CRegExtension::SetExtension( LPCTSTR szExtension )
{
m_csExtension = szExtension;
}
void CRegExtension::SetShellOpenCommand( LPCTSTR szShellOpenCommand )
{
m_csShellOpenCommand = szShellOpenCommand;
}
void CRegExtension::SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
{
m_csDocumentShellOpenCommand = szDocumentShellOpenCommand;
}
void CRegExtension::SetDocumentClassName( LPCTSTR szDocumentClassName )
{
m_csDocumentClassName = szDocumentClassName;
}
void CRegExtension::SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
{
m_csDocumentDefaultIcon = szDocumentDefaultIcon;
} BOOL CRegExtension::SetRegistryValue(HKEY hOpenKey, LPCTSTR szKey, LPCTSTR szValue, LPCTSTR szData
){
// validate input
if( !hOpenKey || !szKey || !szKey[] ||
!szValue || !szData ){
::SetLastError(E_INVALIDARG);
return FALSE;
}
BOOL bRetVal = FALSE;
DWORD dwDisposition;
DWORD dwReserved = ;
HKEY hTempKey = (HKEY);
// 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), REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, ,
&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;
} BOOL CRegExtension::RegSetExtension(void)
{
if( m_csExtension.IsEmpty() )
{
return FALSE;
}
CStdString csKey = CStdString(_T(".")) + m_csExtension;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentClassName.GetData());
if( !m_csShellOpenCommand.IsEmpty() )
{
csKey += _T("\\shell\\open\\command");
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csShellOpenCommand.GetData());
}
return TRUE;
} BOOL CRegExtension::RegSetDocumentType(void)
{
if( m_csDocumentClassName.IsEmpty())
{
return FALSE;
}
CStdString csKey = m_csDocumentClassName;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentDescription.GetData());
// DefaultIcon
if( !m_csDocumentDefaultIcon.IsEmpty() )
{
csKey = m_csDocumentClassName;
csKey += _T("\\DefaultIcon");
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentDefaultIcon.GetData());
}
// shell\open\command
if( !m_csShellOpenCommand.IsEmpty() )
{
csKey = m_csDocumentClassName;
csKey += _T("\\shell\\open\\command");
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csShellOpenCommand.GetData());
}
return TRUE;
} BOOL CRegExtension::RegSetAllInfo(void)
{
RegSetExtension();
RegSetDocumentType();
return TRUE;
} void CRegExtension::RegisterFileAndProgram()
{
////一个应用程序与多个文件后缀关联////
#define strExternsionLength 1
LPCTSTR strExtension[] =
{
_T("myproc")
};
//CGCFileTypeAccess TheFTA;
TCHAR szProgPath[MAX_PATH * ];
//获取程序路径
::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
CStdString csTempText;
for(int i = ; i < strExternsionLength; ++i)
{
//设置程序需要关联的后缀名,如"txt" "doc" 等
SetExtension(strExtension[i]);
csTempText.Format(_T("\"%s\" %s"),szProgPath,_T("\"%1\""));
SetShellOpenCommand(csTempText.GetData());
SetDocumentShellOpenCommand(csTempText.GetData());
//设置注册表中文件类的别名,例如可以是程序名称:MyProc.exe
SetDocumentClassName(_T("MyProc")); // use first icon in program
csTempText = szProgPath;
csTempText += _T(",0");
SetDocumentDefaultIcon(csTempText.GetData());
RegSetAllInfo();
}
}
调用:
一般是在初始化HINSTANCE 的地方:文章来源地址:https://www.yii666.com/article/764357.html
如bool CWndShadow::Initialize(HINSTANCE hInstance)
网址:yii666.com我这里使用的是CWndShadow类,这是一个开源的阴影类,可以google一下
//注册程序与文件后缀名的关联
CRegExtension reg;
reg.RegisterFileAndProgram();
// 分析标准外壳命令、DDE、打开文件操作的命令行
if (__argc > )
{
m_csFilePath = __targv[];//我使用这种方式获取双击的后缀文件的所在路径
}
这样就可以双击运行软件了文章地址https://www.yii666.com/article/764357.html