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

📄 modifyiehomepagedlg.cpp

📁 VisualC高级编程技术精粹.rar
💻 CPP
字号:
// ModifyIEHomepageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ModifyIEHomepage.h"
#include "ModifyIEHomepageDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const LPCTSTR lcsKeyPath = _T("Software\\Microsoft\\Internet Explorer\\Main\\");
/////////////////////////////////////////////////////////////////////////////
// CModifyIEHomepageDlg dialog

CModifyIEHomepageDlg::CModifyIEHomepageDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CModifyIEHomepageDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CModifyIEHomepageDlg)
	m_csOldHomepage = _T("");
	m_csNewHomepage = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CModifyIEHomepageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModifyIEHomepageDlg)
	DDX_Text(pDX, IDC_EDIT_OLD_HOMEPAGE, m_csOldHomepage);
	DDX_Text(pDX, IDC_EDIT_NEW_HOMEPAGE, m_csNewHomepage);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CModifyIEHomepageDlg, CDialog)
	//{{AFX_MSG_MAP(CModifyIEHomepageDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONFIRM, OnButtonConfirm)
	ON_BN_CLICKED(IDC_BUTTON_QUIT, OnButtonQuit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModifyIEHomepageDlg message handlers

BOOL CModifyIEHomepageDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	// 设定旧的主页
	SetOldHomepage();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CModifyIEHomepageDlg::OnButtonConfirm() 
{
	UpdateData();

	// 用户没有输入新主页,系统弹出提示信息
	if (m_csNewHomepage.IsEmpty())
	{
		MessageBox( "请输入新主页!", "友情提示", MB_ICONASTERISK );
		GetDlgItem(IDC_EDIT_NEW_HOMEPAGE)->SetFocus();
		return;
	}

	// 将主页修改成新输入的主页
	ModifyHomePage(lcsKeyPath,m_csNewHomepage);	
}

void CModifyIEHomepageDlg::OnButtonQuit() 
{
	CDialog::OnCancel();
}

CString CModifyIEHomepageDlg::FindHomePage(LPCTSTR lcsKeyPath)
{
	HKEY hKEY; //定义有关的 hKEY, 在查询结束时要关闭。 

	// 打开hKEY,第一个参数为根键名称,第二个参数 
	// 表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。 
	// 访问注册表,hKEY则保存此函数所打开的键的句柄。 
	long lRetOpenKey = ( ::RegOpenKeyEx(HKEY_CURRENT_USER,lcsKeyPath, 0, KEY_READ, &hKEY) ); 
	if ( lRetOpenKey != ERROR_SUCCESS ) //如果无法打开hKEY,则终止程序的执行 
	{
        MessageBox( _T("无法打开有关的hKEY!"), _T("错误发生"), MB_OK );
	    return _T("");
    } 

	// 查询有关的数据 (Start Page StartPage_Get)。 
	LPBYTE Homepage_Get = new BYTE[240]; 
	DWORD dType = REG_SZ ;
	DWORD dData = 240; 

	// hKEY为刚才RegOpenKeyEx()函数所打开的键的句柄,″RegisteredOwner″。 
	// 表示要查询的键值名,type_1表示查询数据的类型,owner_Get保存所。 
	// 查询的数据,cbData_1表示预设置的数据长度。 
	long lRetQueryValue = ::RegQueryValueEx( hKEY, "Start Page", NULL, &dType, Homepage_Get, &dData ); 
	if ( lRetQueryValue != ERROR_SUCCESS ) 
	{ 
        MessageBox( _T("无法查询有关注册表信息!"), _T("错误发生"), MB_OK );
	    return _T(""); 
	} 
	 
	CString csHomePage = CString( Homepage_Get ); 
	::RegCloseKey(hKEY);

    return csHomePage;
}

void CModifyIEHomepageDlg::ModifyHomePage(LPCTSTR lcsKeyPath, 
										  CString csHomePage)
{
	// 定义有关的 hKEY, 在程序的最后要关闭。 
	HKEY hKEY; 
	// LPCTSTR data_Set = "Software\\Microsoft\\Internet Explorer\\Main\\"; 

	// 打开hKEY,KEY_WRITE表示以写的方式打开。 
	long lRetOpenKey = ( ::RegOpenKeyEx(HKEY_CURRENT_USER, lcsKeyPath, 0, KEY_WRITE,&hKEY) ); 
	if ( lRetOpenKey != ERROR_SUCCESS ) 
	{ 
        MessageBox( "无法打开有关的hKEY!", "错误发生", MB_OK );
	    return; 
	} 

	// 将CString型转换为LPBYTE。 
	LPBYTE Homepage_Set = CString_To_LPBYTE( m_csNewHomepage ); 
	DWORD dType = REG_SZ; 
	DWORD dData = m_csNewHomepage.GetLength() + 1; 

	// 与RegQureyValueEx()类似,hKEY表示已打开的键的句柄,″Start Page″ 
	// 表示要访问的键值名,owner_Set表示新的键值,type_1和cbData_1表示新值。 
	// 的数据类型和数据长度 
	long lSetValue = ::RegSetValueEx( hKEY, "Start Page", NULL, dType, Homepage_Set, dData ); 
	if ( lSetValue != ERROR_SUCCESS ) 
	{ 
        MessageBox( "无法修改有关注册表信息!", "错误发生", MB_OK );
	    return; 
	}  

    MessageBox( "注册表修改成功!", "友情提示", MB_ICONASTERISK );

    return;
}

LPBYTE CModifyIEHomepageDlg::CString_To_LPBYTE(CString csDstString)
{
	// 将字符串转换成字节类型
	LPBYTE lpb = new BYTE[csDstString.GetLength()+1];  
    for ( int i = 0; i < csDstString.GetLength(); i++ )
    {
        lpb[i] = csDstString[i];
    }

    lpb[csDstString.GetLength()] = 0;

    return lpb;
}

void CModifyIEHomepageDlg::SetOldHomepage()
{
	// 调用FindHomePage函数查找旧的主页
	m_csOldHomepage = FindHomePage(lcsKeyPath);
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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