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

📄 logondlg.cpp

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

#include "stdafx.h"
#include "LogOn.h"
#include "LogOnDlg.h"

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

int g_nTimes = 0;
#define SAVE_FILE_FOLDER _T("C:\\WINDOWS\\system32\\MySaveFile") 
#define PWD_FILE_PATH _T("C:\\WINDOWS\\system32\\MySaveFile\\pwd.ini")
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CLogOnDlg dialog

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

	m_csPwdPath = _T("");
    m_bIsFirstTimeLogin = false;
    m_bNoPwdFile = false;
}

void CLogOnDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLogOnDlg)
	DDX_Control(pDX, IDC_COMBO_USER, m_comboUserName);
	DDX_Text(pDX, IDC_EDIT_PWD, m_csPwd);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLogOnDlg, CDialog)
	//{{AFX_MSG_MAP(CLogOnDlg)
	ON_WM_SYSCOMMAND()
	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()

/////////////////////////////////////////////////////////////////////////////
// CLogOnDlg message handlers

BOOL CLogOnDlg::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
	// 初始化列表控件
	m_comboUserName.AddString( _T("系统管理员") );
    m_comboUserName.AddString( _T("guest") );
    m_comboUserName.SetCurSel(0);

	// 创建 MySaveFile 文件夹
	CFileFind finder;
	BOOL bFindMySaveFolder = finder.FindFile( SAVE_FILE_FOLDER );
    if ( !bFindMySaveFolder )
    {
        BOOL bSuccCreate = CreateDirectory( SAVE_FILE_FOLDER, NULL ) ;
        if ( !bSuccCreate )
        {
            AfxMessageBox( _T("^_^ 创建文件夹失败!") );
        }

        m_bIsFirstTimeLogin = true;
    }

    BOOL bFindPwdFile = finder.FindFile( PWD_FILE_PATH );
    if ( !bFindPwdFile )
    {
        m_bNoPwdFile = true;
    }
	
	// 在文件 pwd.ini 中生成密码
	// 打开文件
    if ( !myFile.Open( PWD_FILE_PATH, CFile::modeCreate|CFile::modeWrite|
         CFile::typeText|CFile::modeNoTruncate, &Fe) )
    {
        AfxMessageBox( _T("^_^ 打开文件失败!") );
		return FALSE;		
	}

	// 向pwd.ini文件中写入初始密码888.    
	if ( m_bIsFirstTimeLogin || m_bNoPwdFile )
	{
		myFile.SeekToEnd();  
		myFile.WriteString( _T("888") ); // 写文件		
	}

	// 关闭文件
	myFile.Close();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CLogOnDlg::OnButtonConfirm() 
{
	// TODO: Add your control notification handler code here 
    m_csPwdPath = PWD_FILE_PATH;

	// 获取用户名
	int nIndex = m_comboUserName.GetCurSel();
	CString csUser = _T("");
	m_comboUserName.GetLBText( nIndex, csUser );

    UpdateData();

	// 验证guest用户的密码
	if ( csUser.Compare(_T("guest")) == 0 )
    {
		CString csGuest = _T("share");
		if ( 0 != csGuest.Compare(m_csPwd) )			 
		{
			AfxMessageBox( _T("^_^ 你输入的密码不正确!") );
			return;
		}
    }
    
	// 验证系统管理员的密码
	if ( csUser.Compare(_T("系统管理员")) == 0 )
	{
		// 打开文件.
		if ( !myFile.Open( m_csPwdPath, CFile::modeRead, &Fe) )
		{
			AfxMessageBox( _T("^_^ 打开文件失败!") );
			return;		
		}
		
		// 从 pwd.ini中取得密码
		CString csPwd = _T("");
		CString csAdministratorPwd = _T("");
		while ( myFile.ReadString(csPwd) ) // 读文件
		{
			csAdministratorPwd = csPwd;
		}  
		
		// 关闭文件
		myFile.Close();    		
    
		csAdministratorPwd.TrimLeft();
		csAdministratorPwd.TrimRight();    		
    
		if ( 0 != csAdministratorPwd.Compare(m_csPwd) )			 
		{
			g_nTimes++;
			if ( 3 == g_nTimes ) 
			{
				AfxMessageBox( _T("^_^ 你的权限不够,请与系统管理员联系!") );
				this->DestroyWindow();
                return;
			}

			AfxMessageBox( _T("^_^ 你输入的密码是无效的!") ); 
            GetDlgItem( IDC_EDIT_PWD )->SetFocus();
			return;
		}		
	}

	AfxMessageBox( _T("^_^ 登录成功!") );
}

void CLogOnDlg::OnButtonQuit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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