C++ 像我的电脑中 百度网盘的 那图标快捷方式。如何生成的呢?设置程序图标到我的电脑 请看下边代码 就ok了(*^__^*) 嘻嘻……
类似下图:
大家如果看我下边的不是很清楚,可以下载这个具体工程;
这里是我编译的和一个测试Demo:http://pan.baidu.com/s/1dDBYJux
QQ群:103197177 C++进阶讨论;欢迎喜欢编程朋友加入进来一起讨论学习;
注意:在WIN7及以上的系统中,请以官员权限运行代码;
函数说明:
SetProgramToNameSpace:设置程序图标到我的电脑
DelSubKey: 取消设置,删除程序图标
#include <windows.h> #include <iostream> #include <tchar.h>#ifndef _TRACE #define _TRACE(__VA_ARGS__) OutputDebugString(__VA_ARGS__) #endif#define STRNAMESPACE _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MyComputer\\NameSpace\\") #define STRCLSID _T("SOFTWARE\\Classes\\CLSID\\")// 设置图标到我的电脑
// lpszProPath省略 默认给本程序添加图标到我的电脑,否则给指定程序添加图标到我的电脑
BOOL SetProgramToNameSpace(LPCTSTR lpszKeyName, LPCTSTR lpszGUID, LPCTSTR lpszProPath = NULL);// 创建注册表项 BOOL CreateSubkey(HKEY hKey, LPCTSTR lpszSubkey, HKEY& hResult);// 写入注册表键值 BOOL WritRegValue(HKEY hKey, LPCTSTR lpszValue, DWORD dwType = REG_SZ);// 删除注册表项 BOOL DelRegKey(LPCTSTR lpszSubkey);// 删除写入的注册表项 void DelSubKey(LPCTSTR lpszGUID);int main(void) {if (SetProgramToNameSpace(_T("测试"), _T("{FE533112-C7ED-4EBB-8177-F32CC37AB8C7}"))){std::cout << "在我的电脑 中创建图标成功!" << std::endl;}else{std::cout << "在我的电脑 中创建图标失败!" << std::endl;std::cout << "请以右键管理员权限运行!" << std::endl;}// 删除键值项(用于清理我的电脑里边的图标)//DelSubKey(_T("{FE533112-C7ED-4EBB-8177-F32CC37AB8C7}"));system("pause");return 0; }BOOL CreateSubkey(HKEY hKey, LPCTSTR lpszSubkey, HKEY& hResult) {DWORD dwDisposition = REG_CREATED_NEW_KEY;if (ERROR_SUCCESS != RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpszSubkey, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS | KEY_WOW64_64KEY, NULL, &hResult, &dwDisposition)){_TRACE(_T("NameSpace:写注册表%s项失败!\n"), lpszSubkey);return FALSE;}return TRUE; }BOOL WritRegValue(HKEY hKey, LPCTSTR lpszValue, DWORD dwType) {if (ERROR_SUCCESS != RegSetValueEx(hKey, NULL, 0, dwType, (BYTE*)lpszValue, sizeof(TCHAR)*(_tcsclen(lpszValue) + 2*sizeof(TCHAR)))){_TRACE(_T("NameSpace:写注册表键值%s失败!\n"), lpszKeyName);return FALSE;}return TRUE; }BOOL SetProgramToNameSpace(LPCTSTR lpszKeyName, LPCTSTR lpszGUID, LPCTSTR lpszProPath) {BOOL bSuccess = FALSE;HKEY hSubKey = NULL;HKEY hSubKeyCLSID = NULL;HKEY hSubKeyDef = NULL;HKEY hSubKeyShell = NULL;HKEY hSubKeyOpen = NULL;HKEY hSubKeyCommand = NULL;try{// NameSpaceTCHAR szSubKey[MAX_PATH] = STRNAMESPACE;_stprintf_s(szSubKey, _T("%s%s"), STRNAMESPACE,lpszGUID);if (!CreateSubkey(HKEY_LOCAL_MACHINE, szSubKey, hSubKey)){throw(_T("NameSpace:创建注册表%s项失败!\n"), lpszGUID);}if (!WritRegValue(hSubKey, lpszKeyName)){throw(_T("NameSpace:写注册表键值%s失败!\n"), lpszKeyName);}// CLSID_stprintf_s(szSubKey, _T("%s%s"), STRCLSID,lpszGUID);if (!CreateSubkey(HKEY_LOCAL_MACHINE, szSubKey, hSubKeyCLSID)){throw(_T("CLSID:创建注册表%s项失败!\n"), lpszGUID); }if (!WritRegValue(hSubKeyCLSID, lpszKeyName)){throw(_T("CLSID:写注册表%s项失败!\n"), lpszGUID);}_stprintf_s(szSubKey, _T("%s%s\\DefaultIcon"), STRCLSID,lpszGUID);if (!CreateSubkey(HKEY_LOCAL_MACHINE, szSubKey, hSubKeyDef)){throw(_T("CLSID:创建注册表%s项失败!\n"), lpszGUID);}// DefaultIconTCHAR szExePath[MAX_PATH] = { 0 };if (!lpszProPath){GetModuleFileName(NULL, szExePath, MAX_PATH);if (!WritRegValue(hSubKeyDef, szExePath)){throw(_T("CLSID-DefaultIcon:写注册表%s项失败!\n"), szExePath);}}else if (!WritRegValue(hSubKeyDef, lpszProPath)){throw(_T("CLSID-DefaultIcon:写注册表%s项失败!\n"), lpszProPath);}// shell_stprintf_s(szSubKey, _T("%s%s\\shell"), STRCLSID,lpszGUID);if (!CreateSubkey(HKEY_LOCAL_MACHINE, szSubKey, hSubKeyShell)){throw(_T("CLSID-shell:创建注册表%s项失败!\n"), _T("shell"));}// Open_stprintf_s(szSubKey, _T("%s%s\\shell\\Open"), STRCLSID,lpszGUID);if (!CreateSubkey(HKEY_LOCAL_MACHINE, szSubKey, hSubKeyOpen)){throw(_T("CLSID-Open:创建注册表%s项失败!\n"), _T("Open"));}// Command_stprintf_s(szSubKey, _T("%s%s\\shell\\Open\\Command"), STRCLSID,lpszGUID);if (!CreateSubkey(HKEY_LOCAL_MACHINE, szSubKey, hSubKeyCommand)){throw(_T("CLSID-Command:创建注册表%s项失败!\n"), _T("Command"));}if (!lpszProPath){if (!WritRegValue(hSubKeyCommand, szExePath)){throw(_T("CLSID-DefaultIcon:写注册表%s项失败!\n"), szExePath);}}else if (!WritRegValue(hSubKeyCommand, lpszProPath)){throw(_T("CLSID-DefaultIcon:写注册表%s项失败!\n"), lpszProPath);}bSuccess = TRUE;}catch (TCHAR* pError){_TRACE(pError);}// 关闭注册表if (hSubKey)RegCloseKey(hSubKey);if (hSubKeyCLSID)RegCloseKey(hSubKeyCLSID);if (hSubKeyDef)RegCloseKey(hSubKeyDef);if (hSubKeyShell)RegCloseKey(hSubKeyShell);if (hSubKeyOpen)RegCloseKey(hSubKeyOpen);if (hSubKeyCommand)RegCloseKey(hSubKeyCommand);return bSuccess; }BOOL DelRegKey(LPCTSTR lpszSubkey) {HKEY hResult = NULL;if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubkey, 0, KEY_ALL_ACCESS|KEY_WOW64_64KEY, &hResult)){if (ERROR_SUCCESS != RegDeleteKey(hResult, _T(""))){_TRACE(_T("删除%s项失败\n"), lpszSubkey);}RegCloseKey(hResult);return TRUE;}return FALSE; }void DelSubKey(LPCTSTR lpszGUID) {TCHAR szSubKey[MAX_PATH] = {0};_stprintf_s(szSubKey, _T("%s%s"), STRNAMESPACE,lpszGUID);DelRegKey(szSubKey);_stprintf_s(szSubKey, _T("%s%s\\DefaultIcon"), STRCLSID, lpszGUID);DelRegKey(szSubKey);_stprintf_s(szSubKey, _T("%s%s\\shell\\Open\\Command"), STRCLSID, lpszGUID);DelRegKey(szSubKey);_stprintf_s(szSubKey, _T("%s%s\\shell\\Open"), STRCLSID, lpszGUID);DelRegKey(szSubKey);_stprintf_s(szSubKey, _T("%s%s\\shell"), STRCLSID, lpszGUID);DelRegKey(szSubKey); }