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

📄 personbookdlg.cpp

📁 这是我的第一个小学期程序设计的程序
💻 CPP
字号:
// PersonBookDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PersonBook.h"
#include "PersonBookDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPersonBookDlg dialog

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

void CPersonBookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPersonBookDlg)
	DDX_Control(pDX, IDC_NUMBER, m_sNumber);
	DDX_Control(pDX, IDC_POSTCODE, m_sPostcode);
	DDX_Control(pDX, IDC_PHONE, m_sPhone);
	DDX_Control(pDX, IDC_ADDRESS, m_sAddress);
	DDX_Control(pDX, IDC_NAME, m_sName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPersonBookDlg, CDialog)
//{{AFX_MSG_MAP(CPersonBookDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_TOBEGIN, OnTobegin)
ON_BN_CLICKED(IDC_PERV, OnPerv)
ON_BN_CLICKED(IDC_NEXT, OnNext)
ON_BN_CLICKED(IDC_TOEND, OnToend)
ON_BN_CLICKED(IDC_NEW, OnNew)
ON_BN_CLICKED(IDC_INSERT, OnInsert)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_EN_CHANGE(IDC_NAME, OnChangeName)
	ON_EN_CHANGE(IDC_PHONE, OnChangePhone)
	ON_EN_CHANGE(IDC_ADDRESS, OnChangeAddress)
	ON_EN_CHANGE(IDC_POSTCODE, OnChangePostcode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPersonBookDlg message handlers

BOOL CPersonBookDlg::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
	
	// TODO: Add extra initialization here
	m_Book.InitData();
	if(!m_Book.LoadFile("addlist.txt"))
	{
		AfxMessageBox("读取addlist.txt文件失败!");
	}
	m_Book.ToBegin();
	if(m_Book.IsEmpty())
	{
		m_iPos = 0;
	}
	else
	{
		m_iPos = 1;
	}
	Show();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CPersonBookDlg::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 CPersonBookDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CPersonBookDlg::Show()
{
	CString sTemp;
	if(!m_Book.IsEmpty())
	{
		m_sName.ShowWindow(true);
		m_sAddress.ShowWindow(true);
		m_sPhone.ShowWindow(true);
		m_sPostcode.ShowWindow(true);

		sTemp.Format("%s", m_Book.GetCurPerson()->name);
		m_sName.SetWindowText(sTemp);
		sTemp.Format("%s", m_Book.GetCurPerson()->address);
		m_sAddress.SetWindowText(sTemp);
		sTemp.Format("%s", m_Book.GetCurPerson()->phone);
		m_sPhone.SetWindowText(sTemp);
		sTemp.Format("%s", m_Book.GetCurPerson()->postcode);
		m_sPostcode.SetWindowText(sTemp);
		sTemp.Format("(%d/%d)", m_iPos, m_Book.GetCount());
		m_sNumber.SetWindowText(sTemp);
	}
	else
	{
		m_sName.ShowWindow(false);
		m_sAddress.ShowWindow(false);
		m_sPhone.ShowWindow(false);
		m_sPostcode.ShowWindow(false);
		m_sNumber.SetWindowText("(0/0)");
	}
}

void CPersonBookDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	m_Book.FiniData();
}

void CPersonBookDlg::OnTobegin() 
{
	// TODO: Add your control notification handler code here
	if(m_Book.ToBegin())
	{
		m_iPos = 1;
	}
	Show();
}

void CPersonBookDlg::OnPerv() 
{
	// TODO: Add your control notification handler code here
	if(m_Book.Prev())
	{
		--m_iPos;
	}
	Show();
}

void CPersonBookDlg::OnNext() 
{
	// TODO: Add your control notification handler code here
	if(m_Book.Next())
	{
		++m_iPos;
	}
	Show();
}

void CPersonBookDlg::OnToend() 
{
	// TODO: Add your control notification handler code here
	if(m_Book.ToEnd())
	{
		m_iPos = m_Book.GetCount();
	}
	Show();
}

void CPersonBookDlg::OnNew() 
{
	// TODO: Add your control notification handler code here
	Card *pCard = NULL;
	pCard = (Card*)malloc(sizeof(Card));
	strcpy(pCard->person.name, "");
	strcpy(pCard->person.address, "");
	strcpy(pCard->person.phone, "");
	strcpy(pCard->person.postcode, "");
	if(pCard == NULL)
	{
		AfxMessageBox("内存不足");
	}
	if(m_Book.NewPerson(pCard))
	{
		m_iPos = m_Book.GetCount();
	}
	Show();
}

void CPersonBookDlg::OnInsert() 
{
	// TODO: Add your control notification handler code here
	Card *pCard = NULL;
	pCard = (Card*)malloc(sizeof(Card));
	strcpy(pCard->person.name, "");
	strcpy(pCard->person.address, "");
	strcpy(pCard->person.phone, "");
	strcpy(pCard->person.postcode, "");
	if(pCard == NULL)
	{
		AfxMessageBox("内存不足");
	}
	if(m_Book.InsertPerson(pCard))
	{
		++m_iPos;
	}
	Show();	
}

void CPersonBookDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	if(!m_Book.IsEmpty())
	{
		if(MessageBox("确定删除此人员?", "删除人员", MB_YESNO) == 6)
		{
			if(m_Book.IsEnd())
			{
				--m_iPos;
			}
			free(m_Book.DeletePerson());
			Show();
		}
	}
	
}

void CPersonBookDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	if(m_Book.SaveFile("addlist.txt"))
	{
		AfxMessageBox("保存文件成功!");
	}
	else
	{
		AfxMessageBox("保存文件失败!");
	}
}

void CPersonBookDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	if(MessageBox("是否退出人员管理系统", "退出系统", MB_YESNO) == 6)
	{
		exit(0);
	}
}

void CPersonBookDlg::OnChangeName() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	char sName[NAMESIZE + 1];
	m_sName.GetWindowText(sName, NAMESIZE + 1);
	if(!m_Book.IsEmpty())
	{
		strcpy(m_Book.GetCurPerson()->name, sName);
	}
}

void CPersonBookDlg::OnChangePhone() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	char sPhone[PHONESIZE + 1];
	m_sPhone.GetWindowText(sPhone, PHONESIZE + 1);
	if(!m_Book.IsEmpty())
	{
		strcpy(m_Book.GetCurPerson()->phone, sPhone);
	}	
}

void CPersonBookDlg::OnChangeAddress() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	char sAddress[ADDRESSSIZE + 1];
	m_sAddress.GetWindowText(sAddress, ADDRESSSIZE + 1);
	if(!m_Book.IsEmpty())
	{
		strcpy(m_Book.GetCurPerson()->address, sAddress);
	}	
}

void CPersonBookDlg::OnChangePostcode() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	char sPostcode[POSTCODESIZE + 1];
	m_sPostcode.GetWindowText(sPostcode, POSTCODESIZE + 1);
	if(!m_Book.IsEmpty())
	{
		strcpy(m_Book.GetCurPerson()->postcode, sPostcode);
	}	
}

⌨️ 快捷键说明

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