C++课程设计--同学通讯录管理程序(MFC)

课设题目

同学通讯录管理程序

课设要求

①利用MFC的向导,创建基于对话框的应用程序,添加按钮、编辑框等控件;
②设计通讯录的数据结构(如:以数组或者动态链表)和相应的类;
③完成对通讯录中记录的增加、修改、删除和显示。
④将通讯录存入文件。能够打开并显示其中所存的同学信息,并可以进行后续的操作。

程序运行界面

在这里插入图片描述

核心代码

// Class Address listDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Class Address list.h"
#include "Class Address listDlg.h"
#include <vector>
#include<ostream>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class Student{
public:
	CString name;
	CString number;
	CString phone;
	CString email;
	CString yuanxi;
};
vector <Student>stu;
int n = -1;
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()

/////////////////////////////////////////////////////////////////////////////
// CClassAddresslistDlg dialog

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

void CClassAddresslistDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClassAddresslistDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT_Email, m_Email);
	DDX_Text(pDX, IDC_EDIT_Name, m_Name);
	DDX_Text(pDX, IDC_EDIT_Number, m_Number);
	DDX_Text(pDX, IDC_EDIT_Phone, m_Phone);
	DDX_Text(pDX, IDC_EDIT_Xibie, m_Xibie);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClassAddresslistDlg, CDialog)
	//{{AFX_MSG_MAP(CClassAddresslistDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONincrease, OnBUTTONincrease)
	ON_BN_CLICKED(IDC_BUTTONchange, OnBUTTONchange)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_BN_CLICKED(IDC_BUTTONdelete, OnBUTTONdelete)
	ON_BN_CLICKED(IDC_BUTTON6, OnButtontuichu)
	ON_BN_CLICKED(IDC_BUTTONpreserve, OnBUTTONpreserve)
	ON_BN_CLICKED(IDC_BUTTONduqu, OnBUTTONduqu)
	ON_BN_CLICKED(IDC_BUTTONSearch, OnBUTTONSearch)
	ON_BN_CLICKED(IDC_BUTTONqingkong, OnBUTTONqingkong)
	ON_BN_CLICKED(IDC_BUTTONqingkongshuju, OnBUTTONqingkongshuju)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClassAddresslistDlg message handlers

BOOL CClassAddresslistDlg::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_list.SetExtendedStyle(LVS_EX_FLATSB  //扁平风格滚动条
		|LVS_EX_FULLROWSELECT  //选择整行
		|LVS_EX_HEADERDRAGDROP  //报表头可以拖拽
		|LVS_EX_ONECLICKACTIVATE  //单击激活
		|LVS_EX_GRIDLINES);    //绘制表格
	//设置表头
	m_list.InsertColumn(0,"学号",LVCFMT_LEFT,90);
	m_list.InsertColumn(1,"姓名",LVCFMT_LEFT,90);
	m_list.InsertColumn(2,"系别",LVCFMT_LEFT,90);
	m_list.InsertColumn(3,"邮箱",LVCFMT_LEFT,90);
	m_list.InsertColumn(4,"电话",LVCFMT_LEFT,90);

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

}

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

void CClassAddresslistDlg::OnBUTTONincrease() 
{
	// TODO: Add your control notification handler code here

	int flag = 0;//以便于选择性执行if语句
	UpdateData(true);
	if(m_Name==""||m_Number==""||m_Xibie==""||
		m_Phone==""||m_Email=="")
	{
		flag = 1;
		MessageBox("输入信息不完善,请重新输入!");
	}

    if (flag==0)
	    for (int i = 0;i < stu.size();i++)
	{
		if ( stu[i].name == m_Name&&stu[i].phone == m_Phone&&stu[i].email == m_Email&&stu[i].number == m_Number&&stu[i].yuanxi == m_Xibie)		
		{   
			flag = 1;
			MessageBox("该学生信息已经添加!");
			break;
		}
	}
		if (flag==0)
	{
		int n=m_list.GetItemCount();

			m_list.InsertItem(n,"");
			m_list.SetItemText(n,0,m_Number);
			m_list.SetItemText(n,1,m_Name);
			m_list.SetItemText(n,2,m_Xibie);
			m_list.SetItemText(n,3,m_Email);
			m_list.SetItemText(n,4,m_Phone);
        stu.reserve(stu.size()+1);//增加容量以防止溢出

		Student s1;
		s1.name = m_Name;
		s1.number = m_Number;
		s1.yuanxi = m_Xibie;
		s1.email = m_Email;
		s1.phone = m_Phone;
        stu.push_back(s1);//往向量末尾加新信息
			MessageBox("添加成功!");
	m_Name="";
	m_Number="";
	m_Xibie="";
	m_Phone="";
	m_Email="";  //清空编辑框
	UpdateData(false);
	}
		
}

void CClassAddresslistDlg::OnBUTTONchange() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if (n==-1){
		MessageBox("请选中列表中的某一行!");
	}

	else if(m_Name==""&&m_Number==""&&m_Xibie==""&&
		m_Phone==""&&m_Email==""){
		MessageBox("修改信息有误,请重新修改!");
	}

	else if(
		stu[n].number == m_Number&&
        stu[n].name == m_Name&&
	    stu[n].yuanxi == m_Xibie&&
        stu[n].name == m_Email&&
	    stu[n].phone == m_Phone
		)
	{   MessageBox("未修改学生信息!");}

	else 
	{	
	stu[n].number = m_Number;
    stu[n].name = m_Name;
	stu[n].yuanxi =m_Xibie;
    stu[n].email = m_Email;
	stu[n].phone = m_Phone;

    m_list.SetItemText(n,0,m_Number); 
    m_list.SetItemText(n,1,m_Name);  
	m_list.SetItemText(n,2,m_Xibie);  
	m_list.SetItemText(n,3,m_Email); 
	m_list.SetItemText(n,4,m_Phone); 

    MessageBox("修改信息成功!");
	}
}

项目源码

需要源码的小伙伴请前往
微信公众号:海轰Pro
回复: 海轰
O(∩_∩)O哈哈~

发布了155 篇原创文章 · 获赞 110 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_44225182/article/details/103673173