⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phonedlg.cpp

📁 通讯录
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// phoneDlg.cpp : implementation file
//

#include "stdafx.h"
#include "phone.h"
#include "phoneDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_name = _T("");
	m_note = _T("");
	m_sex = _T("");
	m_mail = _T("");
	m_age = 0;
	m_phone = _T("");
	m_QueryText = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_LIST, m_clc);
	DDX_Text(pDX, IDC_NAME, m_name);
	DDX_Text(pDX, IDC_NOTE, m_note);
	DDX_Text(pDX, IDC_SEX, m_sex);
	DDX_Text(pDX, IDC_MAIL, m_mail);
	DDX_Text(pDX, IDC_AGE, m_age);
	DDX_Text(pDX, IDC_PHONE, m_phone);
	DDX_Text(pDX, IDC_QUERYTEXT, m_QueryText);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CANCELMODE()
	ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
	ON_BN_CLICKED(IDC_FIRST, OnFirst)
	ON_BN_CLICKED(IDC_LAST, OnLast)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_BN_CLICKED(IDC_PRE, OnPre)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_MAN, OnMan)
	ON_BN_CLICKED(IDC_WOMAN, OnWoman)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_QUERY, OnQuery)
	ON_BN_CLICKED(IDC_QQUERY, OnQquery)
	ON_BN_CLICKED(IDC_QNAME, OnQname)
	ON_BN_CLICKED(IDC_QAGE, OnQage)
	ON_BN_CLICKED(IDC_QMAIL, OnQmail)
	ON_BN_CLICKED(IDC_QPHONE, OnQphone)
	ON_BN_CLICKED(IDC_QNOTE, OnQnote)
	ON_BN_CLICKED(IDC_QSEX, OnQsex)
	ON_BN_CLICKED(IDC_SHOWALL, OnShowall)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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

   //这一段的作用是判断当前目录下是否有friends.txt文件,如果没有则创建
   CFile file;
   file.Open("friends.txt", CFile::modeCreate |CFile::modeReadWrite | CFile::modeNoTruncate);
   file.Close();

   //记录当前窗口的大小到tempRect;
   //然后设置当前窗口大小为rectSeparator的大小
   GetWindowRect(&tempRect);
   CRect rectSeparator;
   GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);
   SetWindowPos(NULL,300,100,tempRect.Width(),rectSeparator.bottom,SWP_NOZORDER);

   //设置一些变量的状态
   EditState=false;	//是否在编辑状态
   QueryState=false; //是否在查询状态
   
   //设置CListCtrl的表头信息
   m_clc.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_ONECLICKACTIVATE);
   m_clc.InsertColumn(0,"姓名",LVCFMT_LEFT,60);
   m_clc.InsertColumn(1,"性别",LVCFMT_LEFT,40);
   m_clc.InsertColumn(2,"年龄",LVCFMT_LEFT,40);
   m_clc.InsertColumn(3,"邮箱",LVCFMT_LEFT,100);
   m_clc.InsertColumn(4,"电话",LVCFMT_LEFT,100);
   m_clc.InsertColumn(5,"备注",LVCFMT_LEFT,63);
   
   //设置CComboBox的选项
   ((CComboBox*)GetDlgItem(IDC_SEXLIST))->AddString("男");
   ((CComboBox*)GetDlgItem(IDC_SEXLIST))->AddString("女");

   //刷新CListCtrl的内容
   CMyDlg::ListRefresh();
   
   //设置时间显示
   SetTimer(1,1000,NULL);

   return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}



IMPLEMENT_DYNCREATE(CFriend,CObject)

CFriend::CFriend()
{
  name="无";
  sex="男";
  age=0;
  email="www.noemail@no.com";
  phone="未知";
  note="";
}


CFriend::CFriend(CString name,CString sex,int age,CString email,CString phone,CString note)
{
  	this->name=name;
	this->sex=sex;
	this->age=age;
	this->email=email;
	this->phone=phone;
	this->note=note;
}

//DEL void CMyDlg::ReadFromFiles()
//DEL {
//DEL 
//DEL }

 void CMyDlg::ReadFile()
 {
 	CStdioFile file("friends.txt",CFile::modeRead | CFile::typeBinary);
 	bool bok=true;
 	CString str;
	CFriend *frd;
	if(!m_frdList.IsEmpty())     //清空
	{
	  CFriend *frd;
	  POSITION pos=m_frdList.GetHeadPosition();
      while(pos!=NULL)
	  {
	   frd=m_frdList.GetAt(pos);
	   delete frd;
	   m_frdList.GetNext(pos);
	  }
	  m_frdList.RemoveAll();
	}
 	while(bok==true)
 	{
 	   bok=(bool)file.ReadString(str);
 	   str.TrimLeft("\r");
 	   str.TrimRight("\n");
 	   if (str=="")
		   break;
	   int temp=0;
	   int temp2=0;	  
	   {
		  //读取姓名
	      temp2=str.Find(" ",temp);
		  name=str.Mid(temp,temp2-temp);
		//  MessageBox(name);
          //读取性别
		  temp=temp2+1;
		  temp2=str.Find(" ",temp);
          sex=str.Mid(temp,temp2-temp);
		//  MessageBox(sex);
          //读取年龄
		  temp=temp2+1;
		  temp2=str.Find(" ",temp);
          age=atoi(str.Mid(temp,temp2-temp).GetBuffer(0));
          //读取邮箱
		  temp=temp2+1;
		  temp2=str.Find(" ",temp);
		  email=str.Mid(temp,temp2-temp);
		//  MessageBox(email);
          //读取电话
		  temp=temp2+1;
		  temp2=str.Find(" ",temp);
		  phone=str.Mid(temp,temp2-temp);
        //  MessageBox(phone);
          //读取备注
		  temp=temp2+1;	 
		  note=str.Right(str.GetLength()-temp);
		 // MessageBox(note);
	   }

      frd=new CFriend(name,sex,age,email,phone,note);
	  m_frdList.AddTail(frd);
	  curPos=m_frdList.GetHeadPosition();
      
 	}

 	file.Close();
 	
 }

void CMyDlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
    EditState=false;
	GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);
    //当点击图表的时候,会在下方显示数据

	CString temp;
	temp.Format("%d",m_clc.GetFirstSelectedItemPosition()-1);
	CMyDlg::GetPos();
    
    EditState=false;

    m_name=m_clc.GetItemText(atoi(temp),0);
    m_sex=m_clc.GetItemText(atoi(temp),1);
    m_age=atoi(m_clc.GetItemText(atoi(temp),2));
	m_mail=m_clc.GetItemText(atoi(temp),3);
	m_phone=m_clc.GetItemText(atoi(temp),4);
	m_note=m_clc.GetItemText(atoi(temp),5);
	UpdateData(false);
	*pResult = 0;
}

void CMyDlg::OnFirst() 
{
	// TODO: Add your control notification handler code here
	EditState=false;
	GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);

	if(m_clc.GetItemCount()==0)
		MessageBox("当前没有记录!");
	else
    {
	m_clc.SetItemState(0,LVNI_SELECTED,LVNI_SELECTED);
	m_clc.EnsureVisible(1,false);
	m_clc.SetFocus();

	m_name=m_clc.GetItemText(0,0);
    m_sex=m_clc.GetItemText(0,1);
    m_age=atoi(m_clc.GetItemText(0,2));
	m_mail=m_clc.GetItemText(0,3);
	m_phone=m_clc.GetItemText(0,4);
	m_note=m_clc.GetItemText(0,5);
	
	CMyDlg::GetPos();
	UpdateData(false);
	
    }

}

