VC注册表文件关联,图标关联

实例下载地址:http://download.csdn.net/detail/qq_23992597/9467938

前段时间需要编写文件关联的东西,就是特定文件格式和某一个应用程序关联起来,就像单击.doc的文件就会用word软件打开一样。

这个东西通过注册表来实现,明白了道理之后其实编程很简单,几句话既可以解决问题。程序基本上市从网上下载下来的,然后按自己的要求改了一下,实现的功能有:将特定后缀格式的文件和我们的应用程序关联,并修改特定文件格式显示的图标。

程序代码如下;

函数实现的流程大致如下:首先打开注册表中HKEY_LOCAL_MACHINE项下的Software/Classes/.img,通过返回值可以判断在注册表中有没有.img这个子项,如果没有表示我们未在注册表中添加东西,如果有,则可以查询.img这个子项中是否有我们所需要的某一个子键,通过这两步来判断我们是否将需要关联的信息添加到系统中。

注册表添加的函数也比较简单

通过RegCreateKey函数在注册表的HKEY_CLASSES_ROOT项下添加.img的一个子项,这个HKEY_CLASSES_ROOT子项其实就对应到HKEY_LOCAL_MACHINE/Software/Classes/下,这个网上有解释,不信的话,可以windows运行菜单中输入regedit打开注册表编辑器详细查看。之后通过RegSetValueEx函数给新添加的.img项中添加字段。文件关联重要的是:

 

RegCreateKey(HKEY_CLASSES_ROOT,"imgfile//shell//open//command",&hkey);

RegSetValueEx(hkey,NULL,NULL,REG_EXPAND_SZ,(CONST BYTE *)a,strlen(a));

这个"imgfile//shell//open//command" 在注册表中就是将设置以img为后缀文件的open命令,将本程序的路径名设置为打开函数默认的程序即可。

RegisterFileRelation(_T("imgfile"),a,_T("imgfile"),_T("C://img.ico"),_T("img file"));

该函数是设置img文件的图标的,这个函数也是网上下的,其中第三个参数就是ico图标的地址,其他参数我也不是很明白什么意思,都写为一样就可以了。

将这两个函数拷到vc++工程的app类中,将RegeditToSystem()函数添加到InitInstance()函数中,就可以运行了。

通过单击.img的文件既可以打开我们所写的程序了,我们肯定需要获得.img文件名和路径,在InitInstance()函数中添加

 

CCommandLineInfo cmdInfo;

ParseCommandLine(cmdInfo);

CString strFilePathName = cmdInfo.m_strFileName;

通过上面几个函数就可以得到文件名了。

 

 

void CBeta1App::RegeditToSystem()

{

 

//

BOOL isRegedit=true;

//AfxMessageBox(strFilePathName);

HKEY hKEY;//定义有关的hKEY,在查询结束时要关闭

//打开与路径 data_Set相关的hKEY

// LPCTSTR data_Set="Software//Microsoft//Windows NT//CurrentVersion//Accessibility//";

LPCTSTR data_Set="Software//Classes//.img//";

//访问注册表,hKEY则保存此函数所打开的键的句柄

long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_READ,&hKEY));

if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行

{

//AfxMessageBox("错误:未在注册表中找到该键");

isRegedit=false;

// return false;

}

//查询有关的数据

LPBYTE owner_Get=new BYTE[80];//定义用户姓名 owner_Get

DWORD type_1=REG_SZ;//定义数据类型

DWORD cbData_1=80;//定义数据长度

long ret1=::RegQueryValueEx(hKEY,"smartphone",NULL,&type_1,owner_Get,&cbData_1);

if(ret1!=ERROR_SUCCESS)

{

//AfxMessageBox("错误:无法查询注册表中该键值的内容");

isRegedit=false;

//return false;

}

RegCloseKey(hKEY);

//

// TODO: Add your control notification handler code here

char a[MAX_PATH];

CString str="imgfile";

//char b[]={ ' ',"",'%1',"",'/0'};

HKEY hkey;

if(isRegedit==false)

{

RegCreateKey(HKEY_CLASSES_ROOT,".img",&hkey);//创建键".test",键值为testfile

RegSetValueEx(hkey,NULL,NULL,REG_SZ,(CONST BYTE *)str.GetBuffer( str.GetLength() ),strlen(str));

/

RegSetValueEx(hkey,"smartphone",NULL,REG_SZ,(unsigned char *)"smartphone1.0",strlen("smartphone1.0"));

/

RegCloseKey(hkey);

GetModuleFileName(NULL,a,MAX_PATH);//获取自身路径

//lstrcat(a,b);

//AfxMessageBox(_T(a));

RegCreateKey(HKEY_CLASSES_ROOT,"imgfile//shell//open//command",&hkey);创建键"testfile//shell//open//command",键值为本程序指针

RegSetValueEx(hkey,NULL,NULL,REG_EXPAND_SZ,(CONST BYTE *)a,strlen(a));

BOOL temp=RegisterFileRelation(_T("imgfile"),a,_T("imgfile"),_T("C://img.ico"),_T("img file"));

// if(!temp)

// AfxMessageBox(_T("注册表添加成功!"));

}

