一、电话薄的代码如下:
1、
// PhoneDlg.cpp : implementation file
//#include "stdafx.h"
#include "Phone.h"
#include "PhoneDlg.h"
#include "AddDlg.h"
#include "ModifyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CAboutDlg dialog used for App Aboutclass 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_MSGDECLARE_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()/
// CPhoneDlg dialogCPhoneDlg::CPhoneDlg(CWnd* pParent /*=NULL*/): CDialog(CPhoneDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CPhoneDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CPhoneDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CPhoneDlg)DDX_Control(pDX, IDC_LIST, m_list);DDX_Control(pDX, IDC_TREE, m_treePhone);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CPhoneDlg, CDialog)//{{AFX_MSG_MAP(CPhoneDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_ADD, OnAdd)ON_BN_CLICKED(IDC_DEL, OnDel)ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)ON_COMMAND(ID_MODIFY, OnModify)ON_NOTIFY(NM_RCLICK, IDC_LIST, OnRclickList)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CPhoneDlg message handlersBOOL CPhoneDlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization hereHICON hIcon[10];m_imageList.Create(16,16,ILC_COLOR,10,10);hIcon[0] = AfxGetApp()->LoadIcon(IDI_ICON_gege);hIcon[1] = AfxGetApp()->LoadIcon(IDI_ICON_didi);hIcon[2] = AfxGetApp()->LoadIcon(IDI_ICON_zhangsan);hIcon[3] = AfxGetApp()->LoadIcon(IDI_ICON_lisi);hIcon[4] = AfxGetApp()->LoadIcon(IDI_ICON_zhangtongxue);hIcon[5] = AfxGetApp()->LoadIcon(IDI_ICON_litongxue);hIcon[6] = AfxGetApp()->LoadIcon(IDI_ICON_dianhuabu);hIcon[7] = AfxGetApp()->LoadIcon(IDI_ICON_jiating);hIcon[8] = AfxGetApp()->LoadIcon(IDI_ICON_pengyou);hIcon[9] = AfxGetApp()->LoadIcon(IDI_ICON_tongxue);for(int n=0; n<10; n++)m_imageList.Add(hIcon[n]);m_treePhone.SetImageList(&m_imageList, TVSIL_NORMAL);HTREEITEM hRoot = m_treePhone.InsertItem("电话薄",6,6,TVI_ROOT);HTREEITEM hFamily = m_treePhone.InsertItem("家庭",7,7,hRoot);HTREEITEM hFriend = m_treePhone.InsertItem("朋友",8,8,hRoot);HTREEITEM hClassmate = m_treePhone.InsertItem("同学",9,9,hRoot);HTREEITEM h10 = m_treePhone.InsertItem("哥哥",0,0,hFamily);HTREEITEM h11 = m_treePhone.InsertItem("弟弟",1,1,hFamily);HTREEITEM h20 = m_treePhone.InsertItem("张三",2,2,hFriend);HTREEITEM h21 = m_treePhone.InsertItem("李四",3,3,hFriend);HTREEITEM h30 = m_treePhone.InsertItem("张同学",4,4,hClassmate);HTREEITEM h31 = m_treePhone.InsertItem("李同学",5,5,hClassmate);CString *pString = new CString("10101010101");m_treePhone.SetItemData(h10,(DWORD)pString);pString = new CString("11111111111");m_treePhone.SetItemData(h11,(DWORD)pString);pString = new CString("20202020202");m_treePhone.SetItemData(h20,(DWORD)pString);pString = new CString("21212121212");m_treePhone.SetItemData(h21,(DWORD)pString);pString = new CString("30303030303");m_treePhone.SetItemData(h30,(DWORD)pString);pString = new CString("31313131313");m_treePhone.SetItemData(h31,(DWORD)pString);m_list.InsertColumn(0,"姓名",LVCFMT_LEFT,100);m_list.InsertColumn(1,"电话号码",LVCFMT_LEFT,120);m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);return TRUE; // return TRUE unless you set the focus to a control
}void CPhoneDlg::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 CPhoneDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 CPhoneDlg::OnQueryDragIcon()
{return (HCURSOR) m_hIcon;
}void CPhoneDlg::OnAdd()
{// TODO: Add your control notification handler code hereHTREEITEM selItem = m_treePhone.GetSelectedItem();//获得树中选中节点if(selItem == NULL) //如果没有选中节点,返回return;if(selItem == m_treePhone.GetRootItem()) //如果选中了根节点,返回return;DWORD m = m_treePhone.GetItemData(selItem); //获得选中节点关联数据if(m == NULL) //如果为空,是分组节点{CAddDlg dlg;if(dlg.DoModal() == IDOK) //显示添加人员对话框,并单击确定按钮{//在选中节点下添加新节点HTREEITEM item = m_treePhone.InsertItem(dlg.m_strName,2,2,selItem);CString *pString = new CString(dlg.m_strNumber);m_treePhone.SetItemData(item,(DWORD)pString); //设置关联数据refreshList(selItem);}}}void CPhoneDlg::OnDel()
{// TODO: Add your control notification handler code hereHTREEITEM selItem = m_treePhone.GetSelectedItem(); //获得树中选中节点if(selItem == NULL) //如果没有选中节点,返回return;if(selItem == m_treePhone.GetRootItem()) //如果选中了根节点,返回return;DWORD m = m_treePhone.GetItemData(selItem); //获得选中节点关联数据if(m != NULL) //如果不为空,是人员节点{if(AfxMessageBox("确定要删除" + m_treePhone.GetItemText(selItem) + "?",MB_OKCANCEL) == IDOK){HTREEITEM parent = m_treePhone.GetParentItem(selItem);CString *pString = (CString*)m; //删除关联数据delete pString;m_treePhone.DeleteItem(selItem); //删除节点refreshList(parent);}}}void CPhoneDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult)
{NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;// TODO: Add your control notification handler code hereHTREEITEM selItem = m_treePhone.GetSelectedItem(); //获得树中选中节点if(selItem == m_treePhone.GetRootItem()) //如果没有选中节点,返回return;DWORD m = m_treePhone.GetItemData(selItem); //获得选中节点关联数据if(m == NULL) //如果为空,是分组节点{refreshList(selItem); //刷新列表视图中的数据}*pResult = 0;
}void CPhoneDlg::refreshList(HTREEITEM parent)
{ m_list.DeleteAllItems(); //删除原有数据int index = 0; //遍历获得该分组节点下的所有子节点HTREEITEM child = m_treePhone.GetChildItem(parent); while (child != NULL) {DWORD n = m_treePhone.GetItemData(child);CString *pString = (CString*)n; //得到人员节点中的关联电话号码//在列表视图中添加一项m_list.InsertItem(index,m_treePhone.GetItemText(child));m_list.SetItemText(index,1,*pString); //在列表视图中设置一子项index++;child = m_treePhone.GetNextSiblingItem(child); //得到下一个兄弟节点}}void CPhoneDlg::OnModify()
{// TODO: Add your command handler code here//查找列表视图中被选中项的标号int i;for(i=0; i<m_list.GetItemCount(); i++)if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )break;//如果标号大于列表视图中项的个数,返回,即在没有数据的项上单击右键if(i >= m_list.GetItemCount())return;//得到被选中项的数据CString name = m_list.GetItemText(i,0);CString number = m_list.GetItemText(i,1);//声明对话框对象,并设置数据CModifyDlg dlg;dlg.SetValue(name,number);
//如果单击了“确定”按钮if(dlg.DoModal() == IDOK){CString newName = dlg.m_strName;CString newNumber = dlg.m_strNumber; //得到新的姓名和电话号码//查找该人员在树中是哪个节点HTREEITEM child = m_treePhone.GetChildItem(m_treePhone.GetSelectedItem());while (child) {if(name == m_treePhone.GetItemText(child)){//更新修改后的值m_treePhone.SetItemText(child,newName);CString *pString = (CString*)m_treePhone.GetItemData(child);*pString = newNumber;break;}child = m_treePhone.GetNextSiblingItem(child);}//在列表视图中更新m_list.SetItemText(i,0,newName);m_list.SetItemText(i,1,newNumber);UpdateData(FALSE); //更新界面显示}}void CPhoneDlg::OnRclickList(NMHDR* pNMHDR, LRESULT* pResult)
{// TODO: Add your control notification handler code hereCMenu m_menu;m_menu.LoadMenu(IDR_MENU1);//加载整条菜单 CMenu*pMenu=m_menu.GetSubMenu(0);//获得第一个菜单 POINT p; GetCursorPos(&p);//获得鼠标位置 pMenu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, p.x, p.y,this); pMenu->DestroyMenu();//销毁菜单*pResult = 0;
}
// AddDlg.cpp : implementation file
//#include "stdafx.h"
#include "Phone.h"
#include "AddDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CAddDlg dialogCAddDlg::CAddDlg(CWnd* pParent /*=NULL*/): CDialog(CAddDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CAddDlg)m_strName = _T("");m_strNumber = _T("");//}}AFX_DATA_INIT
}void CAddDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAddDlg)DDX_Text(pDX, IDC_NAME, m_strName);DDX_Text(pDX, IDC_NUMBER, m_strNumber);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAddDlg, CDialog)//{{AFX_MSG_MAP(CAddDlg)ON_BN_CLICKED(IDC_OK, OnOk)ON_BN_CLICKED(IDC_CANCEL, OnCancel)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CAddDlg message handlersvoid CAddDlg::OnOk()
{// TODO: Add your control notification handler code hereUpdateData(TRUE);if(m_strName == "" || m_strNumber == ""){AfxMessageBox("信息不完整,请重新输入!");return;}CDialog::OnOK();}void CAddDlg::OnCancel()
{// TODO: Add your control notification handler code hereCDialog::OnCancel();}
// ModifyDlg.cpp : implementation file
//#include "stdafx.h"
#include "Phone.h"
#include "ModifyDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CModifyDlg dialogCModifyDlg::CModifyDlg(CWnd* pParent /*=NULL*/): CDialog(CModifyDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CModifyDlg)m_strName = _T("");m_strNumber = _T("");//}}AFX_DATA_INIT
}void CModifyDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CModifyDlg)DDX_Text(pDX, IDC_NAME, m_strName);DDX_Text(pDX, IDC_NUMBER, m_strNumber);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CModifyDlg, CDialog)//{{AFX_MSG_MAP(CModifyDlg)ON_BN_CLICKED(IDC_OK, OnOk)ON_BN_CLICKED(IDC_CANCEL, OnCancel)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CModifyDlg message handlersvoid CModifyDlg::SetValue(CString name, CString number)
{m_strName = name;m_strNumber = number;}void CModifyDlg::OnOk()
{// TODO: Add your control notification handler code hereUpdateData(TRUE);if(m_strName == "" || m_strNumber == ""){AfxMessageBox("信息不完整,请重新输入!");return;}CDialog::OnOK();}void CModifyDlg::OnCancel()
{// TODO: Add your control notification handler code hereCDialog::OnCancel();
}
// PhoneDlg.h : header file
//#if !defined(AFX_PHONEDLG_H__0B6F97D2_4A74_4BA2_8D17_A10C447C734C__INCLUDED_)
#define AFX_PHONEDLG_H__0B6F97D2_4A74_4BA2_8D17_A10C447C734C__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/
// CPhoneDlg dialogclass CPhoneDlg : public CDialog
{
// Construction
public:void refreshList(HTREEITEM parent);CImageList m_imageList;CPhoneDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CPhoneDlg)enum { IDD = IDD_PHONE_DIALOG };CListCtrl m_list;CTreeCtrl m_treePhone;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CPhoneDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CPhoneDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnAdd();afx_msg void OnDel();afx_msg void OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult);afx_msg void OnModify();afx_msg void OnRclickList(NMHDR* pNMHDR, LRESULT* pResult);//}}AFX_MSGDECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_PHONEDLG_H__0B6F97D2_4A74_4BA2_8D17_A10C447C734C__INCLUDED_)
#if !defined(AFX_ADDDLG_H__6FC879E4_4440_4B76_AB1B_90053CBE1213__INCLUDED_)
#define AFX_ADDDLG_H__6FC879E4_4440_4B76_AB1B_90053CBE1213__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// AddDlg.h : header file
///
// CAddDlg dialogclass CAddDlg : public CDialog
{
// Construction
public:CAddDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CAddDlg)enum { IDD = IDD_ADD_DIALOG };CString m_strName;CString m_strNumber;//}}AFX_DATA// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAddDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:// Generated message map functions//{{AFX_MSG(CAddDlg)afx_msg void OnOk();afx_msg void OnCancel();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_ADDDLG_H__6FC879E4_4440_4B76_AB1B_90053CBE1213__INCLUDED_)
#if !defined(AFX_MODIFYDLG_H__8336A465_F3A0_4F14_9894_28B29B7BFC51__INCLUDED_)
#define AFX_MODIFYDLG_H__8336A465_F3A0_4F14_9894_28B29B7BFC51__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ModifyDlg.h : header file
///
// CModifyDlg dialogclass CModifyDlg : public CDialog
{
// Construction
public:void SetValue(CString name, CString number);CModifyDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CModifyDlg)enum { IDD = IDD_MODIFY_DIALOG };CString m_strName;CString m_strNumber;//}}AFX_DATA// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CModifyDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:// Generated message map functions//{{AFX_MSG(CModifyDlg)afx_msg void OnOk();afx_msg void OnCancel();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MODIFYDLG_H__8336A465_F3A0_4F14_9894_28B29B7BFC51__INCLUDED_)