⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysenvvardlg.cpp

📁 本文件中包含的是《VC编程100例》一书中的源代码
💻 CPP
字号:
// SysEnvVarDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SysEnvVar.h"
#include "SysEnvVarDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "Warning1.h"
#include "Warning2.h"
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSysEnvVarDlg dialog

CSysEnvVarDlg::CSysEnvVarDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSysEnvVarDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSysEnvVarDlg)
	m_strVarName = _T("");
	m_strNewVal = _T("");
	m_strOldVal = _T("");
	m_nType = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bFlag = FALSE;
	m_strVarName = "Temp";
}

void CSysEnvVarDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSysEnvVarDlg)
	DDX_CBString(pDX, IDC_ENVVARNAME, m_strVarName);
	DDX_Text(pDX, IDC_NEWVAL, m_strNewVal);
	DDX_Text(pDX, IDC_OLDVAL, m_strOldVal);
	DDX_CBIndex(pDX, IDC_MYTYPE, m_nType);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSysEnvVarDlg, CDialog)
	//{{AFX_MSG_MAP(CSysEnvVarDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_MYCHANGE, OnMychange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSysEnvVarDlg message handlers

BOOL CSysEnvVarDlg::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 CSysEnvVarDlg::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 CSysEnvVarDlg::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 CSysEnvVarDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//修改系统环境变量的值
void CSysEnvVarDlg::OnMychange() 
{
	// TODO: Add your control notification handler code here
	
	UpdateData(TRUE);
	HKEY hKey;						//定义有关的 hKey, 在查询结束时要关闭。
	LPCTSTR lpSubKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\";

	//打开与路径 lpSubKey 相关的 hKey
	//第一个参数为根键名称,第二个参数表示要访问的键的位置
	//第三个参数必须为0,第四个参数KEY_WRITE表示以写的方式访问注册表
	//第五个参数hKey则保存此函数所打开的键的句柄。
	LONG lRt=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpSubKey, 0, KEY_WRITE, &hKey));
	if(lRt != ERROR_SUCCESS)		//如果无法打开hKey,则终止程序的执行
	{
		RegCloseKey(hKey);			//关闭hKey
		MessageBox("错误: 无法打开有关的hKey!");
		exit(1);
	}
	
	//修改键值,先将CString型转换为LPBYTE。
	LPBYTE lpbSet = new BYTE[m_strNewVal.GetLength()+1];
	DWORD dType;
	DWORD dSize = m_strNewVal.GetLength()+1;

	//确定环境变量的类型,默认为REG_SZ
	switch(m_nType)
	{
	case 0:	dType = REG_BINARY;
		break;
	case 1:	dType = REG_DWORD;
		break;
	case 2:	dType = REG_DWORD_LITTLE_ENDIAN;
		break;
	case 3:	dType = REG_DWORD_BIG_ENDIAN;
		break;
	case 4:	dType = REG_EXPAND_SZ;
		break;
	case 5:	dType = REG_LINK;
		break;
	case 6:	dType = REG_MULTI_SZ;
		break;
	case 7:	dType = REG_NONE;
		break;
	case 8:	dType = REG_RESOURCE_LIST;
		break;
	case 9:	dType = REG_SZ;
		break;
	default: 
			dType = REG_SZ;
	}

	for(INT i = 0; i < m_strNewVal.GetLength(); i++)
		lpbSet[i] = m_strNewVal[i];
	lpbSet[m_strNewVal.GetLength()] = 0;

	//环境变量名为空,提示是否要改变默认环境变量
	if(m_strVarName.IsEmpty())
	{
		CWarning1 cW1;
		cW1.m_pParent = this;
		cW1.DoModal();
	}
	//环境变量名非空但是新值为空,提示是否要进行更改
	else if(m_strNewVal.IsEmpty())
	{
		CWarning2 cW2;
		cW2.m_pParent = this;
		cW2.DoModal();
	}
	//输入符合要求
	else
		m_bFlag = TRUE;

	//与RegQureyValueEx()类似,hKEY表示已打开的键的句柄,m_strVarName表示要访问的键值名
	//lpbSet表示新的键值,dType和dSize表示新值的数据类型和数据长度
	LONG lReturn;
	//用户确认要更改默认环境变量
	if(m_bFlag)
		lReturn = RegSetValueEx(hKey, m_strVarName, NULL, dType, lpbSet, dSize);
	
	//更改不成功,输出错误信息
	if(lReturn != ERROR_SUCCESS)
	{
		delete []lpbSet;
		RegCloseKey(hKey);
	}
	else
	{
		//输出提示信息,为成员变量赋值
		MessageBox("环境变量 " + m_strVarName + " 修改成功!", "完成", MB_OK);
		m_strNewVal = "";
		m_strOldVal = "";
		m_strVarName = "";
		m_nType = -1;
		m_bFlag = FALSE;
	
		//释放内存,关闭hKey
		delete []lpbSet;
		RegCloseKey(hKey);
		UpdateData(FALSE);
	}
}

//重写OnOK消息处理函数,查询系统环境变量
void CSysEnvVarDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	if(!m_strVarName.IsEmpty())
	{
		HKEY hKey;						//定义有关的 hKey, 在查询结束时要关闭。
		LPCTSTR lpSubKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\";

		//打开与路径 lpSubKey 相关的 hKey
		//第一个参数为根键名称,第二个参数表示要访问的键的位置
		//第三个参数必须为0,第四个参数KEY_READ表示以查询的方式访问注册表
		//第五个参数hKey则保存此函数所打开的键的句柄。
		LONG lRt=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpSubKey, 0, KEY_READ, &hKey));
		if(lRt != ERROR_SUCCESS)		//如果无法打开hKey,则终止程序的执行
		{
			RegCloseKey(hKey);			//关闭hKey
			MessageBox("错误: 无法打开有关的hKey!", "错误", MB_OK);
			exit(1);
		}

		//查询键值
		LPBYTE lpbGet = new BYTE[80];
		DWORD dType;
		DWORD dSize = 80;

		//hKey为刚才RegOpenKeyEx()函数所打开的键的句柄
		//m_strVarName表示要查询的键值名,dType表示查询数据的类型
		//lpbGet保存所查询的数据,dSize表示预设置的数据长度
		lRt = RegQueryValueEx(hKey, m_strVarName, NULL, &dType, lpbGet, &dSize);
		if(lRt != ERROR_SUCCESS)
		{
			RegCloseKey(hKey);
			MessageBox("错误: 无法查询有关注册表信息!", "错误", MB_OK);
			exit(1);
		}

		//保存键值,释放内存
		m_strOldVal = CString(lpbGet);
		delete []lpbGet; 

		//程序结束前要关闭已经打开的 hKey
		RegCloseKey(hKey);

		switch(dType)
		{
		case REG_BINARY:				m_nType = 0;
			break;
		case REG_DWORD:					m_nType = 1;  //REG_DWORD_LITTLE_ENDIAN == REG_DWORD
			break;
		case REG_DWORD_BIG_ENDIAN:		m_nType = 3;
			break;
		case REG_EXPAND_SZ:				m_nType = 4;
			break;
		case REG_LINK:					m_nType = 5;
			break;
		case REG_MULTI_SZ:				m_nType = 6;
			break;
		case REG_NONE:					m_nType = 7;
			break;
		case REG_RESOURCE_LIST:			m_nType = 8;
			break;
		case REG_SZ:					m_nType = 9;
			break;
		}
	}
	else
		m_nType = 9;

	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -