www.pudn.com > TongXuLv_Manage.rar > addr_bookDlg.cpp


// addr_bookDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "addr_book.h" 
#include "addr_bookDlg.h" 
#include "APeople.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAddr_bookDlg dialog 
 
CAddr_bookDlg::CAddr_bookDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CAddr_bookDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CAddr_bookDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CAddr_bookDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAddr_bookDlg) 
	DDX_Control(pDX, IDC_LIST2, m_list); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAddr_bookDlg, CDialog) 
	//{{AFX_MSG_MAP(CAddr_bookDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON6, OnButtonClose) 
	ON_BN_CLICKED(IDC_BUTTON8, OnButtonOpen) 
	ON_BN_CLICKED(IDC_BUTTON9, OnButtonSave) 
	ON_BN_CLICKED(IDC_BUTTON7, OnButtonSaveAs) 
	ON_BN_CLICKED(IDC_BUTTON1, OnButtonAdd) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButtonModify) 
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonDelete) 
	ON_BN_CLICKED(IDC_BUTTON4, OnButtonSort) 
	ON_BN_CLICKED(IDC_BUTTON5, OnButtonSearch) 
	ON_NOTIFY(NM_CLICK, IDC_LIST2, OnClickListMain) 
	ON_BN_CLICKED(IDC_BUTTON10, OnButtonCloseDB) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAddr_bookDlg message handlers 
 
BOOL CAddr_bookDlg::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 
	// 初始化ImageList 
	m_imagelist.Create(16,16,ILC_MASK,0,4); 
	HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1); 
	m_imagelist.Add(hIcon); 
 
	// 初始化 ListCtrl 
	m_list.SetImageList(&m_imagelist,LVSIL_SMALL); 
	m_list.InsertColumn(0,"姓名"); 
	m_list.InsertColumn(1,"电话"); 
	m_list.InsertColumn(2,"手机"); 
	m_list.InsertColumn(3,"OICQ"); 
	m_list.InsertColumn(4,"Email"); 
	m_list.InsertColumn(5,"地址"); 
	m_list.SetColumnWidth(0,80); 
	m_list.SetColumnWidth(1,80); 
	m_list.SetColumnWidth(2,80); 
	m_list.SetColumnWidth(3,80); 
	m_list.SetColumnWidth(4,160); 
	m_list.SetColumnWidth(5,160); 
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); 
 
	CComboBox* pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); 
	pComboBox->SetCurSel(0); 
	pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO2); 
	pComboBox->SetCurSel(0); 
	pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO3); 
	pComboBox->SetCurSel(0); 
 
 
	ShowData(); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CAddr_bookDlg::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 CAddr_bookDlg::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 CAddr_bookDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CAddr_bookDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
	 
	//CDialog::OnOK(); 
} 
 
void CAddr_bookDlg::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	 
	//CDialog::OnCancel(); 
} 
 
void CAddr_bookDlg::OnButtonClose()  
{ 
	// TODO: Add your control notification handler code here 
	CDialog::OnCancel(); 
} 
 
void CAddr_bookDlg::OnButtonOpen()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog dlg(TRUE); 
	if(dlg.DoModal()==IDOK) 
	{ 
		m_addrbook.OpenFile(dlg.GetFileName()); 
		ShowData(); 
	} 
} 
 
void CAddr_bookDlg::OnButtonSave()  
{ 
	// TODO: Add your control notification handler code here 
	if(!m_addrbook.Save()) 
	{ 
		AfxMessageBox("保存失败"); 
		return; 
	} 
	AfxMessageBox("保存完成"); 
} 
 
void CAddr_bookDlg::OnButtonSaveAs()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog dlg(FALSE); 
	if(dlg.DoModal()==IDOK) 
	{ 
		m_addrbook.SaveToFile(dlg.GetFileName()); 
	} 
} 
 
