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

📄 telbookdlg.cpp

📁 这是我同学课余编写的自用小程序: 由于本程序是一个很小的小程序
💻 CPP
字号:
// TelBookDlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CTelBookDlg dialog

CTelBookDlg::CTelBookDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTelBookDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTelBookDlg)
		// 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 CTelBookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTelBookDlg)
	DDX_Control(pDX, IDC_LIST_CORDE, m_RecordList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTelBookDlg, CDialog)
	//{{AFX_MSG_MAP(CTelBookDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_FIND, OnFind)
//	ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
	ON_BN_CLICKED(IDC_MODIFY, OnModify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTelBookDlg message handlers

BOOL CTelBookDlg::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
	m_RecordList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);//设置表的风格
	/*设置表的字段*/
	m_RecordList.InsertColumn(0,"姓名",LVCFMT_LEFT,50);
	m_RecordList.InsertColumn(1,"电话号码",LVCFMT_LEFT,85);
	m_RecordList.InsertColumn(2,"手机号码",LVCFMT_LEFT,85);
	m_RecordList.InsertColumn(3,"QQ",LVCFMT_LEFT,70);
	m_RecordList.InsertColumn(4,"E-mail",LVCFMT_LEFT,130);
	m_RecordList.InsertColumn(5,"传真号",LVCFMT_LEFT,85);
	m_RecordList.InsertColumn(6,"传呼机",LVCFMT_LEFT,85);
	m_RecordList.InsertColumn(7,"邮编",LVCFMT_LEFT,50);
	m_RecordList.InsertColumn(8,"地址",LVCFMT_LEFT,200);
    //db.Open(NULL,false,false,"Driver={Microsoft Access Driver (*.mdb)};DBQ=.\\record.mdb;");
	db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=.\\record.mdb;",CDatabase::useCursorLib);
	if(!db.IsOpen()){
		MessageBox("Sorry!Can't open the DateBase!","错误",MB_OK |MB_DEFBUTTON2 |MB_ICONERROR);
		exit(1);
	}
	m_pSet.m_pDatabase=&db;
	m_pSet.Open(CRecordset::snapshot,"table1");//打开一个表
	if(!m_pSet.IsOpen()){
		MessageBox("Sorry!不能打开记录表","错误",MB_OK | MB_ICONERROR);
		exit(1);
	}
	if(m_pSet.GetRecordCount()<=0)//判断数据库里是否有记录
		return true;
	while(!m_pSet.IsEOF( ))
		m_pSet.MoveNext();//移动到最后一个记录,下的GetRecordCount()才能得到正确的记录数目,不能用MoveLast()
	long int n=m_pSet.GetRecordCount();
	CString str;
	m_pSet.MoveFirst();
	if(m_pSet.IsBOF())
		m_pSet.MoveNext();
			
	for(int i=0;i<n&&!m_pSet.IsEOF();i++){
		AddItemToList(i);
		m_pSet.MoveNext();
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CTelBookDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	CMyDlg AddDlg;
	if( AddDlg.DoModal()== IDOK ){
		m_pSet.AddNew();
		/*传递输入的数据*/
		m_pSet.m_Name=AddDlg.m_name;
		m_pSet.m_PhoNo=AddDlg.m_pho;
		m_pSet.m_MobNo=AddDlg.m_mobi;
		m_pSet.m_QQ=AddDlg.m_QQ;
		m_pSet.m_E_mail=AddDlg.m_email;
		m_pSet.m_Call_No=AddDlg.m_callNo;
		m_pSet.m_Call=AddDlg.m_call;
		m_pSet.m_Post=AddDlg.m_post;
		m_pSet.m_Address=AddDlg.m_address;
		
		if( m_pSet.Update() ){/*向数据库写入数据*/
			m_pSet.Requery();
			
			int i=m_RecordList.GetItemCount();
			m_RecordList.InsertItem(i,"");
			m_RecordList.SetItemText(i,0,AddDlg.m_name);
			m_RecordList.SetItemText(i,1,AddDlg.m_pho);
			m_RecordList.SetItemText(i,2,AddDlg.m_mobi);
			m_RecordList.SetItemText(i,3,AddDlg.m_QQ);
			m_RecordList.SetItemText(i,4,AddDlg.m_email);
			m_RecordList.SetItemText(i,5,AddDlg.m_callNo);
			m_RecordList.SetItemText(i,6,AddDlg.m_call);
			m_RecordList.SetItemText(i,7,AddDlg.m_post);
			m_RecordList.SetItemText(i,8,AddDlg.m_address);
			POINT point;
			m_RecordList.GetItemPosition(i-2,&point);//获得的位置是第index条记录的下边位置,所以要减2
			m_RecordList.Scroll(CSize(0,point.y));//滚动到指定位置
			m_RecordList.SetFocus();
			m_RecordList.SetItemState(i,  LVNI_SELECTED,LVNI_SELECTED);//设置index项被选中 
			/*
			//将指针移到最后一条记录,即刚添加的记录
			m_pSet.MoveLast();
			if(m_pSet.IsEOF())
			m_pSet.MovePrev();
			*/
			/*
			m_pSet.m_strFilter="姓名=";
			m_pSet.m_strFilter+="'"+AddDlg.m_name+"'";
			if( m_pSet.Requery() )
			*///	AddItemToList(i);//从数据库中读出并显示刚添加的记录
		}
	}
}

