MFC—SaleSystem project (Part 1)

New project

1.1 Create a project according to the wizard

First, create a new MFC project (SaleSystem), based on a single document, MFC standard project, and remove the toolbar:

1.2 Add preprocessing instructions

Since Microsoft no longer recommends using the traditional C/C++ library functions scanf, strcpy, sprintf, etc. in VS2013, directly using these library functions will prompt a C4996 error.

VS recommends using functions with _s, such as scanf_s and strcpy_s, but these are not standard C/C++ functions. To continue using this function, you need to add the _CRT_SECURE_NO_WARNINGS predefinition.

Add _CRT__NO_WARNINSECUREGS in Project -> Properties -> C/C + ±> Preprocessor -> Preprocessor Settings:

1.3 Set window properties

  1. settings icon
    Open the resource view, add the local ICO icon, and add the icon resource we prepared in advance in the res folder:

Modify the ID (IDI_ICON_WIN) in the resource’s properties:

Add the following code in OnCreate() of CMainFrame:

//Set the icon, IDI_ICON_WIN is the icon resource ID, this is the WINAPI function
SetClassLong(m_hWnd, GCL_HICON, (LONG)AfxGetApp()->LoadIconW(IDI_ICON_WIN));
  1. Set window size and center display
Still write the code in OnCreate() of CMainFrame:
//Set the position and size of the window: CWnd::MoveWindow
//0, 0, starting point coordinates x and y
//800, 500, window width and height
MoveWindow(0, 0, 800, 500);

//Move the window to the center of the screen, CWnd::CenterWindow
CenterWindow();
  1. Set window title
    Add the following code to the OnNewDocument() function in the CSaleSystemDoc document class:

//Set window title, CDocument::SetTitle
SetTitle(TEXT("Sales Management System"));

The program running effect diagram is as follows:

2 File processing

2.1 File content format

Login user information:

Product information:

2.2 Design file processing class CInfoFile

  1. Add file processing class CInfoFile


  1. Design of header file (InfoFile.h)
    Define two configuration file path macros:
#define _F_LOGIN "./login.ini"
#define _F_STOCK "./stock.txt"

Add the file information structure, as follows:

struct msg
{<!-- -->
int id; //product id
string name; //Product name, don’t forget to include the corresponding header file
int price; //product price
int num; //Number of items
};

There are many products, and they need to be added and deleted frequently. You can consider using a linked list to store them. Therefore, add a list type member variable to the member variable:

list<msg> ls; //Storage product container, don’t forget to include the corresponding header file
int num; //Used to record the number of products

There are two types of files that need to be read and written in the project, user information configuration files and product information files. The specific API interface is as follows:

//Read login information
void ReadLogin(CString & amp;name, CString & amp;pwd);

\t//change Password
void WritePwd(char* name, char* pwd);

//Read product data
void ReadDocline();

//Write product to file
void WirteDocline();

//Add new product
void Addline(CString name, int num, int price);

3 Login dialog box

3.1 UI design

1) Add a dialog box resource (change the ID to DIALOG_LOGIN) and add the required controls:

2) Select the dialog box -> right click -> Add class -> Class name: CLoginDlg

3) According to the requirements, the control associates the required variables

The user name editing area is associated with CString m_user, and the password login box is associated with CString m_pwd.

3.2 Function implementation

1) In the dialog class, rewrite the OnInitDialog function, initialize it, and set some default login information.

m_user = TEXT("Axe Gang Leader"); //Username
m_pwd = TEXT("123456");//Password
UpdateData(FALSE); //The content is updated to the corresponding control

2) Creation of login window
Create a login dialog box before the APP is created in InitInstance() of the application class CSaleSystemApp:

CLoginDlg dlg; //To create a login dialog box, the header file #include "LoginDlg.h" is required
dlg.DoModal(); //Run in modal mode

3) Implementation of login button function

//Login button processing function
void CLoginDlg::OnBnClickedButton1()
{<!-- -->
// TODO: Add control notification handler code here

UpdateData(TRUE); //Update the data of the control to the corresponding variable

CInfoFile file; //To create an operating file class object, a header file is required #include "InfoFile.h"
CString user, pwd;

//Read the configuration file and obtain the username and password. The parameters are passed by reference.
file.ReadLogin(user, pwd);

if (m_user == user)//user names are equal
{<!-- -->
if (m_pwd != pwd)
{<!-- -->
MessageBox(_T("Wrong password"));
m_user.Empty(); //Clear
m_pwd.Empty();
}
else
{<!-- -->
CDialogEx::OnOK();
}
}
else
{<!-- -->
MessageBox(_T("Username does not exist"));
m_user.Empty();
m_pwd.Empty();
}
}

4) Implementation of cancel button function

//Cancel button function implementation
void CLoginDlg::OnBnClickedButton2()
{<!-- -->
// TODO: Add control notification handler code here
exit(0); //End the entire program
}

5) Implementation of the close button function in the upper right corner
Select the dialog box template -> right-click -> Properties -> Message -> WM_CLOSE

//Close button
void CLoginDlg::OnClose()
{<!-- -->
// TODO: Add message handler code and/or call defaults here
exit(0); //End the entire program

CDialogEx::OnClose();
}

6) Solving the problem of closing the dialog box by pressing the Enter key in the editing area