📄 addressbookdlg.cpp
字号:
// AddressBookDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AddressBook.h"
#include "AddressBookDlg.h"
#include "io.h"
#include "about.h"
#define DATA_FILE _T("AddressBook.dat")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDlg dialog
CAddressBookDlg::CAddressBookDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAddressBookDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAddressBookDlg)
m_strAddress1 = _T("");
m_strAddress2 = _T("");
m_strCity = _T("");
m_strState = _T("");
m_strZip = _T("");
m_strHomePhone = _T("");
m_strWorkPhone = _T("");
m_strEmail = _T("");
m_strWebPage = _T("");
m_strNotes = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAddressBookDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAddressBookDlg)
DDX_Control(pDX, IDC_CBO_NAMES, m_cboNames);
DDX_Control(pDX, IDC_BTN_WEB_PAGE, m_btnWebPage);
DDX_Control(pDX, IDC_BTN_EMAIL, m_btnEmail);
DDX_Text(pDX, IDC_EDT_ADDR1, m_strAddress1);
DDX_Text(pDX, IDC_EDT_ADDR2, m_strAddress2);
DDX_Text(pDX, IDC_EDT_CITY, m_strCity);
DDX_Text(pDX, IDC_EDT_STATE, m_strState);
DDX_Text(pDX, IDC_EDT_ZIPCODE, m_strZip);
DDX_Text(pDX, IDC_EDT_HOME_PHONE, m_strHomePhone);
DDX_Text(pDX, IDC_EDT_WORK_PHONE, m_strWorkPhone);
DDX_Text(pDX, IDC_EDT_EMAIL, m_strEmail);
DDX_Text(pDX, IDC_EDT_WEB_PAGE, m_strWebPage);
DDX_Text(pDX, IDC_EDT_NOTES, m_strNotes);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAddressBookDlg, CDialog)
//{{AFX_MSG_MAP(CAddressBookDlg)
ON_WM_SYSCOMMAND()
ON_BN_CLICKED(ID_APP_ABOUT, OnAbout)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_CBN_SELCHANGE(IDC_CBO_NAMES, OnSelchangeCboNames)
ON_BN_CLICKED(IDC_BTN_WEB_PAGE, OnBtnWebPage)
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDlg message handlers
BOOL CAddressBookDlg::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
LoadRecords();
FillContactsComboBox();
return TRUE; // return TRUE unless you set the focus to a control
}
void CAddressBookDlg::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 CAddressBookDlg::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 CAddressBookDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAddressBookDlg::OnAbout()
{
CAboutDlg().DoModal();
}
void CAddressBookDlg::OnDestroy()
{
CDialog::OnDestroy();
POSITION pos;
CString strKey;
CContact* pContact;
pos = m_Contacts.GetStartPosition();
while(NULL != pos)
{
m_Contacts.GetNextAssoc(pos, strKey, pContact);
if (pContact)
{
m_Contacts.RemoveKey(strKey);
delete pContact;
}
}
ASSERT(NULL == m_Contacts.GetCount());
}
void CAddressBookDlg::OnSelchangeCboNames()
{
int iIndex = m_cboNames.GetCurSel();
if (CB_ERR != iIndex)
{
CContact* pContact =
(CContact*)m_cboNames.GetItemData(iIndex);
ASSERT(pContact);
if (pContact)
{
m_strAddress1 = pContact->m_strAddress1;
m_strAddress2 = pContact->m_strAddress2;
m_strCity = pContact->m_strCity;
m_strState = pContact->m_strState;
m_strZip = pContact->m_strZip;
m_strNotes = pContact->m_strNotes;
m_strHomePhone = pContact->m_strHomePhone;
m_strWorkPhone = pContact->m_strWorkPhone;
m_strEmail = pContact->m_strEmail;
m_strWebPage = pContact->m_strWebPage;
UpdateData(FALSE);
}
}
}
void CAddressBookDlg::OnBtnWebPage()
{
if (UpdateData(TRUE))
{
if (!m_strWebPage.IsEmpty())
{
if (32 >= (int)ShellExecute(NULL, "open",
m_strWebPage, NULL, NULL, SW_SHOWNORMAL))
{
AfxMessageBox("::ShellExecute failed "
"to open this link!");
}
}
else
{
AfxMessageBox("You must first specify a URL");
}
}
}
void CAddressBookDlg::Serialize(CArchive& ar)
{
m_Contacts.Serialize(ar);
if (ar.IsStoring())
{ // storing code
}
else
{ // loading code
}
}
BOOL CAddressBookDlg::CanSerialize()
{
return (0 == _access(DATA_FILE, 6));
}
void CAddressBookDlg::LoadRecords()
{
if (CanSerialize())
{
CFile file;
CFileException fe;
if (file.Open(DATA_FILE,
CFile::typeBinary | CFile::modeRead, &fe))
{
CArchive ar(&file, CArchive::load);
Serialize(ar);
}
}
}
void CAddressBookDlg::FillContactsComboBox()
{
POSITION pos;
CString strKey;
CContact* pContact;
int iIndex;
pos = m_Contacts.GetStartPosition();
while(NULL != pos)
{
m_Contacts.GetNextAssoc(pos, strKey, pContact);
if (pContact)
{
if (CB_ERR != (iIndex =
m_cboNames.AddString(pContact->m_strName)))
{
m_cboNames.SetItemData(iIndex, (DWORD)pContact);
}
}
}
}
void CAddressBookDlg::SaveRecords()
{
BOOL bCanSave = FALSE;
UINT nFlags = CFile::typeBinary | CFile::modeWrite;
// file doesn't exist, so create it
if (_access(DATA_FILE, 0))
{
nFlags |= CFile::modeCreate;
bCanSave = TRUE;
}
else
{
bCanSave = CanSerialize();
}
if (bCanSave)
{
CFile file;
CFileException fe;
// file exists with read & write permissions
if (file.Open(DATA_FILE, nFlags, &fe))
{
CArchive ar(&file, CArchive::store);
Serialize(ar);
}
}
}
void CAddressBookDlg::OnBtnSave()
{
if (UpdateData(TRUE))
{
CContact* pContact = NULL;
CString strName;
m_cboNames.GetWindowText(strName);
if (0 != strName.GetLength())
{
if (!m_Contacts.Lookup(strName, (CContact*)pContact))
{
pContact = new CContact;
int iIndex = m_cboNames.AddString(strName);
if (CB_ERR != iIndex)
{
m_cboNames.SetItemData(iIndex, (DWORD)pContact);
m_Contacts.SetAt(strName, pContact);
}
}
if (pContact)
{
pContact->m_strName = strName;
pContact->m_strAddress1 = m_strAddress1;
pContact->m_strAddress2 = m_strAddress2;
pContact->m_strCity = m_strCity;
pContact->m_strState = m_strState;
pContact->m_strZip = m_strZip;
pContact->m_strNotes = m_strNotes;
pContact->m_strHomePhone = m_strHomePhone;
pContact->m_strWorkPhone = m_strWorkPhone;
pContact->m_strEmail = m_strEmail;
pContact->m_strWebPage = m_strWebPage;
}
}
InitializeDialog();
}
}
void CAddressBookDlg::OnBtnClose()
{
SaveRecords();
OnOK();
}
void CAddressBookDlg::InitializeDialog()
{
m_cboNames.SetCurSel(CB_ERR);
m_strAddress1 = _T("");
m_strAddress2 = _T("");
m_strCity = _T("");
m_strState = _T("");
m_strZip = _T("");
m_strHomePhone = _T("");
m_strWorkPhone = _T("");
m_strEmail = _T("");
m_strWebPage = _T("");
m_strNotes = _T("");
UpdateData(FALSE);
m_cboNames.SetFocus();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -