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

 更新时间:2016年01月05日 09:36:03   投稿:hebedich  
本文给大家汇总了3个版本的C++实现添加桌面右键新建菜单的代码,陆陆续续写的,有需要的小伙伴可以根据自己的需求来选择

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

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

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

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

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

接下来直接贴代码~

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

/*
 * Author: Haipz
 * School: HDU
 * File Name: 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>
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 Java\n");
  printf("Author: Haipz\nSchool: HDU\n");
  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] != '\0'; ++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;
}

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

 /*
 * Author: Haipz
 * School: HDU
 * File Name: 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>
 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: Haipz\nSchool: HDU\n");
   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;
 }

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

/*
 * Author: Haipz
 * School: HDU
 * File Name: 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>
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: Haipz\nSchool: HDU\n");
  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:\Windows\System32目录下。

相关文章

  • Qt读取和写入配置(ini)文件

    Qt读取和写入配置(ini)文件

    ini文件在windows系统中可以存储需要持久保存的配置信息,本文主要介绍了Qt读取和写入配置(ini)文件,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • C语言输出教学日历表的方法实例

    C语言输出教学日历表的方法实例

    最近帮朋友做一些C语言的练习题,期间遇到了个比较有意思的题目,下面这篇文章主要给大家介绍了关于C语言输出教学日历表的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Qt之使用GraphicsView框架实现思维导图的示例

    Qt之使用GraphicsView框架实现思维导图的示例

    思维导图可以更方便的整理知识,本文主要介绍了Qt之使用GraphicsView框架实现思维导图的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • C++指针与数组:指针详解

    C++指针与数组:指针详解

    本文从初学者的角度,深入浅出地讲解C++中的指针、数组指针,对最常混淆的引用传递、值传递和指针传递做了区处,需要的朋友可以参考下
    2021-09-09
  • C语言实现学生信息管理系统(链表)

    C语言实现学生信息管理系统(链表)

    这篇文章主要为大家详细介绍了C语言实现学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • c++与c中的数组初始化默认值如何为0

    c++与c中的数组初始化默认值如何为0

    这篇文章主要介绍了c++与c中的数组初始化默认值如何为0问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • C++中关于set删除的一些坑

    C++中关于set删除的一些坑

    这篇文章主要介绍了C++中关于set删除的一些坑,因为这个问题浪费了很多的时间,所以想着分享出来给大家,方便同样遇到这个问题的朋友们,有需要的朋友们下面来一起看看吧。
    2017-02-02
  • c语言网络编程-标准步骤(比较简单)

    c语言网络编程-标准步骤(比较简单)

    这篇文章主要介绍了c语言网络编程-标准步骤(比较简单),需要的朋友可以参考下
    2014-01-01
  • Qt实现部件透明阴影效果与不规则窗体详解

    Qt实现部件透明阴影效果与不规则窗体详解

    这篇文章主要为大家详细介绍了Qt实现部件透明阴影效果与不规则窗体的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-01-01
  • Linux下C语言实现贪吃蛇小游戏

    Linux下C语言实现贪吃蛇小游戏

    这篇文章主要为大家详细介绍了Linux下C语言实现贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03

最新评论