?? 通訊簿2.0dlg.cpp
字號:
// 通訊簿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<WPARAM>(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<HCURSOR>(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<length;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<<length;
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<<string;
ar<<data->phone;
ar<<data->mobile;
ar<<data->department;
ar<<data->address;
ar<<data->other;
}
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);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -