C++ implementation to add desktop right click new menu

  • 2020-05-07 20:12:41
  • OfStack

It is not uncommon for programmers to create a new cpp file.

For convenience, we're used to right-clicking on a new file on the desktop instead of creating a new text document and then changing the suffix.

Baidu Google query 1, finally know how to add the registry.

Itchy hands, took time to write a procedure with cpp, convenient for later operation.

Customer demand is never able to meet, after the students test, wrote three versions in succession.

Next directly paste code ~

The first version can only add c, cpp and java suffixes.


/*
 * 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;
}

Version 2, streamlined code, support for adding user input suffixes.


 /*
 * 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;
 }

Version 3, which supports multiple additions and allows for the deletion of added registries.


/*
 * 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;
}

Package download address:

http://xiazai.ofstack.com/201601/yuanma/Regedity(ofstack.com).zip

Note that if an dll file is missing, please download it and copy it to C:\Windows\System32.


Related articles: