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

📄 passwordptddlg.cpp

📁 AppWizard has created this PasswordPTD application for you. This application not only demonstrates
💻 CPP
字号:
// PasswordPTDDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PasswordPTD.h"
#include "PasswordPTDDlg.h"

#define KEY_NAME "password"

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

/////////////////////////////////////////////////////////////////////////////
// CPasswordPTDDlg dialog

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

void CPasswordPTDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPasswordPTDDlg)
	DDX_Control(pDX, IDC_CHANGE_OK_BUTTON, m_oChangeOkButton);
	DDX_Control(pDX, IDC_OLD_PASSWORD_EDIT, m_oOldEdit);
	DDX_Control(pDX, IDC_NEW_PASSWORD_EDIT, m_oNewEdit);
	DDX_Control(pDX, IDC_CONFIRM_PASSWORD_EDIT, m_oConfirmEdit);
	DDX_Control(pDX, IDC_OLD_PW_STATIC, m_oOldPWStatic);
	DDX_Control(pDX, IDC_NEW_PW_STATIC, m_oNewPWStatic);
	DDX_Control(pDX, IDC_CONFIRM_PW_STATIC, m_oConfirmPWStatic);
	DDX_Control(pDX, IDC_CHANGE_STATIC, m_oChangeStaticBox);
	DDX_Control(pDX, IDC_CHANGE_PW_BUTTON, m_ChangePWButton);
	DDX_Control(pDX, IDC_STATUS_STATIC, m_oStatusStatic);
	DDX_Text(pDX, IDC_PASSWORD_EDIT, m_sPasswordEdit);
	DDV_MaxChars(pDX, m_sPasswordEdit, 15);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPasswordPTDDlg, CDialog)
	//{{AFX_MSG_MAP(CPasswordPTDDlg)
	ON_BN_CLICKED(IDC_CHANGE_PW_BUTTON, OnChangePwButton)
	ON_BN_CLICKED(IDC_CHANGE_OK_BUTTON, OnChangeOkButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPasswordPTDDlg message handlers

BOOL CPasswordPTDDlg::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
	
	//CenterWindow(GetDesktopWindow());	// center to the hpc screen
    SetWindowPos(GetDesktopWindow(), 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW);

	HKEY  hKeyResult;
	DWORD dwDisposition;
	DWORD dwType;
	DWORD dwBytes = 0;
	LPCTSTR sDefaultPassword = _T("password");
				
	// ja - open / create registry key
	RegCreateKeyEx(HKEY_CLASSES_ROOT, _T("THEBOOK\\PASSWORD"), NULL, NULL, REG_OPTION_NON_VOLATILE, 
		KEY_ALL_ACCESS, NULL, &hKeyResult, &dwDisposition);
	
	if (hKeyResult){

		CString sResult;
		 
	    RegQueryValueEx(hKeyResult, _T(KEY_NAME), 0, &dwType, NULL, &dwBytes);
		LPCTSTR sBuffer = sResult.GetBuffer(dwBytes + 1);

		if (sBuffer){
			::RegQueryValueEx(hKeyResult, _T(KEY_NAME), 0, &dwType, (LPBYTE)sBuffer, &dwBytes);
			sResult.ReleaseBuffer();
		}
	
		// ja - set the data (default to "password")
		if (lstrcmp(sBuffer, _T("")) == 0){
			if (RegSetValueEx(hKeyResult, _T(KEY_NAME), 0, REG_SZ, (LPBYTE)sDefaultPassword,
				(lstrlen(sDefaultPassword)+1)*sizeof(TCHAR)) != ERROR_SUCCESS)
					return false; // ja - exit the program
			else
				m_sPassword = sDefaultPassword;
		}
		else // assign data to member var
		{
			m_sPassword = sBuffer;
		}
		
		// ja - and finally close the key
		RegCloseKey(hKeyResult);

	}
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPasswordPTDDlg::OnOK() 
{
	// ja - if the password is valid let the function continue on (exit)
	if (CheckPassword())
		CDialog::OnOK();
}

void CPasswordPTDDlg::OnCancel() 
{
	// ja - if the password is valid let the function continue on (exit)
	if (CheckPassword())
		CDialog::OnCancel();
		
}

LRESULT CPasswordPTDDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// ja - check the message for activate
	if (message == WM_ACTIVATE){
		// ja - if disactivating ...
		if (!wParam){
			// ja - call local function to check for valid password
			if (!CheckPassword())
				// ja - call function to set the focus back to window
				::SetForegroundWindow(m_hWnd);
		}
	}
	
	return CDialog::WindowProc(message, wParam, lParam);
}

