服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现添加桌面右键新建菜单

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

2021-03-19 13:2999re C/C++

本文给大家汇总了3个版本的C++实现添加桌面右键新建菜单的代码,陆陆续续写的,有需要的小伙伴可以根据自己的需求来选择

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

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

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

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

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

接下来直接贴代码~

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * 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;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* 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;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * 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;
}

打包下载地址:

Regedity.zip

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

延伸 · 阅读

精彩推荐
  • C/C++C语言中炫酷的文件操作实例详解

    C语言中炫酷的文件操作实例详解

    内存中的数据都是暂时的,当程序结束时,它们都将丢失,为了永久性的保存大量的数据,C语言提供了对文件的操作,这篇文章主要给大家介绍了关于C语言中文件...

    针眼_6702022-01-24
  • C/C++C++之重载 重定义与重写用法详解

    C++之重载 重定义与重写用法详解

    这篇文章主要介绍了C++之重载 重定义与重写用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    青山的青6062022-01-04
  • C/C++学习C++编程的必备软件

    学习C++编程的必备软件

    本文给大家分享的是作者在学习使用C++进行编程的时候所用到的一些常用的软件,这里推荐给大家...

    谢恩铭10102021-05-08
  • C/C++详解c语言中的 strcpy和strncpy字符串函数使用

    详解c语言中的 strcpy和strncpy字符串函数使用

    strcpy 和strcnpy函数是字符串复制函数。接下来通过本文给大家介绍c语言中的strcpy和strncpy字符串函数使用,感兴趣的朋友跟随小编要求看看吧...

    spring-go5642021-07-02
  • C/C++c++ 单线程实现同时监听多个端口

    c++ 单线程实现同时监听多个端口

    这篇文章主要介绍了c++ 单线程实现同时监听多个端口的方法,帮助大家更好的理解和学习使用c++,感兴趣的朋友可以了解下...

    源之缘11542021-10-27
  • C/C++深入理解goto语句的替代实现方式分析

    深入理解goto语句的替代实现方式分析

    本篇文章是对goto语句的替代实现方式进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网7342020-12-03
  • C/C++C语言实现电脑关机程序

    C语言实现电脑关机程序

    这篇文章主要为大家详细介绍了C语言实现电脑关机程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xiaocaidayong8482021-08-20
  • C/C++C/C++经典实例之模拟计算器示例代码

    C/C++经典实例之模拟计算器示例代码

    最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示...

    jia150610152021-06-07