用VC6編寫登錄檔管理程式(三) (轉)

amyz發表於2007-11-28
用VC6編寫登錄檔管理程式(三) (轉)[@more@]

用VC6編寫登錄檔管理(三):namespace prefix = o ns = "urn:schemas--com::office" />

顧劍輝(Solarsoft)

 

右邊列表CregExplorerView類的實現:

在類裡定義private型的變數  HKEY m_hKey;//通訊傳來主鍵

CString m_strSubKey;//子鍵名

1.  和文件類進行通訊的程式碼同上。

2.  顯示當前樹的資料

在CRegLeftView::OnSelchanging中有一通訊函式pDoc->m_RegExplorerView->

DoListView(hKey,strSubKey);

void CRegExplorerView::DoListView(HKEY hKey, LPCTSTR strSubKey)

{

  LV_ITEM  lvitem;

  int iActualItem;

  CString strTemp1="(預設)",strTemp2="(未設定型別)",strTe="(未設定鍵值)";

 

  m_hKey=hKey;

  m_strSubKey=strSubKey;

  lvitem.mask = LVIF_TEXT | LVIF_IMAGE;

  lvitem.iItem = 0;

  lvitem.iSubItem = 0;

  lvitem.pszText = strTemp1.GetBuffer(strTemp1.GetLength());

  lvitem.iImage =ICI_STR;

  iActualItem = GetListCtrl().InsertItem(&lvitem);

 

  GetListCtrl().SetItemText(iActualItem,1,strTemp2.GetBuffer(0));

    GetListCtrl().SetItemText(iActualItem,2,strTemp3.GetBuffer(0));

  EnumerateValues(hKey,strSubKey);//列舉該子鍵的所有資料項並顯示在ListView上

}

void CRegExplorerView::EnumerateValues(HKEY hKey, LPCTSTR cstrKey)

{

  static HKEY hLastKey = hKey;

  LONG lResult;

  D dwIndex = 0;

  HKEY hCurKey = hKey;

  DWORD dwKeyType;

  DWORD dwKeyDataLength, dwKeyNameLen;

  LPBYTE pbbinKeyData = NULL;

  TCHAR *tcKeyName = NULL;

  TCHAR tcDataType[1024] = _T("");

  lResult = RegOpenKeyEx(hCurKey, cstrKey, 0, KEY_QUERY_VALUE , &hKey);

  if(lResult != ERROR_SUCCESS)

    return;

 

  DWORD lNoOfValues = 0;

  DWORD lLongestKeyNameLen = 1;

  DWORD lLongestDataLen = 1;

 

  lResult = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &lNoOfValues, &lLongestKeyNameLen, &lLongestDataLen,

        NULL, NULL);

 

  if(lResult != ERROR_SUCCESS)

    return;

 

 

  hLastKey = hKey;

 

  lLongestKeyNameLen++;

  lLongestDataLen++;

 

  tcKeyName =  new TCHAR[lLongestKeyNameLen];

  pbbinKeyData = new BYTE[lLongestDataLen];

 

  CString cstrFinalData, cstrTemp;

 

  while(TRUE)

  {

    memset(pbbinKeyData, 0, lLongestDataLen);

    memset(tcKeyName, 0, lLongestKeyNameLen);

    dwKeyType = dwKeyDataLength = dwKeyNameLen = 0;

 

    dwKeyNameLen = lLongestKeyNameLen;

    dwKeyDataLength = lLongestDataLen;

 

    lResult = RegEnumValue(hKey, dwIndex, tcKeyName, &dwKeyNameLen, NULL, &dwKeyType, pbbinKeyData, &dwKeyDataLength);

    if(lResult == ERROR_NO_MORE_ITEMS)

    break;

  AddRegistryItem(tcKeyName, dwKeyType, pbbinKeyData, dwKeyDataLength, dwIndex);//顯示資料於ListView

 

    dwIndex++;

  }

  RegCloseKey(hKey);

  delete tcKeyName;

  delete pbbinKeyData;

}

int CRegExplorerView::AddRegistryItem(CString Name, DWORD Type, void *Data, DWORD DataSize, int Row)

{

  if (Name.IsEmpty())

  {

    DeleteAllItems();

    Name="(預設)";

  }

  LVITEM Item={0};

  Item.mask=LVIF_TEXT | LVIF_IMAGE;

  Item.cchTextMax=Name.GetLength();

  Item.iItem=Row;

  Item.pszText=Name.GetBuffer(0);

  int iPosition;

 

  CString FormatData;

 

  switch (Type)

  {

    case REG_DWORD_BIG_ENDIAN:

    case REG_DWORD: 

  {

    Item.iImage=1;

    iPosition=GetListCtrl().InsertItem(&Item);

    GetListCtrl().SetItemText(iPosition,1,"REG_DWORD");

    DWORD* dwData=(DWORD*) Data;

   

    if (Data!=NULL)

    {

      FormatData.Format("0x%x (%d)",*dwData,*dwData);

      GetListCtrl().SetItemText(iPosition,2,FormatData.GetBuffer(0));

    }

    break;

  }

 

    case REG_BINARY:

  {

    Item.iImage=1;

    iPosition=GetListCtrl().InsertItem(&Item);

    GetListCtrl().SetItemText(iPosition,1,"REG_BINARY");

    if (Data!=NULL)

    {

      FormatData=FormatBinary((BYTE*) Data,DataSize);

      GetListCtrl().SetItemText(iPosition,2,FormatData.GetBuffer(0));

    }

    break;

  }

 

    case REG_EXPAND_SZ:

    case REG_SZ:

    case REG_LINK:

    case REG_MULTI_SZ:

  {

    Item.iImage=0;

    iPosition=GetListCtrl().InsertItem(&Item);

    GetListCtrl().SetItemText(iPosition,1,"REG_SZ");

    if (Data!=NULL)

    {

      FormatData.Format(""%s"",(char*) Data);

      GetListCtrl().SetItemText(iPosition,2,FormatData.GetBuffer(0));

    }

    break;

  }

 

    default:

  {

    Item.mask=LVIF_TEXT;

    iPosition=GetListCtrl().InsertItem(&Item);

    GetListCtrl().SetItemText(iPosition,1,"REG_NONE");

    break;

  }

  }

 

  return iPosition;

}