void CTelBookDlg::AddItemToList(int nIndex)
{
	CString str;
	str.Format("%d",nIndex);
	m_RecordList.InsertItem(nIndex,str);
	
	m_pSet.GetFieldValue("姓名",str);
	m_RecordList.SetItemText(nIndex,0,str);
	
	m_pSet.GetFieldValue("电话号码",str);
	m_RecordList.SetItemText(nIndex,1,str);
	
	m_pSet.GetFieldValue("手机号",str);
	m_RecordList.SetItemText(nIndex,2,str);
	
	m_pSet.GetFieldValue("QQ",str);
	m_RecordList.SetItemText(nIndex,3,str);
	
	m_pSet.GetFieldValue("E-mail",str);
	m_RecordList.SetItemText(nIndex,4,str);
	
	m_pSet.GetFieldValue("传真号",str);
	m_RecordList.SetItemText(nIndex,5,str);
	
	m_pSet.GetFieldValue("传呼机",str);
	m_RecordList.SetItemText(nIndex,6,str);
	
	m_pSet.GetFieldValue("邮编",str);
	m_RecordList.SetItemText(nIndex,7,str);
	
	m_pSet.GetFieldValue("地址",str);
	m_RecordList.SetItemText(nIndex,8,str);
}

void CTelBookDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}

void CTelBookDlg::OnAbout() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg AbDlg;
	AbDlg.DoModal();
}

void CTelBookDlg::OnButtonHelp() 
{
	// TODO: Add your control notification handler code here
	ShellExecute(NULL,"open","ReadMe.txt",NULL,NULL,SW_SHOW);
}

void CTelBookDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	 POSITION pos = m_RecordList.GetFirstSelectedItemPosition();
	 if(pos==NULL){
		 MessageBox("请选中你要删除的记录","提示",MB_ICONWARNING);
		 return;
	 }

	 int index = m_RecordList.GetNextSelectedItem(pos);  // 得到项目索引
	 CString del_name=m_RecordList.GetItemText(index,0);
	 m_RecordList.DeleteItem(index);
     m_pSet.m_strFilter="姓名=";
	 m_pSet.m_strFilter+="'"+del_name+"'";//设置数据库查找的条件 姓名='NAME'
	 if( m_pSet.Requery()){
		 m_pSet.Delete();//在数据库中删除记录
		 m_pSet.MoveNext();
		 if( m_pSet.IsEOF() )
			 m_pSet.MoveLast();
		 if( m_pSet.IsBOF() )
			 m_pSet.SetFieldNull(NULL);
		 m_pSet.Requery();
	 }
}

void CTelBookDlg::OnFind() 
{ 
	// TODO: Add your control notification handler code here
	CFindDlg FinDlg;
	if( FinDlg.DoModal()==IDOK ){
		LVFINDINFO FindInfo;//设置查找的信息
		FindInfo.flags=LVFI_STRING;
		FindInfo.psz=FinDlg.m_find;
		int index=m_RecordList.FindItem(&FindInfo);
		POINT point;
		if( index==-1 ){
			MessageBox("没有找到"+FinDlg.m_find,"提示", MB_ICONSTOP);
			return;
		}
		m_RecordList.GetItemPosition(index-2,&point);//获得的位置是第index条记录的下边位置,所以要减2
		m_RecordList.Scroll(CSize(0,point.y));//滚动到指定位置
		m_RecordList.SetFocus();
		m_RecordList.SetItemState(index,  LVNI_SELECTED,LVNI_SELECTED);//设置index项被选中  
		}

}

void CTelBookDlg::OnModify() 
{
	// TODO: Add your control notification handler code here
	CMyDlg ModifyDlg;
	POSITION pos = m_RecordList.GetFirstSelectedItemPosition();
	if(pos==NULL){
		MessageBox("请选中你要修改的记录","提示",MB_ICONWARNING);
		return;
	}
	int index = m_RecordList.GetNextSelectedItem(pos);  // 得到项目索引
	CString del_name=m_RecordList.GetItemText(index,0);
	m_pSet.m_strFilter="姓名=";
	m_pSet.m_strFilter+="'"+del_name+"'";
	if( m_pSet.Requery()){//在数据库中找到要修改的记录
		ModifyDlg.m_name=m_pSet.m_Name;
		ModifyDlg.m_pho=m_pSet.m_PhoNo;
		ModifyDlg.m_mobi=m_pSet.m_MobNo;
		ModifyDlg.m_QQ=m_pSet.m_QQ;
		ModifyDlg.m_email=m_pSet.m_E_mail;
		ModifyDlg.m_callNo=m_pSet.m_Call_No;
		ModifyDlg.m_call=m_pSet.m_Call;
		ModifyDlg.m_post=m_pSet.m_Post;
		ModifyDlg.m_address=m_pSet.m_Address;
		UpdateData(false);//显示出要修改的全部内容
		if( ModifyDlg.DoModal()==IDOK){
			m_pSet.Edit();
			m_pSet.m_Name=ModifyDlg.m_name;
			m_pSet.m_PhoNo=ModifyDlg.m_pho;
			m_pSet.m_MobNo=ModifyDlg.m_mobi;
			m_pSet.m_QQ=ModifyDlg.m_QQ;
			m_pSet.m_E_mail=ModifyDlg.m_email;
			m_pSet.m_Call_No=ModifyDlg.m_callNo;
			m_pSet.m_Call=ModifyDlg.m_call;
			m_pSet.m_Post=ModifyDlg.m_post;
			m_pSet.m_Address=ModifyDlg.m_address;
			
			m_pSet.Update();/*向数据库写入数据*/
			CString str;
			/*在ContolList中显示修改后的内容*/
			m_pSet.GetFieldValue("姓名",str);
			m_RecordList.SetItemText(index,0,str);
			
			m_pSet.GetFieldValue("电话号码",str);
			m_RecordList.SetItemText(index,1,str);
			
			m_pSet.GetFieldValue("手机号",str);
			m_RecordList.SetItemText(index,2,str);
			
			m_pSet.GetFieldValue("QQ",str);
			m_RecordList.SetItemText(index,3,str);
			
			m_pSet.GetFieldValue("E-mail",str);
			m_RecordList.SetItemText(index,4,str);
			
			m_pSet.GetFieldValue("传真号",str);
			m_RecordList.SetItemText(index,5,str);
			
			m_pSet.GetFieldValue("传呼机",str);
			m_RecordList.SetItemText(index,6,str);
			
			m_pSet.GetFieldValue("邮编",str);
			m_RecordList.SetItemText(index,7,str);
			
			m_pSet.GetFieldValue("地址",str);
			m_RecordList.SetItemText(index,8,str);
			
			m_RecordList.SetFocus();
			m_RecordList.SetItemState(index,  LVNI_SELECTED,LVNI_SELECTED);//设置index项被选中 

			m_pSet.Requery();
		}
	}
	else
		MessageBox("在数据库中没有找到你要修改的记录","提示", MB_ICONERROR);
}

⌨️ 快捷键说明

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