#include <Windows.h> #include <iostream> #include <string> using namespace std; void add(string strMenuText, string strApplication); void del(string strMenuText); int main(){ system("pause"); return 0; } void add(string strMenuText, string strApplication){ //添加右键菜单项"加密该文件" HKEY hresult; long value; //string strRegKeyName = "我的加密"; string strRegKeyName = strMenuText; string strRegKey = "*\\shell\\" + strRegKeyName;//if txt file, please use "txtfile\\shell\\用我的文本编辑程序打开" string strRegSubkey="*\\shell\\" + strRegKeyName + "\\command"; //string strMenuText="加密该文件"; //string strApplication="E:\\Desktop\\Use\\xsearch_CHS.exe %1"; strApplication = strApplication + " %1"; DWORD dwPos; //创建“加密”子键,并设置右键菜单项的名称 value=RegCreateKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegKey.c_str(),0,NULL,REG_OPTION_NON_VOLATILE,KEY_CREATE_SUB_KEY|KEY_ALL_ACCESS,NULL,&hresult,&dwPos); value=RegSetValueEx(hresult,NULL,0,REG_SZ,(BYTE*)(LPCTSTR)strMenuText.c_str(), strMenuText.length()); //创建“command”子键,并设置关联的应用程序 value=RegCreateKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegSubkey.c_str(),0,NULL,REG_OPTION_NON_VOLATILE,KEY_CREATE_SUB_KEY|KEY_ALL_ACCESS,NULL,&hresult,&dwPos); value=RegSetValueEx(hresult,NULL,0,REG_SZ,(const BYTE*)(LPCTSTR)strApplication.c_str(),strApplication.length()); //关闭注册表 ::RegCloseKey(hresult); } void del(string strMenuText){ HKEY hKey; //string strRegKeyName = "我的加密"; string strRegKeyName = strMenuText; string strRegKey="*\\shell\\" + strRegKeyName; string strRegSubkey="*\\shell"; //打开父键 long lRet=::RegOpenKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegKey.c_str(),0,KEY_READ,&hKey); //删除“command”子键 ::RegDeleteKey(hKey,LPCTSTR("command")); ::RegCloseKey(hKey); lRet=::RegOpenKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegSubkey.c_str(),0,KEY_READ,&hKey); //删除“加密”子键 ::RegDeleteKey(hKey,LPCTSTR(strRegKeyName.c_str())); ::RegCloseKey(hKey); }
下面是一个比较完整的例子:
#include <Windows.h> #include <iostream> #include <string> #include "CommDlg.h" using namespace std; bool add(string strMenuText, string strApplication); bool del(string strMenuText); string openDlg(); int main(){ while(true){ cin.clear(); cin.sync(); int m = 0; cout << "(1)添加桌面菜单项 (2)删除桌面菜单项 (3)退出:"; 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 = ("Exe文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0");//要选择的文件后缀 ofn.lpstrInitialDir = ("C:\\Program Files");//默认的文件路径 ofn.lpstrFile = szBuffer;//存放文件的缓冲区 ofn.nMaxFile = sizeof(szBuffer)/sizeof(*szBuffer); ofn.nFilterIndex = 0; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER ;//标志如果是多选要加上OFN_ALLOWMULTISELECT BOOL bSel = GetOpenFileName(&ofn); return szBuffer; } bool add(string strMenuText, string strApplication){ //添加右键菜单项"加密该文件" HKEY hresult; long value; //string strRegKeyName = "我的加密"; string strRegKeyName = strMenuText; string strRegKey = "*\\shell\\" + strRegKeyName;//if txt file, please use "txtfile\\shell\\用我的文本编辑程序打开" string strRegSubkey="*\\shell\\" + strRegKeyName + "\\command"; //string strMenuText="加密该文件"; //string strApplication="E:\\Desktop\\Use\\xsearch_CHS.exe %1"; strApplication = strApplication + " %1"; DWORD dwPos; //创建“加密”子键,并设置右键菜单项的名称 value=RegCreateKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegKey.c_str(),0,NULL,REG_OPTION_NON_VOLATILE,KEY_CREATE_SUB_KEY|KEY_ALL_ACCESS,NULL,&hresult,&dwPos); if(0 != value){ return false; } value=RegSetValueEx(hresult,NULL,0,REG_SZ,(BYTE*)(LPCTSTR)strMenuText.c_str(), strMenuText.length()); if(0 != value){ return false; } //创建“command”子键,并设置关联的应用程序 value=RegCreateKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegSubkey.c_str(),0,NULL,REG_OPTION_NON_VOLATILE,KEY_CREATE_SUB_KEY|KEY_ALL_ACCESS,NULL,&hresult,&dwPos); if(0 != value){ return false; } value=RegSetValueEx(hresult,NULL,0,REG_SZ,(const BYTE*)(LPCTSTR)strApplication.c_str(),strApplication.length()); if(0 != value){ return false; } //关闭注册表 ::RegCloseKey(hresult); } bool del(string strMenuText){ HKEY hKey; //string strRegKeyName = "我的加密"; string strRegKeyName = strMenuText; string strRegKey="*\\shell\\" + strRegKeyName; string strRegSubkey="*\\shell"; //打开父键 long lRet=::RegOpenKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegKey.c_str(),0,KEY_READ,&hKey); if(0 != lRet){ return false; } //删除“command”子键 lRet = ::RegDeleteKey(hKey,LPCTSTR("command")); if(0 != lRet){ return false; } ::RegCloseKey(hKey); lRet=::RegOpenKeyEx(HKEY_CLASSES_ROOT,(LPSTR)(LPCSTR)strRegSubkey.c_str(),0,KEY_READ,&hKey); if(0 != lRet){ return false; } //删除“加密”子键 lRet = ::RegDeleteKey(hKey,LPCTSTR(strRegKeyName.c_str())); if(0 != lRet){ return false; } ::RegCloseKey(hKey); }