?? regdemodlg.cpp
字號:
// RegDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RegDemo.h"
#include "RegDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//注冊表操作
HKEY hKey;
char content[256]; //所查詢注冊表鍵值的內容
DWORD dwType=REG_SZ; //定義讀取數據類型
DWORD dwLength=256;
struct HKEY__*RootKey; //注冊表主鍵名稱
TCHAR *SubKey; //欲打開注冊表項的地址
TCHAR *KeyName; //欲設置項的名字
TCHAR *ValueName; //欲設置值的名稱
LPBYTE SetContent_S; //字符串類型
int SetContent_D[256]; //DWORD類型
BYTE SetContent_B[256]; //二進制類型
int ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);
int SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S);
int SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]);
int SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]);
int DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName);
int DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);
//查看函數
ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName)
{
int i=0; //操作結果:0==succeed
if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS)
{
if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(unsigned char *)content,&dwLength)!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
i=1;
}
RegCloseKey(hKey);
}
else
{
AfxMessageBox("錯誤:無法打開有關的hKEY");
i=1;
}
return i;
}
//設置字符串值函數
SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S)
{
int i=0; //操作結果:0==succeed
//int StrLength;
//StrLength=CString(SetContent_S).GetLength();
if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
{
if(RegSetValueEx(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,CString(SetContent_S).GetLength())!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法設置有關的注冊表信息");
i=1;
}
RegCloseKey(hKey);
}
else
{
AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
i=1;
}
return i;
}
//設置DWORD值函數
SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256])
{
int i=0; //操作結果:0==succeed
if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
{
if(RegSetValueEx(hKey,ReValueName,NULL,REG_DWORD,(const unsigned char *)ReSetContent_D,4)!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法設置有關的注冊表信息");
i=1;
}
RegCloseKey(hKey);
}
else
{
AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
i=1;
}
return i;
}
//設置二進制值函數
SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256])
{
int i=0; //操作結果:0==succeed
if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
{
if(RegSetValueEx(hKey,ReValueName,NULL,REG_BINARY,(const unsigned char *)ReSetContent_B,4)!=ERROR_SUCCESS)
{
AfxMessageBox("錯誤:無法設置有關的注冊表信息");
i=1;
}
RegCloseKey(hKey);
}
else
{
AfxMessageBox("錯誤:無法查詢有關的注冊表信息");
i=1;
}
return i;
}
//刪除子項函數
DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName)
{
int i=0; //操作結果:0==succeed
if((RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey))==ERROR_SUCCESS)
{
if((RegDeleteKey(hKey,ReKeyName))!=ERROR_SUCCESS)
{
//AfxMessageBox("清除指定項失敗!");
i=1;
}
RegCloseKey(hKey);
}
else
{
//AfxMessageBox("錯誤:無法打開有關的hKEY");
i=1;
}
return i;
}
//刪除鍵值函數
DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName)
{
int i=0; //操作結果:0==succeed
if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
{
if(RegDeleteValue(hKey,ReValueName)!=ERROR_SUCCESS)
{
//AfxMessageBox("清除指定值失敗!");
i=1;
}
RegCloseKey(hKey);
}
else
{
//AfxMessageBox("錯誤:無法打開有關的hKEY");
i=1;
}
return i;
}
//注冊表操作結束
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRegDemoDlg dialog
CRegDemoDlg::CRegDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRegDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRegDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRegDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRegDemoDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRegDemoDlg, CDialog)
//{{AFX_MSG_MAP(CRegDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnSetValue_S)
ON_BN_CLICKED(IDC_BUTTON2, OnSetContent_B)
ON_BN_CLICKED(IDC_BUTTON3, OnSetContent_D)
ON_BN_CLICKED(IDC_BUTTON4, OnDeleteValue_1)
ON_BN_CLICKED(IDC_BUTTON5, OnDeleteValue_2)
ON_BN_CLICKED(IDC_BUTTON6, OnDeleteValue_3)
ON_BN_CLICKED(IDC_BUTTON7, OnDeleteKey)
ON_BN_CLICKED(IDC_BUTTON8, OnShowContent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRegDemoDlg message handlers
BOOL CRegDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CRegDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CRegDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRegDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRegDemoDlg::OnSetValue_S()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例1"; //欲設置值的名稱
SetContent_S=LPBYTE("成功"); //值的內容
if((SetValue_S(RootKey,SubKey,ValueName,SetContent_S))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnSetContent_B()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例2"; //欲設置值的名稱
SetContent_B[0]=1; //值的內容
//SetContent_B[1]=0x1B;
//SetContent_B[2]=0x2C;
//SetContent_B[3]=0x3D;
//SetContent_B[4]=0x4E;
if((SetValue_B(RootKey,SubKey,ValueName,SetContent_B))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnSetContent_D()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例3"; //欲設置值的名稱
SetContent_D[0]=4294967295; //值的內容
if((SetValue_D(RootKey,SubKey,ValueName,SetContent_D))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnDeleteValue_1()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例1"; //欲設置值的名稱
if((DeleteValue (RootKey,SubKey,ValueName))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnDeleteValue_2()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例2"; //欲設置值的名稱
if((DeleteValue (RootKey,SubKey,ValueName))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnDeleteValue_3()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例3"; //欲設置值的名稱
if((DeleteValue (RootKey,SubKey,ValueName))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnDeleteKey()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"; //欲打開注冊表值的地址
KeyName="Doc Find Spec MRU"; //欲設置項的名稱
if((DeleteKey (RootKey,SubKey,KeyName))!=0)
AfxMessageBox("操作失敗!");
}
void CRegDemoDlg::OnShowContent()
{
// TODO: Add your control notification handler code here
RootKey=HKEY_CURRENT_USER; //注冊表主鍵名稱
SubKey="Software\\Microsoft"; //欲打開注冊表值的地址
ValueName="例1"; //欲設置值的名稱
if ((ShowContent(RootKey,SubKey,ValueName))==0)
MessageBox(content,"本操作是利用ShowContent()函數完成的。");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -