Created
June 29, 2017 04:02
-
-
Save mkdym/c67cabab9bdedbfcfddcd14a7ac07ab8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Jump2Regedit(HKEY hRoot, const CString& strKey, const CString& strValue = TEXT(""), const DWORD dwSleepTime = 100) | |
{ | |
do | |
{ | |
if (NULL == hRoot || strKey.IsEmpty()) | |
{ | |
break; | |
} | |
//连接字符串 | |
CString strRegPath(TEXT("\\")); | |
switch((ULONG_PTR)((LONG)hRoot)) | |
{ | |
case HKEY_LOCAL_MACHINE: | |
strRegPath += TEXT("HKEY_LOCAL_MACHINE\\") + strKey; | |
break; | |
case HKEY_CURRENT_USER: | |
strRegPath += TEXT("HKEY_CURRENT_USER\\") + strKey; | |
break; | |
case HKEY_CURRENT_CONFIG: | |
strRegPath += TEXT("HKEY_CURRENT_CONFIG\\") + strKey; | |
break; | |
case HKEY_CLASSES_ROOT: | |
strRegPath += TEXT("HKEY_CLASSES_ROOT\\") + strKey; | |
break; | |
case HKEY_USERS: | |
strRegPath += TEXT("HKEY_USERS\\") + strKey; | |
break; | |
default: | |
break; | |
} | |
//打开注册表编辑器并查找其窗口 | |
HWND hRegeditMainWnd = FindWindow(TEXT("RegEdit_RegEdit"), NULL); | |
if (NULL == hRegeditMainWnd) | |
{ | |
SHELLEXECUTEINFO info; | |
ZeroMemory(&info, sizeof(SHELLEXECUTEINFO)); | |
info.cbSize = sizeof(SHELLEXECUTEINFO); | |
info.fMask = SEE_MASK_NOCLOSEPROCESS; | |
info.lpVerb = TEXT("open"); | |
info.lpFile = TEXT("regedit.exe"); | |
info.nShow = SW_SHOWNORMAL; | |
if (ShellExecuteEx(&info)) | |
{ | |
WaitForInputIdle(info.hProcess, INFINITE); | |
hRegeditMainWnd = FindWindow(TEXT("RegEdit_RegEdit"), NULL); | |
CloseHandle(info.hProcess); | |
} | |
} | |
if (NULL == hRegeditMainWnd) | |
{ | |
break; | |
} | |
//调到前台显示 | |
ShowWindow(hRegeditMainWnd, SW_SHOW); | |
SetForegroundWindow(hRegeditMainWnd); | |
//获取左边树状控件,并设置焦点 | |
HWND hRegeditKeyWnd = FindWindowEx(hRegeditMainWnd, NULL, TEXT("SysTreeView32"), NULL); | |
SetForegroundWindow(hRegeditKeyWnd); | |
SetFocus(hRegeditKeyWnd); | |
//回到根节点:假设最多30层 | |
for (int iBackTime = 0; iBackTime != 30; ++iBackTime) | |
{ | |
SendMessage(hRegeditKeyWnd, WM_KEYDOWN, VK_LEFT, 0); | |
Sleep(5); | |
SendMessage(hRegeditKeyWnd, WM_KEYUP, VK_LEFT, 0); | |
} | |
//等一会 | |
DEVMODE devMode; | |
devMode.dmSize = sizeof(DEVMODE); | |
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode); | |
if (devMode.dmBitsPerPel > 8) | |
{ | |
Sleep(dwSleepTime); | |
} | |
//打开路径 | |
for (TCHAR *ch = strRegPath.GetBuffer(strRegPath.GetLength() + 1); *ch; ++ch) | |
{ | |
if (TEXT('\\') == *ch)//下一层路径 | |
{ | |
SendMessage(hRegeditKeyWnd, WM_KEYDOWN, VK_RIGHT, 0); | |
if (devMode.dmBitsPerPel > 8) | |
{ | |
Sleep(dwSleepTime); | |
} | |
Sleep(5); | |
SendMessage(hRegeditKeyWnd, WM_KEYUP, VK_RIGHT, 0); | |
} | |
else | |
{ | |
SendMessage(hRegeditKeyWnd, WM_CHAR, _totupper(*ch), 0); | |
} | |
} | |
strRegPath.ReleaseBuffer(); | |
SetForegroundWindow(hRegeditKeyWnd); | |
SetFocus(hRegeditKeyWnd); | |
//选中值 | |
if (!strValue.IsEmpty()) | |
{ | |
HWND hRegeditValueWnd = FindWindowEx(hRegeditMainWnd, NULL, TEXT("SysListView32"), NULL); | |
SetForegroundWindow(hRegeditValueWnd); | |
SetFocus(hRegeditValueWnd); | |
Sleep(dwSleepTime); | |
SendMessage(hRegeditValueWnd, WM_KEYDOWN, VK_HOME, 0); | |
Sleep(5); | |
SendMessage(hRegeditValueWnd, WM_KEYUP, VK_HOME, 0); | |
CString strValueTemp = strValue; | |
for (TCHAR *ch = strValueTemp.GetBuffer(strValueTemp.GetLength() + 1); *ch; ++ch) | |
{ | |
SendMessage(hRegeditValueWnd, WM_CHAR, _totupper(*ch), 0); | |
} | |
strValueTemp.ReleaseBuffer(); | |
SetForegroundWindow(hRegeditValueWnd); | |
SetFocus(hRegeditValueWnd); | |
} | |
} while (FALSE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment