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:
Moshe Katz 2014-02-27 18:34:37 -05:00
parent 7688823886
commit 371ffbc069
2 changed files with 36 additions and 21 deletions

BIN
Cmder.exe

Binary file not shown.

View File

@ -13,7 +13,8 @@
#define USE_TASKBAR_API (_WIN32_WINNT >= _WIN32_WINNT_WIN7) #define USE_TASKBAR_API (_WIN32_WINNT >= _WIN32_WINNT_WIN7)
#define MB_TITLE L"Cmder Launcher" #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) #define streqi(a, b) (_wcsicmp((a), (b)) == 0)
@ -154,30 +155,42 @@ HKEY GetRootKey(std::wstring opt)
return root; return root;
} }
void RegisterShellMenu(std::wstring opt) void RegisterShellMenu(std::wstring opt, wchar_t* keyBaseName)
{ {
HKEY root = GetRootKey(opt); // First, get the paths we will use
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));
wchar_t exePath[MAX_PATH] = { 0 }; wchar_t exePath[MAX_PATH] = { 0 };
wchar_t icoPath[MAX_PATH] = { 0 };
GetModuleFileName(NULL, exePath, sizeof(exePath)); GetModuleFileName(NULL, exePath, sizeof(exePath));
wchar_t commandStr[MAX_PATH + 20] = { 0 }; wchar_t commandStr[MAX_PATH + 20] = { 0 };
swprintf_s(commandStr, L"\"%s\" \"%%V\"", exePath); 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)); FAIL_ON_ERROR(RegSetValue(command, L"", REG_SZ, commandStr, NULL));
RegCloseKey(command); RegCloseKey(command);
@ -185,15 +198,15 @@ void RegisterShellMenu(std::wstring opt)
RegCloseKey(root); RegCloseKey(root);
} }
void UnregisterShellMenu(std::wstring opt) void UnregisterShellMenu(std::wstring opt, wchar_t* keyBaseName)
{ {
HKEY root = GetRootKey(opt); HKEY root = GetRootKey(opt);
HKEY cmderKey; HKEY cmderKey;
FAIL_ON_ERROR( 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)); REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &cmderKey, NULL));
FAIL_ON_ERROR(RegDeleteTree(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(cmderKey);
RegCloseKey(root); RegCloseKey(root);
} }
@ -215,11 +228,13 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
} }
else if (streqi(opt.first.c_str(), L"/REGISTER")) 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")) 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 else
{ {