参考:http://topic.csdn.net/u/20100627/10/d39caf34-cbcd-41c0-ab60-4ad140250df1.html
原先的代码需要进行修改才能使用,直接使用会报错(什么东西都不能不劳而获吗,呵呵~~),主要是获取注册表编辑器进程窗体句柄、左侧属性控件句柄、还有列表控件句柄,因为在代码中需要操作这些窗体句柄实现注册表键值的查找及定位,所以这些代码必须能真正获取到,否则将导致查找定位失败,改写代码中也就是对上面的三处进行了修改,并修改了一个分割字符串的函数,感觉原先的不好用,就自己写了个(可能原先的我自己没用好,见谅),下面是代码:
class CJumpToRegistry
{
public:
static int SplitString(CString str, CStringArray& strArr,LPTSTR split)
{
CString strLeft;
int nCurPos = 0;
strArr.RemoveAll();//清空结果
if (str.IsEmpty() || str.GetLength() <= 0)
{
return 0;
}
strLeft = str.Tokenize(split,nCurPos);
while (strLeft != _T(""))
{
strArr.Add(strLeft);
strLeft = str.Tokenize(split,nCurPos);
}
return strArr.GetCount();
}
static BOOL JumpToRegistry(LPCTSTR lpszPath, LPCTSTR lpszValue)
{
CWaitCursor wc;
CString strKeyPath(lpszPath);
CString strValName(lpszValue);
CString strCmd=_T( "regedit.exe ");
TRACE1("%s",lpszPath);
HWND hTop=NULL;
hTop = FindWindow(_T("RegEdit_RegEdit"), NULL );
if ( hTop == NULL )
{
SHELLEXECUTEINFO info;
memset( &info, 0, sizeof info );
info.cbSize = sizeof info;
info.fMask = SEE_MASK_NOCLOSEPROCESS;
info.lpVerb = _T("open");
info.lpFile = _T("regedit.exe");
info.nShow = SW_SHOWNORMAL;
ShellExecuteEx( &info );
WaitForInputIdle( info.hProcess, INFINITE );
hTop = FindWindow(_T("RegEdit_RegEdit"), NULL );
}
if (hTop==NULL)
return FALSE;
//前端显示
::SetWindowPos(hTop,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
//得到TREEVIEW
HWND hTree=NULL;
{
hTree = FindWindowEx( hTop, NULL, _T("SysTreeView32"), NULL );
}
if (hTree==NULL)
return FALSE;
//得到listview
HWND hList=NULL;
{
hList = FindWindowEx( hTop, NULL, _T("SysListView32"), NULL );
}
if (hList==NULL)
return FALSE;
//选中ROOT结点
::SendMessage(hTree,WM_SETFOCUS,0,0);
::SendMessage(hTree,WM_KEYDOWN,VK_HOME,0);
//展开TREE
CStringArray vKeys;
int nCount = SplitString(strKeyPath,vKeys,_T("\\"));
for (int i=0; i <nCount; ++i)
{
::SendMessage(hTree,WM_KEYDOWN,VK_RIGHT,0);
CString sSelectKey=vKeys[i];
sSelectKey.MakeUpper();
for (int iLetter=0; iLetter <sSelectKey.GetLength(); ++iLetter)
{
::SendMessage(hTree,WM_CHAR,sSelectKey.GetAt(iLetter),0);
}
}
//LISTVIEW定位
::SendMessage(hList,WM_SETFOCUS,0,0);
//选中value
Sleep(300);
::SendMessage(hList,WM_KEYDOWN,VK_HOME,0);
CString sSelectVal=strValName;
sSelectVal.MakeUpper();
for (int iLetter=0; iLetter <sSelectVal.GetLength(); ++iLetter)
{
::SendMessage(hList,WM_CHAR,sSelectVal.GetAt(iLetter),0);
}
return TRUE;
}
};
使用方式和原先的一样:
用的时候,先包含上面的文件,调用例子:
CJumpToRegistry::JumpToRegistry(_T( "HKEY_LOCAL_MACHINE\\SOFTWARE "), "testsoft ");
CJumpToRegistry::JumpToRegistry(_T( "HKEY_LOCAL_MACHINE\\SOFTWARE "),NULL);