void CAddr_bookDlg::OnButtonAdd()  
{ 
	// TODO: Add your control notification handler code here 
	APeople aPeople; 
	CString s,s1; 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_NAME); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.name)) 
	{ 
		s1.Format("姓名最多不超过%d个字符!",aPeople.name); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("姓名最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.name,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.tel)) 
	{ 
		s1.Format("电话最多不超过%d个字符!",aPeople.tel); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("电话最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.tel,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.mobile)) 
	{ 
		s1.Format("手机最多不超过%d个字符!",aPeople.mobile); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("手机最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.mobile,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_OICQ); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.oicq)) 
	{ 
		s1.Format("OICQ最多不超过%d个字符!",aPeople.oicq); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("OICQ最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.oicq,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_EMAIL); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.email)) 
	{ 
		s1.Format("Email最多不超过%d个字符!",aPeople.email); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("Email最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.email,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ADDR); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.addr)) 
	{ 
		s1.Format("地址最多不超过%d个字符!",aPeople.addr); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("地址最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.addr,s,s.GetLength()); 
	if(!m_addrbook.Add(&aPeople)) 
	{ 
		AfxMessageBox("添加失败!"); 
		return; 
	} 
	ShowData(); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_NAME); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_OICQ); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_EMAIL); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ADDR); 
	pEdit->SetWindowText(""); 
} 
 
void CAddr_bookDlg::OnButtonModify()  
{ 
	// TODO: Add your control notification handler code here 
	if(m_list.GetSelectedCount()<1) 
		return; 
	int n=m_list.GetSelectionMark(); 
	DWORD i=m_list.GetItemData(n); 
	APeople aPeople; 
	POSITION pos=m_addrbook.GetPositionWithIndex(i); 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_NAME); 
	CString s,s1; 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.name)) 
	{ 
		s1.Format("姓名最多不超过%d个字符!",aPeople.name); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("姓名最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.name,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.tel)) 
	{ 
		s1.Format("电话最多不超过%d个字符!",aPeople.tel); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("电话最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.tel,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.mobile)) 
	{ 
		s1.Format("手机最多不超过%d个字符!",aPeople.mobile); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("手机最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.mobile,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_OICQ); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.oicq)) 
	{ 
		s1.Format("OICQ最多不超过%d个字符!",aPeople.oicq); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("OICQ最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.oicq,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_EMAIL); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.email)) 
	{ 
		s1.Format("Email最多不超过%d个字符!",aPeople.email); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("Email最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.email,s,s.GetLength()); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ADDR); 
	pEdit->GetWindowText(s); 
	if(s.GetLength()>sizeof(aPeople.addr)) 
	{ 
		s1.Format("地址最多不超过%d个字符!",aPeople.addr); 
		AfxMessageBox(s1); 
		return; 
	} 
	if(s.GetLength()<2) 
	{ 
		AfxMessageBox("地址最小不小于2个字符!"); 
		return; 
	} 
	memcpy(aPeople.addr,s,s.GetLength()); 
	APeople *pPeople=(APeople*)m_addrbook.pList.GetAt(pos); 
	memcpy(pPeople,&aPeople,sizeof(APeople)); 
	ShowData(); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_NAME); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_OICQ); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_EMAIL); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ADDR); 
	pEdit->SetWindowText(""); 
} 
 
void CAddr_bookDlg::OnButtonDelete()  
{ 
	// TODO: Add your control notification handler code here 
	if(m_list.GetSelectedCount()<1) 
		return; 
	int n=m_list.GetSelectionMark(); 
	DWORD i=m_list.GetItemData(n); 
	m_addrbook.Delete(m_addrbook.GetPositionWithIndex(i)); 
	ShowData(); 
} 
 
void CAddr_bookDlg::OnButtonSort()  
{ 
	// TODO: Add your control notification handler code here 
	CComboBox* pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO2); 
	CString s; 
	pComboBox->GetLBText(pComboBox->GetCurSel(),s); 
	pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO3); 
	if(!m_addrbook.Sort(s,pComboBox->GetCurSel())) 
	{ 
		AfxMessageBox("排序失败"); 
		return; 
	} 
	ShowData(); 
} 
 
void CAddr_bookDlg::OnButtonSearch()  
{ 
	// TODO: Add your control notification handler code here 
	CComboBox* pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); 
	CString s,s1; 
	pComboBox->GetLBText(pComboBox->GetCurSel(),s); 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT7); 
	pEdit->GetWindowText(s1); 
	if(!m_addrbook.Search(s,s1)) 
	{ 
		AfxMessageBox("查询失败"); 
		return; 
	} 
	ShowData(); 
} 
 
void CAddr_bookDlg::ShowData() 
{ 
	m_list.DeleteAllItems(); 
	POSITION pos=m_addrbook.pList.GetHeadPosition(); 
	APeople *pPeople; 
	int i=0; 
	CString s; 
	while(pos) 
	{ 
		pPeople=(APeople*)m_addrbook.pList.GetNext(pos); 
		s=pPeople->name; 
		m_list.InsertItem(i,s,0); 
		s=pPeople->tel; 
		m_list.SetItemText(i,1,s); 
		s=pPeople->mobile; 
		m_list.SetItemText(i,2,s); 
		s=pPeople->oicq; 
		m_list.SetItemText(i,3,s); 
		s=pPeople->email; 
		m_list.SetItemText(i,4,s); 
		s=pPeople->addr; 
		m_list.SetItemText(i,5,s); 
		m_list.SetItemData(i,i); 
		i++; 
	} 
} 
 
void CAddr_bookDlg::OnClickListMain(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	// TODO: Add your control notification handler code here 
	if(m_list.GetSelectedCount()<1) 
		return; 
	int n=m_list.GetSelectionMark(); 
	DWORD i=m_list.GetItemData(n); 
	POSITION pos=m_addrbook.pList.GetHeadPosition(); 
	for(DWORD j=0;jSetWindowText(pPeople->name); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL); 
	pEdit->SetWindowText(pPeople->tel); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE); 
	pEdit->SetWindowText(pPeople->mobile); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_OICQ); 
	pEdit->SetWindowText(pPeople->oicq); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_EMAIL); 
	pEdit->SetWindowText(pPeople->email); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ADDR); 
	pEdit->SetWindowText(pPeople->addr); 
	*pResult = 0; 
} 
 
void CAddr_bookDlg::OnButtonCloseDB()  
{ 
	// TODO: Add your control notification handler code here 
	m_addrbook.CloseFile(); 
	ShowData(); 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_NAME); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_OICQ); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_EMAIL); 
	pEdit->SetWindowText(""); 
	pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ADDR); 
	pEdit->SetWindowText(""); 
}