www.gusucode.com > VC++操作注册表写入程序注册信息-源码程序 > VC++操作注册表写入程序注册信息-源码程序\code\PersistDlg.cpp
//Download by http://www.NewXing.com /* This file was written by Amir Israeli , July 2000 Email: israelaq@walla.co.il No warranty of any kind . Dont remove this header . Thanks. */ // PersistDlg.cpp : implementation file // #include "stdafx.h" #include "serialize.h" #include "PersistDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPersistDlg dialog CPersistDlg::CPersistDlg(CWnd* pParent /*=NULL*/) : CDialog(CPersistDlg::IDD, pParent) { //{{AFX_DATA_INIT(CPersistDlg) m_strDefault = _T(""); m_iAge = 0; m_strName = _T(""); m_strPoint = _T(""); m_strRect = _T(""); //}}AFX_DATA_INIT } void CPersistDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPersistDlg) DDX_Text(pDX, IDC_DEFAULT, m_strDefault); DDX_Text(pDX, IDC_AGE, m_iAge); DDV_MinMaxInt(pDX, m_iAge, 0, 150); DDX_Text(pDX, IDC_NAME, m_strName); DDX_Text(pDX, IDC_POINT, m_strPoint); DDX_Text(pDX, IDC_RECT, m_strRect); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPersistDlg, CDialog) //{{AFX_MSG_MAP(CPersistDlg) ON_BN_CLICKED(IDC_INSTALL, OnInstall) ON_BN_CLICKED(IDC_UNINSTALL, OnUninstall) ON_BN_CLICKED(IDC_LOAD, OnLoad) ON_BN_CLICKED(IDC_SAVE, OnSave) ON_BN_CLICKED(IDC_SAVEMAP, OnSavemap) ON_BN_CLICKED(IDC_LOADMAP, OnLoadmap) ON_BN_CLICKED(IDC_INFO, OnInfo) ON_BN_CLICKED(IDC_LOADRES, OnLoadres) //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CPersistDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here /* This is how we build a map in prorammatic way , instead we (1) can load a resource (LoadFromResource) in one line : and by that eliminate the need to write these line (great when we have a big map) (2) another method is using an external file (not recommented: what happens if someone (for heaven sake) deletes it. */ persistObj.InsertPath(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence"),FALSE); persistObj.InsertValue( NULL, _T(""), _T("CString") );//default value persistObj.InsertValue( NULL, _T("Name"), _T("CString") ); persistObj.InsertValue( NULL, _T("Age"), _T("int"));//default value persistObj.InsertPath(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence\\Some SubKey"),FALSE); persistObj.InsertValue( NULL, _T("rect"), _T("CRect") ); persistObj.InsertValue( NULL, _T("point"), _T("CPoint") ); persistObj.InsertPath(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence\\Some SubKey\\Subkey2Delete"),TRUE); persistObj.InsertPath(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence\\Some SubKey\\Another SubKey"),TRUE); ////////////////////////////////// /// setting data tips if (tip.Create(this)) { tip.AddTool( GetDlgItem(IDC_INSTALL) , _T("Installs a map of keys \nand values in the registry ,\ninstalled values of any type \nwill have default data (Zero's) ,\n so you'll have to take another\n step to initialize it.")); tip.AddTool( GetDlgItem(IDC_UNINSTALL) , _T("UnInstalls a map from the \nregistry , If you want to leave a\n key or value (still there after \nuninstalling) you can do it \n(a BOOL flag)")); tip.AddTool( GetDlgItem(IDC_LOAD) , _T("load data of values from their \nplace in registry and show it in \nedit boxes , if edit ctrl doesn't \nsupport a ddx with the type \nof value : as the case of rect/point data\n use some conversion function")); tip.AddTool( GetDlgItem(IDC_SAVE) , _T("save the data in edit boxes\ninto proper place in registry\n (use conversion if direct\n ddx not supported)")); tip.AddTool( GetDlgItem(IDC_SAVEMAP) , _T("save the map to \nan external file")); tip.AddTool( GetDlgItem(IDC_LOADMAP) , _T("load map from external file")); tip.AddTool( GetDlgItem(IDC_INFO) , _T("get information about your map\n (defined only in debug build)")); tip.AddTool( GetDlgItem(IDC_LOADRES) , _T("load a map from a resource")); } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } ///////////////////////////////////////////////////////////////////////////// // CPersistDlg message handlers void CPersistDlg::OnInstall() { persistObj.Install();//TRUE } void CPersistDlg::OnUninstall() { persistObj.UnInstall();//Install(FALSE); } ///// These 2 functions : load + Save will use the same handler with // minor modifications to help with transformation of types void CPersistDlg::OnLoad() { // call Persist PersistObject(TRUE); UpdateData(FALSE);//update controls } void CPersistDlg::OnSave() { UpdateData(TRUE); //update variables //call persist PersistObject(FALSE); } void CPersistDlg::PersistObject(BOOL bLoad) //TRUE= loading { // coping the whole section from WM_INITDIALOG function handler // and modifying the Insert... functions (used in Installation) to // Path/Value //each path marks the begining on a new search path for values //the value names that come after each path are under that path persistObj.Path( _T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence")); persistObj.Value( _T("") , &m_strDefault ,bLoad);//default value persistObj.Value( _T("Name"), &m_strName ,bLoad); persistObj.Value( _T("Age"), &m_iAge ,bLoad);//default value //setting a new path persistObj.Path(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence\\Some SubKey")); //such values are a bit problematic (need to transform from CString 2 that type and oposite) //(problem : there's no ddx-action between the member variable attached to edit-ctrl // to the variable that the data entered inside is going to be (MFC has built in ddx //conversions from edit-ctrls to int's strings DWORD's time_t ... //(but not for rects or points which are demonstrated CRect tmpRect; CPoint tmpPoint; if (bLoad) { //in load persistObj.Value( _T("rect"), &tmpRect ,bLoad); persistObj.Value( _T("point"), &tmpPoint ,bLoad); m_strPoint.Format("%d %d",tmpPoint.x,tmpPoint.y); m_strRect.Format("%d %d %d %d",tmpRect.left,tmpRect.top,tmpRect.right,tmpRect.bottom); } else { //in save (look into the function) //converting string to rect and point persistObj.FormatElements( m_strRect, _T("%d%d%d%d"),&tmpRect.left,&tmpRect.top,&(tmpRect.right),&(tmpRect.bottom)); persistObj.FormatElements( m_strPoint, _T("%d%d"),&tmpPoint.x,&tmpPoint.y); //saving those converted rect and point persistObj.Value( _T("rect"), &tmpRect ,bLoad); persistObj.Value( _T("point"), &tmpPoint ,bLoad); } //these converions are done since I convert a string(member of dialog) //to required variables (look at source how it's done) //values that are not contain values are not needed to persist // persistObj.Path(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence\\Some SubKey\\Subkey2Delete"), bLoad); // persistObj.Path(_T("HKEY_CURRENT_USER\\Software\\Amir Israeli\\Registry Persistence\\Some SubKey\\Another SubKey"), bLoad); } //persisting of raw data : right from vriables will be much //easier than that of controls : thers a need to manipulate some of data //these 2 function use a feature of creating an outer file : // a map of persisting the contents : you'll be able to insert this file // as a resource and treat it as a memory mapped file void CPersistDlg::OnSavemap() { //you can put any file name(no path=in current dir) (or full path) persistObj.Serialize( _T("mydata.map"), FALSE); } void CPersistDlg::OnLoadmap() { //loading map from file persistObj.Serialize( _T("mydata.map"), TRUE); } void CPersistDlg::OnInfo() { #ifdef _DEBUG persistObj.Info(); #else AfxMessageBox(_T("Info function not \nsuppoted in debug version") ,MB_OK,0); #endif } //Load a map from a resource void CPersistDlg::OnLoadres() { persistObj.LoadFromResource( IDR_MAP1,_T("MAP")); } BOOL CPersistDlg::PreTranslateMessage(MSG* pMsg) { tip.RelayEvent(pMsg); return CDialog::PreTranslateMessage(pMsg); }