else

; // AfxMessageBox(_T("注册表添加成功!"));

}

 

BOOL CBeta1App::RegisterFileRelation( const CString& strExtension,  

const CString& strApplicationName,

const CString& strRunKey,

const CString& strDefaultIcon,  

const CString& strDescription )

{

HKEY hRegeditKey ;

if ( RegOpenKey( HKEY_CLASSES_ROOT, strExtension, &hRegeditKey ) != ERROR_SUCCESS )

{

if ( RegCreateKey( HKEY_CLASSES_ROOT, strExtension, &hRegeditKey ) != ERROR_SUCCESS )

{

AfxMessageBox(_T("errors11!"));

return FALSE ;

}

}

RegSetValue( hRegeditKey, _T(""), REG_SZ, strRunKey, strRunKey.GetLength()+1 ) ;

RegCloseKey( hRegeditKey ) ;

if ( RegOpenKey( HKEY_CLASSES_ROOT, strRunKey, &hRegeditKey ) != ERROR_SUCCESS )

{

if ( RegCreateKey( HKEY_CLASSES_ROOT, strRunKey, &hRegeditKey ) != ERROR_SUCCESS )

{

AfxMessageBox(_T("errors22!"));

return FALSE ;

}

}

RegSetValue( hRegeditKey, _T(""), REG_SZ, strDescription, strDescription.GetLength()+1 ) ;

RegCloseKey( hRegeditKey ) ;

if ( RegOpenKey( HKEY_CLASSES_ROOT, strRunKey, &hRegeditKey ) != ERROR_SUCCESS )

{

if ( RegCreateKey( HKEY_CLASSES_ROOT, strRunKey, &hRegeditKey ) != ERROR_SUCCESS )

{

AfxMessageBox(_T("errors33!"));

return FALSE ;

}

}

CString strRegedit ;

strRegedit.Format( _T("%s//DefaultIcon"), strRunKey ) ;

if ( RegOpenKey( HKEY_CLASSES_ROOT, strRegedit, &hRegeditKey ) != ERROR_SUCCESS )

{

if ( RegCreateKey( HKEY_CLASSES_ROOT, strRegedit, &hRegeditKey ) != ERROR_SUCCESS )

{

AfxMessageBox(_T("errors44!"));

return FALSE ;

}

}

if ( strDefaultIcon.IsEmpty() )

{

strRegedit.Format( _T("/"%s/",3"), strApplicationName ) ;

}

else

{

strRegedit = strDefaultIcon ;

}

RegSetValue( hRegeditKey, _T(""), REG_SZ, strRegedit, strRegedit.GetLength()+1 ) ;

RegCloseKey( hRegeditKey ) ;

strRegedit.Format( _T("%s//Shell"), strRunKey ) ;

if ( RegOpenKey( HKEY_CLASSES_ROOT, strRegedit, &hRegeditKey ) != ERROR_SUCCESS )

{

if ( RegCreateKey( HKEY_CLASSES_ROOT, strRegedit, &hRegeditKey ) != ERROR_SUCCESS )

{

AfxMessageBox(_T("errors55!"));

return FALSE ;

}

}

strRegedit = _T("Open") ;

RegSetValue( hRegeditKey, _T(""), REG_SZ, strRegedit, strRegedit.GetLength()+1 ) ;

RegCloseKey( hRegeditKey ) ;

strRegedit.Format( _T("%s//Shell//Open//Command"), strRunKey ) ;

if ( RegOpenKey( HKEY_CLASSES_ROOT, strRegedit, &hRegeditKey ) != ERROR_SUCCESS )

{

if ( RegCreateKey( HKEY_CLASSES_ROOT, strRegedit, &hRegeditKey ) != ERROR_SUCCESS )

{

AfxMessageBox(_T("errors66!"));

return FALSE ;

}

}

strRegedit.Format( _T("/"%s/" /"%%1/""), strApplicationName ) ;

RegSetValue( hRegeditKey, _T(""), REG_SZ, strRegedit, strRegedit.GetLength()+1 ) ;

RegCloseKey( hRegeditKey ) ;

//AfxMessageBox(_T("errors777!"));

return TRUE ;

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值