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

📄 mousedlg.cpp

📁 一个用MFC开发的简单的通讯录
💻 CPP
字号:
// MouseDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CMouseDlg dialog

CMouseDlg::CMouseDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMouseDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMouseDlg)
	m_NAME = _T("");
	m_QQ = 0;
	size=0;
	item__len=0;
	m_RELATH = _T("");
	m_TELPH = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CMouseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMouseDlg)
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
	DDX_Text(pDX, IDC_NAME, m_NAME);
	DDX_Text(pDX, IDC_QQ, m_QQ);
	DDX_Text(pDX, IDC_RELATH, m_RELATH);
	DDX_Text(pDX, IDC_TELPH, m_TELPH);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMouseDlg, CDialog)
	//{{AFX_MSG_MAP(CMouseDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Add, OnAdd)
	ON_BN_CLICKED(IDC_Del, OnDel)
	ON_BN_CLICKED(IDC_Change, OnChange)
	ON_BN_CLICKED(IDC_Find, OnFind)
	ON_BN_CLICKED(IDC_Clear, OnClear)
	ON_BN_CLICKED(IDC_Show, OnShow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMouseDlg message handlers

BOOL CMouseDlg::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
    Load();
	m_ListCtrl.ModifyStyle(LVS_TYPEMASK,LVS_REPORT);
	ListView_SetExtendedListViewStyle(m_ListCtrl.m_hWnd,LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
    m_ListCtrl.InsertColumn(0,"Name",LVCFMT_CENTER,96,0);
    m_ListCtrl.InsertColumn(1,"Telephone",LVCFMT_CENTER,96,1);
	m_ListCtrl.InsertColumn(2,"QQ",LVCFMT_CENTER,96,2);
    m_ListCtrl.InsertColumn(3,"Selection",LVCFMT_CENTER,96,3);
	for(int item=0;item<PersoN.size();item++)
	{
       m_ListCtrl.InsertItem(item,PersoN[item].getname());
       m_ListCtrl.SetItemText(item,1,PersoN[item].gettpnum());
       char a[20];
	   sprintf(a,"%d",PersoN[item].getqq());
       m_ListCtrl.SetItemText(item,2,a);
	   m_ListCtrl.SetItemText(item,3,PersoN[item].getselection());
	}
	item__len=PersoN.size();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMouseDlg::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 CMouseDlg::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 CMouseDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
  //保存操作
void CMouseDlg::Save()
{
	ofstream out("person.dat",ios::binary);
	for(int i=0;i<PersoN.size();i++)
		out.write((char*)&PersoN[i],sizeof(person));
	out.close();
}
//载入操作
void CMouseDlg::Load()
{
	ifstream in1("person.dat",ios_base::binary);
	person temp;
	while(in1)
	{
		in1.read((char*)&temp,sizeof(temp));
		if(!in1)break;
		PersoN.push_back(temp);
	}
	in1.close();
}
//添加操作
bool CMouseDlg::Add_person()
{
	person temp;
    UpdateData();
	if(""==m_NAME)
	{
		MessageBox("姓名不能为空!");
		return false;
	}
	string name=m_NAME;
    temp.setname(name.c_str());
    string tpnum=m_TELPH;
	temp.settpnum(tpnum.c_str());
    long qq=m_QQ;
    temp.setqq(qq);
    string selection=m_RELATH;
    temp.setselection(selection.c_str());
	PersoN.push_back(temp);
	return true;
}
//取得容器中的索引值
int CMouseDlg::Getnum(char *Name)
{
	person person0;
	ifstream in2("person.dat",ios::binary);
	for(int i=0;i<PersoN.size();i++)
	{
		in2.read((char*)&person0,sizeof(person0));
		if(strcmp(Name,person0.getname())==0)
		{
			in2.close();
		    return i;
		}
	}
	return -1;
}
//删除操作
bool CMouseDlg::Del_person()
{
	UpdateData();
    char Name[20];
	sprintf(Name,m_NAME);

	int i,n;
	n=Getnum(Name);
	for(i=0;i<PersoN.size();i++)
	{
		if(i==n)
		goto right;
	}
	MessageBox("输入姓名不存在,请重新输入!");
	return false;
right:	vector<person>::iterator it=PersoN.begin()+i;
	PersoN.erase(it,it+1);
	return true;
}
//查找操作
bool CMouseDlg::Find_person()
{
	UpdateData();
	int i,n;
	char Name[20];
	sprintf(Name,m_NAME);

	n=Getnum(Name);
	for(i=0;i<PersoN.size();i++)
		if(n==i)
		{
			char a[100];
            sprintf(a,"要查找的信息:\t\t\t\nName:%s\nTelphone:%s\nQQ:%d\nSelection:%s\n",PersoN[i].getname(),
			PersoN[i].gettpnum(),PersoN[i].getqq(),PersoN[i].getselection());
			MessageBox(a);
		    return true;
		}
	return false;
}
//修改操作
bool CMouseDlg::Change_person()
{
	UpdateData();
	int i,n;
	char Name[20];
	sprintf(Name,m_NAME);

	n=Getnum(Name);
	for(i=0;i<PersoN.size();i++)
	{
		if(n==i)
		{   
			UpdateData();
            string temp=m_NAME;
            if(temp.size()!=0)
			PersoN[i].setname(temp.c_str());
            string tpnum=m_TELPH;
            if(tpnum.size()!=0)
			PersoN[i].settpnum(tpnum.c_str());
            long Qq=m_QQ;
			if(Qq!=0)
			PersoN[i].setqq(Qq);
			string tep=m_RELATH;
			if(temp.size()!=0)
			PersoN[i].setselection(tep.c_str());
			return true;
		}
	}
	MessageBox("没有此用户!");
	return false; 
}
//显示信息


void CMouseDlg::OnAdd() 
{
	size=PersoN.size();
	if(Add_person())
	{
	    Save();
		Add_Item();
        Brush();
		MessageBox("添加成功!");
	}
	else MessageBox("添加失败!");// TODO: Add your control notification handler code here
    size=PersoN.size();
}

void CMouseDlg::OnDel() 
{
	size=PersoN.size();
	int a=MessageBox("确定删除吗?","警告",MB_YESNO);
	    if (a==IDYES)
	{
		if(Del_person())
		{
			Save();
			Add_Item();
			Brush();
			MessageBox("删除成功!");
		}
		else MessageBox("删除失败!");// TODO: Add your control notification handler code here
	}
}

void CMouseDlg::OnChange() 
{
    int a=MessageBox("确定修改吗?","警告",MB_YESNO);
    if (a==IDYES)
	{
		if(Change_person())
		{
			Save();
		    Add_Item();
            Brush();
			MessageBox("修改成功!");
		}
		else MessageBox("修改失败!");// TODO: Add your control notification handler code here
	}
}

void CMouseDlg::OnFind() 
{
	if(Find_person())
	{

		Save();
	}
	else MessageBox("查找失败!");// TODO: Add your control notification handler code here
	
}

void CMouseDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	int a=MessageBox("确定清空吗?慎用!","警告",MB_YESNO);
	if (a==IDYES)
	{
		size=PersoN.size();
	   for(int item=0 ;item<size;item++)
		{
		   int iPos=item;
		   m_ListCtrl.SetItemText(iPos,0,"");
		   m_ListCtrl.SetItemText(iPos,1,"");
		   m_ListCtrl.SetItemText(iPos,2,"");
		   m_ListCtrl.SetItemText(iPos,3,"");
		}
	  while(PersoN.size())
		{
			vector<person>::iterator it=PersoN.begin();
			PersoN.erase(it,it+1);
			Save();
		}
		MessageBox("清空成功!");
	}

}

void CMouseDlg::OnShow() 
{
	size=PersoN.size();
	Add_Item();
    Brush();
}
bool CMouseDlg::Brush()
{
	for(int item=0 ;item<size;item++)
	{
       int iPos=item;
       m_ListCtrl.SetItemText(iPos,0,"");
	   m_ListCtrl.SetItemText(iPos,1,"");
       m_ListCtrl.SetItemText(iPos,2,"");
	   m_ListCtrl.SetItemText(iPos,3,"");
	}
    for(item=0;item<PersoN.size();item++)
	{
       m_ListCtrl.SetItemText(item,0,PersoN[item].getname());
       m_ListCtrl.SetItemText(item,1,PersoN[item].gettpnum());
       char a[20];
	   sprintf(a,"%d",PersoN[item].getqq());
       m_ListCtrl.SetItemText(item,2,a);
	   m_ListCtrl.SetItemText(item,3,PersoN[item].getselection());
	}
    return 1;
}
void CMouseDlg::Add_Item()
{
   for(;item__len<PersoN.size();item__len++)
	{
       m_ListCtrl.InsertItem(item__len,"");
	}
}

⌨️ 快捷键说明

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