mirror of
				https://github.com/cmderdev/cmder.git
				synced 2025-10-31 09:22:15 +08:00 
			
		
		
		
	Add registration for right-click on folder item; Add context-menu icon
Existing code registered right-click only for blank areas of the Explorer window. This change adds a menu item for right-clicking on a folder in the file list. Also adds the program's icon to help find it in a crowded context menu.
This commit is contained in:
		| @@ -13,7 +13,8 @@ | ||||
| #define USE_TASKBAR_API (_WIN32_WINNT >= _WIN32_WINNT_WIN7) | ||||
|  | ||||
| #define MB_TITLE L"Cmder Launcher" | ||||
| #define SHELL_MENU_REGISTRY_PATH L"Directory\\Background\\shell\\Cmder" | ||||
| #define SHELL_MENU_REGISTRY_PATH_BACKGROUND L"Directory\\Background\\shell\\Cmder" | ||||
| #define SHELL_MENU_REGISTRY_PATH_LISTITEM L"Directory\\shell\\Cmder" | ||||
|  | ||||
| #define streqi(a, b) (_wcsicmp((a), (b)) == 0)  | ||||
|  | ||||
| @@ -154,30 +155,42 @@ HKEY GetRootKey(std::wstring opt) | ||||
| 	return root; | ||||
| } | ||||
|  | ||||
| void RegisterShellMenu(std::wstring opt) | ||||
| void RegisterShellMenu(std::wstring opt, wchar_t* keyBaseName) | ||||
| { | ||||
| 	HKEY root = GetRootKey(opt); | ||||
|  | ||||
| 	HKEY cmderKey; | ||||
| 	FAIL_ON_ERROR( | ||||
| 		RegCreateKeyEx(root, SHELL_MENU_REGISTRY_PATH, 0, NULL, | ||||
| 		REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL)); | ||||
|  | ||||
| 	FAIL_ON_ERROR(RegSetValue(cmderKey, L"", REG_SZ, L"Cmder Here", NULL)); | ||||
| 	FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"NoWorkingDirectory", 0, REG_SZ, (BYTE *)L"", 2)); | ||||
|  | ||||
| 	HKEY command; | ||||
| 	FAIL_ON_ERROR( | ||||
| 		RegCreateKeyEx(cmderKey, L"command", 0, NULL, | ||||
| 		REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &command, NULL)); | ||||
| 	// First, get the paths we will use | ||||
|  | ||||
| 	wchar_t exePath[MAX_PATH] = { 0 }; | ||||
| 	wchar_t icoPath[MAX_PATH] = { 0 }; | ||||
|  | ||||
| 	GetModuleFileName(NULL, exePath, sizeof(exePath)); | ||||
|  | ||||
| 	wchar_t commandStr[MAX_PATH + 20] = { 0 }; | ||||
| 	swprintf_s(commandStr, L"\"%s\" \"%%V\"", exePath); | ||||
|  | ||||
| 	// Now that we have `commandStr`, it's OK to change `exePath`... | ||||
| 	PathRemoveFileSpec(exePath); | ||||
|  | ||||
| 	PathCombine(icoPath, exePath, L"icons\\cmder.ico"); | ||||
|  | ||||
| 	// Now set the registry keys | ||||
|  | ||||
| 	HKEY root = GetRootKey(opt); | ||||
|  | ||||
| 	HKEY cmderKey; | ||||
| 	FAIL_ON_ERROR( | ||||
| 		RegCreateKeyEx(root, keyBaseName, 0, NULL, | ||||
| 		REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL)); | ||||
|  | ||||
| 	FAIL_ON_ERROR(RegSetValue(cmderKey, L"", REG_SZ, L"Cmder Here", NULL)); | ||||
| 	FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"NoWorkingDirectory", 0, REG_SZ, (BYTE *)L"", 2)); | ||||
|  | ||||
| 	FAIL_ON_ERROR(RegSetValueEx(cmderKey, L"Icon", 0, REG_SZ, (BYTE *)icoPath, sizeof icoPath)); | ||||
|  | ||||
| 	HKEY command; | ||||
| 	FAIL_ON_ERROR( | ||||
| 		RegCreateKeyEx(cmderKey, L"command", 0, NULL, | ||||
| 		REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &command, NULL)); | ||||
|  | ||||
| 	FAIL_ON_ERROR(RegSetValue(command, L"", REG_SZ, commandStr, NULL)); | ||||
|  | ||||
| 	RegCloseKey(command); | ||||
| @@ -185,15 +198,15 @@ void RegisterShellMenu(std::wstring opt) | ||||
| 	RegCloseKey(root); | ||||
| } | ||||
|  | ||||
| void UnregisterShellMenu(std::wstring opt) | ||||
| void UnregisterShellMenu(std::wstring opt, wchar_t* keyBaseName) | ||||
| { | ||||
| 	HKEY root = GetRootKey(opt); | ||||
| 	HKEY cmderKey; | ||||
| 	FAIL_ON_ERROR( | ||||
| 		RegCreateKeyEx(root, SHELL_MENU_REGISTRY_PATH, 0, NULL, | ||||
| 		RegCreateKeyEx(root, keyBaseName, 0, NULL, | ||||
| 		REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL)); | ||||
| 	FAIL_ON_ERROR(RegDeleteTree(cmderKey, NULL)); | ||||
| 	FAIL_ON_ERROR(RegDeleteKey(root, SHELL_MENU_REGISTRY_PATH)); | ||||
| 	FAIL_ON_ERROR(RegDeleteKey(root, keyBaseName)); | ||||
| 	RegCloseKey(cmderKey); | ||||
| 	RegCloseKey(root); | ||||
| } | ||||
| @@ -215,11 +228,13 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, | ||||
| 	} | ||||
| 	else if (streqi(opt.first.c_str(), L"/REGISTER")) | ||||
| 	{ | ||||
| 		RegisterShellMenu(opt.second); | ||||
| 		RegisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_BACKGROUND); | ||||
| 		RegisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_LISTITEM); | ||||
| 	} | ||||
| 	else if (streqi(opt.first.c_str(), L"/UNREGISTER")) | ||||
| 	{ | ||||
| 		UnregisterShellMenu(opt.second); | ||||
| 		UnregisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_BACKGROUND); | ||||
| 		UnregisterShellMenu(opt.second, SHELL_MENU_REGISTRY_PATH_LISTITEM); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user