VC 实现文件关联

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VC/MFC 设置程序与文件关联与双击文件获取文件路径(2008-10-10 10:59:15)

分类:编程技术标签:vc/mfc设置程序与文件关联双击

文件获取文件路径it

如何设置程序与文件关联并且双击关联文件时获取文件的路径呢?

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

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

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());

}

相关文档
最新文档