C++实现添加桌面右键新建菜单
对于程序员来说,新建一个cpp文件是再频繁不过的事情了。
为了方便,我们习惯在桌面右键新建文件,而不是新建一个文本文档,然后修改后缀名。
百度谷歌查询了一下,终于知道如何添加注册表。
手痒,抽出时间用cpp写了一个程序,方便以后操作。
客户需求是永远无法满足的,经同学测试,陆续写了三个版本。
接下来直接贴代码~
第一个版本,只能添加c、cpp、java三种后缀。
/*
*Author:Haipz
*School:HDU
*FileName:registry1.0.cpp
*/
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<cfloat>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
usingnamespacestd;
chars[1024],buffer[128],result[1024*4];
voidwork_1(){
system("regadd\"HKEY_CLASSES_ROOT\\.c\\ShellNew\"/v\"NullFile\"/tREG_SZ");
}
voidwork_2(){
system("regadd\"HKEY_CLASSES_ROOT\\.cpp\\ShellNew\"/v\"NullFile\"/tREG_SZ");
}
voidwork_3(){
system("regadd\"HKEY_CLASSES_ROOT\\.java\\ShellNew\"/v\"NullFile\"/tREG_SZ");
}
intmain(){
printf("AddregistryforC,C++andJava\n");
printf("Author:Haipz\nSchool:HDU\n");
printf("1forC;\n2forC++;\n3forJava.\n");
printf("Example:12toaddCandC++.\n");
printf("Pleasemakechoice(s):");
gets(s);
for(inti=0;s[i]!='\0';++i){
printf("Working...\n");
if(s[i]=='1')work_1();
elseif(s[i]=='2')work_2();
elseif(s[i]=='3')work_3();
elseprintf("%cisawrongenter!\n",s[i]);
}
system("pause");
return0;
}
第二个版本,精简了代码,支持添加用户输入的后缀。
/*
*Author:Haipz
*School:HDU
*FileName:registry2.0.cpp
*/
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<cfloat>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
usingnamespacestd;
chara[1024];
charb[1024]="regadd\"HKEY_CLASSES_ROOT\\.";
charc[1024]="\\ShellNew\"/v\"NullFile\"/tREG_SZ";
voidwork(char*a){
strcat(b,a);
strcat(b,c);
system(b);
}
intmain(){
printf("Function:Addregistrytobuildanewfilesimply!\n");
printf("Author:Haipz\nSchool:HDU\n");
printf("Example:EnterctoaddCandentercpptoaddC++.\n");
printf("Youropion:");
gets(a);
work(a);
system("pause");
return0;
}
第三个版本,支持多次添加,并允许删除已添加的注册表。
/*
*Author:Haipz
*School:HDU
*FileName:registry2.0.cpp
*/
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<cfloat>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
usingnamespacestd;
charkey[1024];
chara[1024];
voidadd(char*t){
charb[1024]="regadd\"HKEY_CLASSES_ROOT\\.";
charc[1024]="\\ShellNew\"/v\"NullFile\"/tREG_SZ";
strcat(b,t);
strcat(b,c);
system(b);
}
voiddel(char*t){
chard[1024]="regdelete\"HKEY_CLASSES_ROOT\\.";
chare[1024]="\\ShellNew\"/f";
strcat(d,t);
strcat(d,e);
system(d);
}
intmain(){
printf("Function:Buildanewfilesimply!\n");
printf("Author:Haipz\nSchool:HDU\n");
printf("Example:Enter\"c\"toaddCandenter\"cpp\"toaddC++;\n");
printf("Enter\"-c\"todeleteC.\n");
do{
printf("Youropion:");
gets(a);
if(a[0]=='-')del(a+1);
elseadd(a);
printf("Enter\"r\"torunagainoranyotherkeytoquit:");
gets(key);
}while(key[0]=='r');
return0;
}
打包下载地址:
http://xiazai.jb51.net/201601/yuanma/Regedity(jb51.net).zip
注意,如果系统提示缺少某dll文件,请到网上下载,并复制到C:\Windows\System32目录下。