到此資料的顯示已經基本完成,餘下為資料操作的程式碼

3.  操作當前值,雙擊名稱項,彈出對話方塊,需實現滑鼠的雙擊訊息NM_LCLK

 

void CRegExplorerView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)

{

  OnRegModify();

  *pResult = 0;

}

OnRegModify()是為配合選單的自定義訊息

void CRegExplorerView::OnRegModify()

{

  long lResult;

  ASSERT(m_hKey);

  if (GetListCtrl().GetedCount()>1)

  {

    TRACE0("CRegistryListCtrl::OnRegModify -> Can not modify several values.n");

    return;

  }

 

  POSITION pos=GetListCtrl().GetFirstSelectedItemPosition();

 

  int SelectedItem=0;

  SelectedItem=GetListCtrl().GetNextSelectedItem(pos);

 

  CString ItemType=GetListCtrl().GetItemText(SelectedItem,1);

 

  if (ItemType.Compare("REG_SZ")==0)//||ItemType.Compare("(未設定型別)")==0)

  { 

    CRegEditStrDlg EditStrDlg;

    CString KeyName, KeyData;

    KeyName=GetListCtrl().GetItemText(SelectedItem,0);

    KeyData=GetListCtrl().GetItemText(SelectedItem,2);

    EditStrDlg.SetString(KeyName, KeyData);

  //  if(ItemType.Compare("(未設定型別)")==0)

  //    KeyName="";

 

  if (EditStrDlg.odal()==IDOK)

  {

    EditStrDlg.GetString(KeyName, KeyData);

 

    DWORD dwDataSize=KeyData.GetLength();

      HKEY hKey;

    RegOpenKey(m_hKey, m_strSubKey, &hKey);

    RegSetValueEx(hKey, KeyName, 0, REG_SZ, (BYTE*) KeyData.GetBuffer(0), dwDataSize);

    RegCloseKey(hKey);

  }

  }

 

  if (ItemType.Compare("REG_DWORD")==0)

  {

    CRegEditDwordDlg EditDWORDDlg;

    CString KeyName=GetListCtrl().GetItemText(SelectedItem,0);

 

    DWORD KeyData;

    DWORD DataSize;

    HKEY hKey;

    DWORD dwType=REG_DWORD;

    DataSize=sizeof(DataSize);

    lResult = RegOpenKeyEx(m_hKey, m_strSubKey, 0, KEY_QUERY_VALUE , &hKey);

    if(lResult != ERROR_SUCCESS)

      return; 

 

    lResult=RegQueryValueEx(hKey, KeyName, 0, &dwType,(BYTE*) &KeyData, &DataSize);

    if(lResult != ERROR_SUCCESS)

      return;

 

    EditDWORDDlg.SetStringDWORD(KeyName, KeyData);

 

  if (EditDWORDDlg.DoModal()==IDOK)

  {

    EditDWORDDlg.GetStringDWORD(KeyName, KeyData);

    RegSetValueEx(hKey, KeyName, 0, REG_DWORD, (BYTE*) &KeyData, DataSize);

  }

 

    RegCloseKey(hKey);

  }

 

  if (ItemType.Compare("REG_BINARY")==0)

  {

    CString KeyName=GetListCtrl().GetItemText(SelectedItem, 0);

    CRegEditBinDlg EditBinaryDlg;

 

    HKEY hKey;

    BYTE Data[1024]="";

    DWORD dwDataSize=sizeof(Data);

    DWORD dwType=REG_BINARY;

    RegOpenKey(m_hKey, m_strSubKey, &hKey);

    RegQueryValueEx(hKey, KeyName, 0, &dwType, Data, &dwDataSize);

    EditBinaryDlg.SetStringData(KeyName, Data, dwDataSize);

 

  if (EditBinaryDlg.DoModal()==IDOK)

  {

    EditBinaryDlg.GetStringData(KeyName, Data, dwDataSize);

    RegSetValueEx(hKey, KeyName, 0, REG_BINARY, Data, dwDataSize);

  }

 

    RegCloseKey(hKey);

  } 

}

資料的顯示與操作的程式碼已經基本結束。附:工程資料

要到

以上程式碼程式VC6、中透過。

 

參考資料: Barretto VN的DriveExplorer Version 1.0

codeproject的reglistctrl

  Win32

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-985536/,如需轉載,請註明出處,否則將追究法律責任。

相關文章