C++添加右键菜单

在文件区添加

使用QtCreator编译:

#include <Windows.h>
#include <iostream>
#include <string>
#include <locale>
#include "CommDlg.h"

using namespace std;

bool add(const string &strMenuText, const string &strApplication);
bool del(const string &strMenuText);
string openDlg();
#pragma comment(lib, "comdlg32.lib")

int main() {
    std::setlocale(LC_ALL, "zh_CN.UTF-8");

    while(true) {
        cin.clear();
        cin.sync();
        int m = 0;
       // cout << "(1)添加桌面菜单项 (2)删除桌面菜单项 (3)退出:";//
        cout << "(1) Add desktop menu item (2) Delete desktop menu item (3) Exit:";
        cin >> m;
        if(m == 1) {
            std::string name, path;
            cout << "请输入菜单名:";
            cin.clear();
            cin.sync();
            getline(cin, name);
            cout << "输入程序路径:";
            cin.clear();
            cin.sync();
            //getline(cin, path);
            path = openDlg();
            if(!add(name, path)) {
                cout << "添加失败!请以管理员身份运行" << endl;
            } else {
                cout << "添加完成!右键点击任意文件看看" << endl;
            }
        }
        if(m == 2) {
            std::string name;
            cout << "请输入菜单名:";
            cin.clear();
            cin.sync();
            cin >> name;
            if(!del(name)) {
                cout << "删除失败!请以管理员身份运行" << endl;
            } else {
                cout << "删除成功!" << endl;
            }
        }
        if(m == 3) {
            return 0;
        }
    }
    system("pause");
    return 0;
}
string openDlg() {
    TCHAR szBuffer[MAX_PATH] = {0};
    OPENFILENAME ofn = {0};
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter =
        TEXT("Exe文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0"); //要选择的文件后缀
    ofn.lpstrInitialDir = TEXT("C:\\Program Files"); //默认的文件路径
    ofn.lpstrFile = szBuffer; //存放文件的缓冲区
    ofn.nMaxFile = MAX_PATH;
    ofn.nFilterIndex = 0;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
                OFN_EXPLORER; //标志如果是多选要加上OFN_ALLOWMULTISELECT
    BOOL bSel = GetOpenFileName(&ofn);
    if (bSel) {
        int size_needed = WideCharToMultiByte(CP_UTF8, 0, szBuffer, -1, NULL, 0, NULL,
                                              NULL);
        std::string strTo(size_needed, 0);
        WideCharToMultiByte(CP_UTF8, 0, szBuffer, -1, &strTo[0], size_needed, NULL,
                            NULL);
        return strTo;
    } else {
        return "";
    }
}


bool add(const string &strMenuText, const string &strApplication) {
    // 添加右键菜单项"加密该文件"
    HKEY hresult;
    long value;
    wstring wstrRegKeyName(strMenuText.begin(), strMenuText.end());
    wstring wstrRegKey = L"*\\shell\\" + wstrRegKeyName;
    wstring wstrRegSubkey = L"*\\shell\\" + wstrRegKeyName + L"\\command";
    wstring wstrApplication(strApplication.begin(), strApplication.end());
    wstrApplication += L" %1";
    DWORD dwPos;
    value = RegCreateKeyExW(HKEY_CLASSES_ROOT, wstrRegKey.c_str(), 0, NULL,
                            REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY | KEY_ALL_ACCESS, NULL, &hresult,
                            &dwPos);
    if (0 != value) {
        return false;
    }
    value = RegCreateKeyExW(HKEY_CLASSES_ROOT, wstrRegSubkey.c_str(), 0, NULL,
                            REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY | KEY_ALL_ACCESS, NULL, &hresult,
                            &dwPos);
    if (0 != value) {
        RegCloseKey(hresult);
        return false;
    }
    value = RegSetValueExW(hresult, NULL, 0, REG_SZ,
                           reinterpret_cast<const BYTE *>(wstrApplication.c_str()),
                           (wstrApplication.length() + 1) * sizeof(wchar_t)); // 加上 null 终止符
    if (0 != value) {
        RegCloseKey(hresult);
        return false;
    }
    RegCloseKey(hresult);
    return true;
}




bool del(const string &strMenuText) {
    HKEY hKey;
    wstring wstrRegKey(strMenuText.begin(), strMenuText.end());
    wstring wstrRegSubkey = L"*\\shell";
    // 打开父键
    long lRet = ::RegOpenKeyExW(HKEY_CLASSES_ROOT, wstrRegKey.c_str(), 0, KEY_READ,
                                &hKey);
    if (0 != lRet) {
        return false;
    }
    // 删除“command”子键
    lRet = ::RegDeleteKeyW(hKey, L"command");
    if (0 != lRet) {
        ::RegCloseKey(hKey);
        return false;
    }
    ::RegCloseKey(hKey);
    // 重新打开父键
    lRet = ::RegOpenKeyExW(HKEY_CLASSES_ROOT, wstrRegSubkey.c_str(), 0, KEY_READ,
                           &hKey);
    if (0 != lRet) {
        return false;
    }
    // 删除“加密”子键
    lRet = ::RegDeleteKeyW(hKey, wstrRegKey.c_str());
    if (0 != lRet) {
        ::RegCloseKey(hKey);
        return false;
    }
    ::RegCloseKey(hKey);
    return true;
}

编译选项需要添加:

LIBS += -lcomdlg32

在新建文件区添加

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

参考: 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值