|
用CRegKey類來(lái)操作注冊(cè)表是非常方便的。CRegKey類并不是一個(gè)MFC類,而是一個(gè)ATL類,所以在使用的時(shí)候不要忘記在StdAfx.h頭文件中加入
#include <atlbase.h>。XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />
1.打開(kāi)需要查詢注冊(cè)表鍵: 原型是:LONG Open( HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired = KEY_ALL_Access ); 只有打開(kāi)了一個(gè)注冊(cè)表鍵才能對(duì)其值進(jìn)行操作。 hKeyParent:打開(kāi)的鍵的句柄。 lpszKeyName:鍵所在的注冊(cè)表的路徑。 samDesired:注冊(cè)表訪問(wèn)的安全性。 例子: CRegKey rk; LPCTSTR lp="Software\\Compupacific\\NewEn\\AddIns\\AddButton";
if(rk.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS) { AfxMessageBox(“Successful!”0); }
2.獲取注冊(cè)表中某一鍵的鍵值: LONG QueryValue( DWORD& dwValue, LPCTSTR lpszValueName ); LONG QueryValue( LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount );
有兩個(gè)函數(shù),第一個(gè)是查詢整型值,第二個(gè)是查詢字符串類型的值。下面分別對(duì)它們進(jìn)行舉例:
//取整型值
CRegKey rk; DWORD dValue ; LPCTSTR lp="Software\\Compupacific\\NewEn\\AddIns\\AddButton"; if(rk.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS) { if(rk.QueryValue( dValue,"LoadBehavior")==ERROR_SUCCESS) { CString temp; temp.Format("%d",dValue); SetDlgItemText(IDC_EDIT1,temp); } else { AfxMessageBox("Query Error"); } } else { AfxMessageBox("Open error!"); } rk.Close();
//取字符串類型的值
CRegKey rk;
HKEY m_hKey; DWORD pCount=1024; CString KeyValue; char szValue[1024]; LPCTSTR lp="Software\\Compupacific\\NewEn\\AddIns\\AddButton"; if(rk.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS) { LPCTSTR lKeyName="Description"; if(rk.QueryValue(szValue,lKeyName,& pCount)== ERROR_SUCCESS) { KeyValue=szValue; SetDlgItemText(IDC_EDIT1,KeyValue); } else { SetDlgItemText(IDC_EDIT1,"Query error"); } //rk.SetValue(lKeyName,"HH"); } else { SetDlgItemText(IDC_EDIT1,"Open error"); } rk.Close();
3.加入一個(gè)鍵值: LONG SetValue( DWORD dwValue, LPCTSTR lpszValueName ); LONG SetValue( LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL ); LONG SetValue( HKEY hKeyParent, LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL ); 有三個(gè)重載函數(shù),大同小異。我們針對(duì)第二個(gè)舉例,舉一反三: LONG lResult = 0; CRegKey reg; //open the required reGIStry key LPCTSTR lpszKey = "Software\\Microsoft\\Internet Explorer\\Toolbar"; lResult = reg.Open(HKEY_CURRENT_USER,lpszKey); //check if opened successfully if(ERROR_SUCCESS != lResult) { return FALSE; } //set the value lResult = reg.SetValue(m_szFilePath,"BackBitmap"); if(ERROR_SUCCESS != lResult) { return FALSE; } //done, close and return success reg.Close(); m_szFilepath是一張圖片的位置,通過(guò)這個(gè)函數(shù),你的IE工具欄的背景就變成的你指定的圖片了,很爽吧。
4. 刪除一個(gè)鍵值: LONG DeleteValue( LPCTSTR lpszValue );
lpszValue:你要?jiǎng)h除的鍵值的名字. 例子:
LONG lResult = 0; CRegKey reg; //open the required registry key LPCTSTR lpszKey = "Software\\Microsoft\\Internet Explorer\\Toolbar"; lResult = reg.Open(HKEY_CURRENT_USER,lpszKey); //check if opened successfully if(ERROR_SUCCESS != lResult) { return FALSE; } //delete the value "BackBitmap" from toolbar lResult = reg.DeleteValue("BackBitmap"); //check if deleted successfully if(ERROR_SUCCESS != lResult) { return FALSE; //perhaps value not found, if skin is not set } //done, return success reg.Close(); 這樣就去掉了你給IE工具欄設(shè)定的背景圖片,也就是刪掉了IE工具欄的BackBitmap鍵值。 一般來(lái)說(shuō)最主要的操作就是這些了,是不是很簡(jiǎn)單啊。
|