// 通讯簿2.0Dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "通讯簿2.0.h"
#include "通讯簿2.0Dlg.h"
#include ".\通讯簿2.0dlg.h"
#include "DialogDelete.h"
#include "DialogSetSecret.h"
#include "DialogChange.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMyDlg 对话框
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
, m_Name(_T(""))
, m_Phone(_T(""))
, m_Mobile(_T(""))
, m_Department(_T(""))
, m_Address(_T(""))
, m_Other(_T(""))
, length(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDITNAME, m_Name);
DDX_Text(pDX, IDC_EDITPHONE, m_Phone);
DDX_Text(pDX, IDC_EDITMOBILE, m_Mobile);
DDX_Text(pDX, IDC_EDITDEPARTMENT, m_Department);
DDX_Text(pDX, IDC_EDITADDRESS, m_Address);
DDX_Text(pDX, IDC_EDITOTHER, m_Other);
DDX_Control(pDX, IDC_LISTBOX, m_ListBox);
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTONADD, OnBnClickedButtonadd)
ON_LBN_SELCHANGE(IDC_LISTBOX, OnLbnSelchangeListbox)
ON_BN_CLICKED(IDC_BUTTONDELETE, OnBnClickedButtondelete)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTONBEGIN, OnBnClickedButtonbegin)
ON_BN_CLICKED(IDC_BUTTONSECRET, OnBnClickedButtonsecret)
ON_BN_CLICKED(IDC_BUTTONCHANGE, OnBnClickedButtonchange)
END_MESSAGE_MAP()
// CMyDlg 消息处理程序
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
CFile file("nindex",/*dlg.GetPathName(),*/CFile::modeRead);
CArchive arload( &file, CArchive::load) ;
LoadFile(arload);
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CMyDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CMyDlg::OnBnClickedButtonadd()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
if(m_Name.IsEmpty())
{
MessageBox("姓名不能为空!");
return;
}
m_Name.TrimLeft();
m_Name.TrimRight();
if((m_ListBox.FindString(-1,m_Name))!=LB_ERR)
{
MessageBox("列表框中已有相同姓名,不能添加!");
return;
}
int nIndex=m_ListBox.AddString(m_Name);
Information data;
data.phone = m_Phone;
data.mobile = m_Mobile;
data.department = m_Department;
data.address = m_Address;
data.other = m_Other;
m_ListBox.SetItemDataPtr(nIndex,new Information(data));
initialize();
UpdateData(false);
}
void CMyDlg::initialize(void)
{
m_Name = "";
m_Phone = "";
m_Mobile = "";
m_Department = "";
m_Address = "";
m_Other = "";
}
void CMyDlg::OnLbnSelchangeListbox()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex=m_ListBox.GetCurSel();
if(nIndex!=LB_ERR)
{
m_ListBox.GetText(nIndex,m_Name);
Information* data = (Information*)m_ListBox.GetItemDataPtr(nIndex);
m_Phone = data->phone;
m_Mobile = data->mobile;
m_Department = data->department;
m_Address = data->address;
m_Other = data->other;
UpdateData(FALSE);
}
}
void CMyDlg::OnBnClickedButtondelete()
{
// TODO: 在此添加控件通知处理程序代码
CDialogDelete dlg;
int nIndex=m_ListBox.GetCurSel();
if(dlg.DoModal() == IDOK)
{
if(nIndex!=LB_ERR)
{
Information* data = (Information*)m_ListBox.GetItemDataPtr(nIndex);
delete data;
m_ListBox.DeleteString(nIndex);
m_Name.Empty();
m_Phone.Empty();
m_Mobile.Empty();
m_Department.Empty();
m_Address.Empty();
m_Other.Empty();
UpdateData(FALSE);
}
else
MessageBox("当前没有选择项!");
}
}
void CMyDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: 在此处添加消息处理程序代码
CDialog::OnDestroy();
CFile file("nindex",CFile::modeWrite|CFile::modeCreate);
CArchive arsave( &file, CArchive::store) ;
SaveFile(arsave);
for(int nIndex=m_ListBox.GetCount()-1;nIndex>=0;nIndex--)
{
delete(Information*)m_ListBox.GetItemDataPtr(nIndex);
}
}
void CMyDlg::OnBnClickedButtonbegin()
{
// TODO: 在此添加控件通知处理程序代码
initialize();
UpdateData(FALSE);
}
void CMyDlg::LoadFile(CArchive& ar)
{
ar>>length;
for(int i=0;i {
ar>>m_Name;
ar>>m_Phone;
ar>>m_Mobile;
ar>>m_Department;
ar>>m_Address;
ar>>m_Other;
int nIndex=m_ListBox.AddString(m_Name);
Information data;
data.phone = m_Phone;
data.mobile = m_Mobile;
data.department = m_Department;
data.address = m_Address;
data.other = m_Other;
m_ListBox.SetItemDataPtr(nIndex,new Information(data));
}
return;
}
void CMyDlg::SaveFile(CArchive& ar)
{
length = m_ListBox.GetCount();
ar for(int nIndex=m_ListBox.GetCount()-1;nIndex>=0;nIndex--)
{
CString string;
Information* data = (Information*)m_ListBox.GetItemDataPtr(nIndex);
m_ListBox.GetText(nIndex,string);
ar ar ar ar ar ar }
return ;
}
void CMyDlg::OnBnClickedButtonsecret()
{
// TODO: 在此添加控件通知处理程序代码
CDialogSetSecret dlg;
dlg.DoModal();
}
void CMyDlg::OnBnClickedButtonchange()
{
// TODO: 在此添加控件通知处理程序代码
CDialogChange dlg;
int nIndex=m_ListBox.GetCurSel();
if(dlg.DoModal() == IDOK)
{
if(nIndex!=LB_ERR)
{
Information* data = (Information*)m_ListBox.GetItemDataPtr(nIndex);
UpdateData(TRUE);
data->phone = m_Phone;
data->mobile = m_Mobile;
data->department = m_Department;
data->address = m_Address;
data->other = m_Other;
AfxMessageBox("您已经成功地修改了该联系人的信息!");
}
else
MessageBox("当前没有选择项!");
}
UpdateData(FALSE);
}