栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现添加桌面右键新建菜单

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现添加桌面右键新建菜单

对于程序员来说,新建一个cpp文件是再频繁不过的事情了。

为了方便,我们习惯在桌面右键新建文件,而不是新建一个文本文档,然后修改后缀名。

百度谷歌查询了一下,终于知道如何添加注册表。

手痒,抽出时间用cpp写了一个程序,方便以后操作。

客户需求是永远无法满足的,经同学测试,陆续写了三个版本。

接下来直接贴代码~

第一个版本,只能添加c、cpp、java三种后缀。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

char s[1024], buffer[128], result[1024*4];

void work_1() {
  system("reg add "HKEY_CLASSES_ROOT\.c\ShellNew" /v "NullFile" /t REG_SZ");
}

void work_2() {
  system("reg add "HKEY_CLASSES_ROOT\.cpp\ShellNew" /v "NullFile" /t REG_SZ");
}

void work_3() {
  system("reg add "HKEY_CLASSES_ROOT\.java\ShellNew" /v "NullFile" /t REG_SZ");
}

int main() {
  printf("Add registry for C, C++ and Javan");
  printf("Author: HaipznSchool: HDUn");
  printf("1 for C;n2 for C++;n3 for Java.n");
  printf("Example: 12 to add C and C++.n");
  printf("Please make choice(s): ");
  gets(s);
  for (int i = 0; s[i] != ''; ++i) {
    printf("Working...n");
    if (s[i] == '1') work_1();
    else if (s[i] == '2') work_2();
    else if (s[i] == '3') work_3();
    else printf("%c is a wrong enter!n", s[i]);
  }
  system("pause");
  return 0;
}

第二个版本,精简了代码,支持添加用户输入的后缀。

 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 using namespace std;
 
 char a[1024];
 char b[1024] = "reg add "HKEY_CLASSES_ROOT\.";
 char c[1024] = "\ShellNew" /v "NullFile" /t REG_SZ";
 
 void work(char* a) {
   strcat(b, a);
   strcat(b, c);
   system(b);
 }
 
 int main() {
   printf("Function: Add registry to build a new file simply!n");
   printf("Author: HaipznSchool: HDUn");
   printf("Example: Enter c to add C and enter cpp to add C++.n");
   printf("Your opion: ");
   gets(a);
   work(a);
   system("pause");
   return 0;
 }

第三个版本,支持多次添加,并允许删除已添加的注册表。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

char key[1024];
char a[1024];

void add(char* t) {
  char b[1024] = "reg add "HKEY_CLASSES_ROOT\.";
  char c[1024] = "\ShellNew" /v "NullFile" /t REG_SZ";
  strcat(b, t);
  strcat(b, c);
  system(b);
}

void del(char* t) {
  char d[1024] = "reg delete "HKEY_CLASSES_ROOT\.";
  char e[1024] = "\ShellNew" /f";
  strcat(d, t);
  strcat(d, e);
  system(d);
}

int main() {
  printf("Function: Build a new file simply!n");
  printf("Author: HaipznSchool: HDUn");
  printf("Example: Enter "c" to add C and enter "cpp" to add C++;n");
  printf("     Enter "-c" to delete C.n");
  do {
    printf("Your opion: ");
    gets(a);
    if (a[0] == '-') del(a + 1);
    else add(a);
    printf("Enter "r" to run again or any other key to quit: ");
    gets(key);
  } while (key[0] == 'r');
  return 0;
}

打包下载地址:

http://xiazai.jb51.net/201601/yuanma/Regedity(jb51.net).zip

注意,如果系统提示缺少某dll文件,请到网上下载,并复制到C:WindowsSystem32目录下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/64652.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号