BOOL CPasswordPTDDlg::CheckPassword()
{
	// ja - get the text from the edit box
	UpdateData(TRUE);

	if (m_sPasswordEdit == m_sPassword)
		return TRUE;
	else
	{
		//UpdateData(TRUE);
		m_oStatusStatic.SetWindowText(_T("Invalid Password")); 
		//UpdateData(FALSE);
		// ja - T this did not work for CE ,i don't think i can make a system modal message box
		// ja - we will use the CWnd MessageBox here so we can make it system modal
		//MessageBox(_T("Invalid Password"), _T("Password Checker"), /*MB_OK |*/ MB_SYSTEMMODAL);
		return FALSE;
	}

}

void CPasswordPTDDlg::OnChangePwButton() 
{
	ShowControls(TRUE);

}
		

void CPasswordPTDDlg::OnChangeOkButton() 
{
	// ja- read in the text from the 3 fields
	CString sOldEdit;
	CString sNewEdit;
	CString sConfirmEdit;
	
	m_oOldEdit.GetWindowText(sOldEdit);
	m_oNewEdit.GetWindowText(sNewEdit);
	m_oConfirmEdit.GetWindowText(sConfirmEdit);

	// ja- test for empty fields
	/*if (sOldEdit == NULL || sNewEdit == NULL || sConfirmEdit == NULL){
		m_oStatusStatic.SetWindowText(_T("One or more Fields Left Blank!!")); 
		return;
	}*/

	// ja- test the old password with the new one
	if (sOldEdit != m_sPassword){
		m_oStatusStatic.SetWindowText(_T("Invalid Old Password!!")); 
		ClearFields();	
		return;
	}

	// ja- make sure the new passwords match 
	if (sNewEdit != sConfirmEdit){
		m_oStatusStatic.SetWindowText(_T("New and Confirm Passwords Differ!!")); 
		ClearFields();
		return;
	}

	m_sPassword = sNewEdit;
	
	// ja - copy the cstring into a null term string
	LPTSTR sTemp = new TCHAR[sNewEdit.GetLength()+1];
	_tcscpy(sTemp, sNewEdit);

	// ja - now write the new password to the reg
	HKEY  hKeyResult;

	// ja - open registry key
	RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("THEBOOK\\PASSWORD"), 0, KEY_ALL_ACCESS, &hKeyResult);
				
	if (hKeyResult){

		// ja - set the data 
		if (RegSetValueEx(hKeyResult, _T(KEY_NAME), 0, REG_SZ, (LPBYTE)sTemp, 
			(lstrlen(sTemp)+1)*sizeof(TCHAR)) == ERROR_SUCCESS)
				m_oStatusStatic.SetWindowText(_T("Password Sucsefully Changed!!")); 
                
		// ja - and finally close the key
		RegCloseKey(hKeyResult);

	}

	ShowControls(FALSE);
}

void CPasswordPTDDlg::ShowControls(BOOL bShow)
{
	// ja - clear out old values
	ClearFields();
	
	// ja- switch buttons
	m_ChangePWButton.ShowWindow(!bShow);
	m_oChangeOkButton.ShowWindow(bShow);
	
	// ja- now the controls
	m_oChangeStaticBox.ShowWindow(bShow);
	m_oOldPWStatic.ShowWindow(bShow);
	m_oOldEdit.ShowWindow(bShow);
	m_oNewPWStatic.ShowWindow(bShow);
	m_oNewEdit.ShowWindow(bShow);
	m_oConfirmEdit.ShowWindow(bShow);
	m_oConfirmPWStatic.ShowWindow(bShow);
}

void CPasswordPTDDlg::ClearFields()
{
	// ja- clear the edit fields
	m_oOldEdit.Clear(); 
	m_oNewEdit.Clear();
	m_oConfirmEdit.Clear();
	
	UpdateData(TRUE);
	m_sPasswordEdit = "";
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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