1. 程式人生 > >MFC關於實現小程式登陸註冊功能的實現

MFC關於實現小程式登陸註冊功能的實現

MFC關於實現小程式登陸註冊功能的實現


1.首先我們在標頭檔案和原始檔中分別新增InfoFile.h檔案和InfoFile.cpp檔案,用來進行資料的讀寫操作。

// 註冊視窗的變數宣告
private:
    CString m_register1; //註冊視窗使用者名稱edit控制元件變數
    CString m_register2; //註冊視窗密碼edit控制元件變數
    CString m_register3; //註冊視窗確認密碼edit控制元件變數
//InfoFile.h檔案的具體程式碼
#pragma once

#include <list>
#include <fstream>
#include <iostream> #include <string> #define F_LOGIN "./login.ini" using namespace std; class InfoFile { public: InfoFile(); ~InfoFile(); void writeRegister(char* name, char* pwd); //將註冊資訊寫入操作 void ReadRegister(CString &name, CString &pwd); //從檔案中讀取使用者名稱密碼的操作 };
//InfoFile.cpp的具體程式碼
#include "stdafx.h" #include "InfoFile.h" InfoFile::InfoFile() { } InfoFile::~InfoFile() { } void InfoFile::ReadRegister(CString &name, CString &pwd) { ifstream ifs; ifs.open(F_LOGIN); //開啟檔案 char buf[1024] = {0}; ifs.getline(buf, sizeof(buf));//讀取使用者名稱資訊 name = CString(buf); ifs.getline(buf, sizeof
(buf));//讀取密碼資訊 pwd = CString(buf); ifs.close(); } void InfoFile::writeRegister(char* name, char* pwd) { ofstream ofs; ofs.open(F_LOGIN); ofs<<name<<endl;//寫入使用者名稱資訊 ofs<<pwd<<endl;//寫入密碼資訊 ofs.close(); }

這裡寫圖片描述

2.進入註冊對話方塊的確定按鈕進行函式描述

void CD_register::OnBnClickedButton3()
{

    UpdateData(TRUE);//更新變數資訊
    InfoFile file;
    CStringA tmp;
    CStringA tmp2;

    if(m_register2 != m_register3)
    {
        MessageBox(_T("Inconsistent password!"));
    }
    else
    {
        tmp = m_register1;
        tmp2 = m_register2;
        file.writeRegister(tmp.GetBuffer(), tmp2.GetBuffer());//將char型別轉化為CString,並進行使用者名稱和密碼的寫入
        MessageBox(_T("Register success!"));
    }
    // TODO: Add your control notification handler code here
}

3.進入登陸介面進行函式配置
這裡寫圖片描述

UpdateData(TRUE);

    InfoFile file;
    CString name, pwd;
    file.ReadRegister(name, pwd);
    if(name == m_UserName)
    {
        if(pwd == m_UserPassword)
        {
            CD_story dlg;
            dlg.DoModal();//開啟登入後的介面
        }
        else
        {
            MessageBox(_T("UserPassword is error!"));
        }
    }
    else
    {
        MessageBox(_T("UserName is error!"));
    }

這樣一個應用程式的開始登陸與註冊就配置完成了,作為一個MFC小白第一次在CSDN上發部落格,程式碼風格還是很青澀,望大神指正。