如何設置程序與文件關聯並且雙擊關聯文件時獲取文件的路徑呢?
一般來說可以通過寫註冊表的方式實現,在函數入口處實現功能。
例如在VC/MFC中,可以在應用程序的C**App.cpp文件中InitInstance()函數中實現該功能。
#include <string>
using namespace std;
//關聯文件的後綴名,如"txt"、"doc"等
string m_csExtension;
string m_csShellOpenCommand;
string m_csDocumentShellOpenCommand;
//註冊表中文件夾類名
string m_csDocumentClassName;
//關聯文件的默認圖標
string m_csDocumentDefaultIcon;
///////賦值函數//////
void SetExtension( LPCTSTR szExtension )
{
m_csExtension = szExtension;
}
void SetShellOpenCommand( LPCTSTR szShellOpenCommand )
{
m_csShellOpenCommand = szShellOpenCommand;
}
void SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
{
m_csDocumentShellOpenCommand = szDocumentShellOpenCommand;
}
void SetDocumentClassName( LPCTSTR szDocumentClassName )
{
m_csDocumentClassName = szDocumentClassName;
}
void SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
{
m_csDocumentDefaultIcon = szDocumentDefaultIcon;
}
///////賦值函數//////
//////關鍵函數:實現寫註冊表的函數////////
BOOL SetRegistryValue(
HKEY hOpenKey,
LPCTSTR szKey,
LPCTSTR szValue,
LPCTSTR szData
){
// validate input
if( !hOpenKey || !szKey || !szKey[0] ||
!szValue || !szData ){
::SetLastError(E_INVALIDARG);
return FALSE;
}
BOOL bRetVal = FALSE;
DWORD dwDisposition;
DWORD dwReserved = 0;
HKEY hTempKey = (HKEY)0;
// 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)0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0,
&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 RegSetExtension(void)
{
if( m_csExtension.empty() )
{
return FALSE;
}
std::string csKey = "." + m_csExtension;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csDocumentClassName.c_str());
if( !m_csShellOpenCommand.empty() )
{
csKey += "//shell//open//command";
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csShellOpenCommand.c_str());
}
return TRUE;
}
BOOL RegSetDocumentType(void)
{
if( m_csDocumentClassName.empty())
{
return FALSE;
}
std::string csKey = m_csDocumentClassName;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csDocumentDescription.c_str());
// DefaultIcon
if( !m_csDocumentDefaultIcon.empty() )
{
csKey = m_csDocumentClassName;
csKey += "//DefaultIcon";
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csDocumentDefaultIcon.c_str());
}
// shell/open/command
if( !m_csShellOpenCommand.empty() )
{
csKey = m_csDocumentClassName;
csKey += "//shell//open//command";
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.c_str(), "", m_csShellOpenCommand.c_str());
}
return TRUE;
}
BOOL RegSetAllInfo(void)
{
RegSetExtension();
RegSetDocumentType();
return TRUE;
}
//註冊程序與文件後綴名的關聯
void RegisterFileAndProgram()
{
////一個應用程序與多個文件後綴關聯////
#define strExternsionLength 4
LPCTSTR strExtension[] =
{
"bmp",
"jpg",
"jpe",
"jpeg"
};
CGCFileTypeAccess TheFTA;
TCHAR szProgPath[MAX_PATH * 2];
//獲取程序路徑
::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
CString csTempText;
for(int i = 0; i < strExternsionLength; ++i)
{
//設置程序需要關聯的後綴名,如"txt" "doc" 等
SetExtension(strExtension[i]);
csTempText.Format("/"%s/" %s",szProgPath,"/"%1/"");
SetShellOpenCommand(csTempText);
SetDocumentShellOpenCommand(csTempText);
//設置註冊表中文件類的別名,例如可以是程序名稱:**.exe
SetDocumentClassName("**");
// use first icon in programcsTempText = szProgPath;
csTempText += ",0";
SetDocumentDefaultIcon(csTempText);
RegSetAllInfo();
}
}
//入口函數:初始化所需的操作
BOOL C**App::InitInstance()
{
//////////
//***Code***
////////
//註冊程序與文件後綴名的關聯
RegisterFileAndProgram();
// 分析標準外殼命令、DDE、打開文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
//獲取命令行傳過來的參數:雙擊打開的文件的文件路徑名稱
CString strFilePathName = cmdInfo.m_strFileName;
// 調度在命令行中指定的命令。如果
// 用 /RegServer、/Register、/Unregserver 或 /Unregister 啓動應用程序,則返回 FALSE。
if (!ProcessShellCommand(cmdInfo))
return FALSE;
//////////
//通過獲取的strFilePathName文件名稱實現相關操作//
//Add code here//
////////
return TRUE;
}