void CMyDlg::OnLast() 
{
	// TODO: Add your control notification handler code here
	EditState=false;

	GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);

	int i=m_clc.GetItemCount();
	if(i==0)
		MessageBox("当前没有记录!");
	else
    {
	m_clc.SetItemState(i-1,LVNI_SELECTED,LVNI_SELECTED);
	m_clc.EnsureVisible(1,false);
	m_clc.SetFocus();

	m_name=m_clc.GetItemText(i-1,0);
    m_sex=m_clc.GetItemText(i-1,1);
    m_age=atoi(m_clc.GetItemText(i-1,2));
	m_mail=m_clc.GetItemText(i-1,3);
	m_phone=m_clc.GetItemText(i-1,4);
	m_note=m_clc.GetItemText(i-1,5);

	CMyDlg::GetPos();
	UpdateData(false);
    }
}

void CMyDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	//OnClose();
	SendMessage(WM_CLOSE);
}

void CMyDlg::OnNext() 
{
// TODO: Add your control notification handler code here
EditState=false;

GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);


POSITION pos = m_clc.GetFirstSelectedItemPosition();
CString temp;
	temp.Format("%d",m_clc.GetFirstSelectedItemPosition());
if (pos == NULL)
    this->OnFirst(); //想了半天怎么获取窗口句柄。。。
else if(atoi(temp)==m_clc.GetItemCount())
    this->OnFirst();
else
	{	
	int i=atoi(temp);
	m_clc.SetItemState(i,LVNI_SELECTED,LVNI_SELECTED);
	m_clc.EnsureVisible(1,false);
	m_clc.SetFocus();

	m_name=m_clc.GetItemText(i,0);
    m_sex=m_clc.GetItemText(i,1);
    m_age=atoi(m_clc.GetItemText(i,2));
	m_mail=m_clc.GetItemText(i,3);
	m_phone=m_clc.GetItemText(i,4);
	m_note=m_clc.GetItemText(i,5);

	CMyDlg::GetPos();
	UpdateData(false);
	}

}

void CMyDlg::OnPre() 
{
	// TODO: Add your control notification handler code here
EditState=false;

GetDlgItem(IDC_MAN)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_WOMAN)->ShowWindow(SW_HIDE);

POSITION pos = m_clc.GetFirstSelectedItemPosition();
CString temp;
temp.Format("%d",m_clc.GetFirstSelectedItemPosition());
if (pos == NULL)
    this->OnLast(); //想了半天怎么获取窗口句柄。。。
else if(atoi(temp)==1)
    this->OnLast();
else
	{	
	int i=atoi(temp)-2;
	m_clc.SetItemState(i,LVNI_SELECTED,LVNI_SELECTED);
	m_clc.EnsureVisible(1,false);
	m_clc.SetFocus();

	m_name=m_clc.GetItemText(i,0);
    m_sex=m_clc.GetItemText(i,1);
    m_age=atoi(m_clc.GetItemText(i,2));
	m_mail=m_clc.GetItemText(i,3);
	m_phone=m_clc.GetItemText(i,4);
	m_note=m_clc.GetItemText(i,5);

	CMyDlg::GetPos();
	UpdateData(false);
	}

}

void CMyDlg::Hello()
{
	CString hello[5]={
	"太惨了点吧!",
	"人生得一知己足矣,女的吧?",
	"三人行,必有我师焉",
	"可以凑桌麻将了",
	"多多益善!",
	};
	int i=m_clc.GetItemCount();
	char *temp=new char();
	CString temp1="您当前有";
	CString temp2=itoa(i,temp,10);
	CString temp3="位好友,";
    CString temp4;
	if(i<=3)
	   temp4=hello[i];
	else
	   temp4=hello[4];

	CString temp5=temp1+temp2+temp3+temp4;
	
	SetDlgItemText(IDC_HELLO,temp5);
}

void CMyDlg::OnAdd() 
{
   
	// TODO: Add your control notification handler code here
	if(EditState==false)      //刚才处于浏览状态
	{
		this->ListRefresh();
		EditState=true;
		m_name="姓名";
		m_sex="请选择性别";
		m_age=0;
		m_mail="请输入邮箱";
		m_phone="请输入电话";
		m_note="无";
		GetDlgItem(IDC_MAN)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_WOMAN)->ShowWindow(SW_SHOW);
		UpdateData(false);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -