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

📄 adressview.cpp

📁 简单的通信录
💻 CPP
字号:
// AdressView.cpp : implementation of the CAdressView class
//

#include "stdafx.h"
#include "Adress.h"

#include "AdressDoc.h"
#include "AdressView.h"
#include "RecordAddDlg.h"
#include "RecordSearchDlg.h"
#include "RecordEditDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAdressView

IMPLEMENT_DYNCREATE(CAdressView, CListView)

BEGIN_MESSAGE_MAP(CAdressView, CListView)
	//{{AFX_MSG_MAP(CAdressView)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	ON_COMMAND(ID_SET_FONT, OnSetFont)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_SEARCH, OnRecordSearch)
	ON_COMMAND(ID_RECORD_SHOWALL, OnRecordShowall)
	ON_UPDATE_COMMAND_UI(ID_RECORD_SHOWALL, OnUpdateRecordShowall)
	ON_COMMAND(ID_RECORD_DEL, OnRecordDel)
	ON_UPDATE_COMMAND_UI(ID_RECORD_DEL, OnUpdateRecordDel)
	ON_COMMAND(ID_RECORD_EDIT, OnRecordEdit)
	ON_UPDATE_COMMAND_UI(ID_RECORD_EDIT, OnUpdateRecordEdit)
	ON_COMMAND(ID_EXPORTDATA, OnExportdata)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdressView construction/destruction

CAdressView::CAdressView()
{
	// TODO: add construction code here

}

CAdressView::~CAdressView()
{
}

BOOL CAdressView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAdressView drawing

void CAdressView::OnDraw(CDC* pDC)
{
	CAdressDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CAdressView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	CListCtrl &ctl = GetListCtrl();
	ctl.ModifyStyle(0,LVS_REPORT|LVS_SINGLESEL);
	ctl.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
    CHeaderCtrl*pH=ctl.GetHeaderCtrl();
	pH->ModifyStyle(0,HDS_HOTTRACK);

	m_pSet = &GetDocument()->m_dataSet;

	if(m_pSet->IsOpen())
		m_pSet->Close();
	m_pSet->Open();
	GetDocument()->SetTitle(m_pSet->m_pCfgInfo->szDaoFilePath);
	m_pFont = new CFont;
	m_pFont->CreateFontIndirect(&(m_pSet->m_pCfgInfo->lfViewFont));
	ctl.SetFont(m_pFont);
	ctl.SetTextColor(m_pSet->m_pCfgInfo->crFontColr);

	CString str;
	str.Format("序号");
	ctl.InsertColumn(0,str);

	CDaoFieldInfo fieldInfo;
	int i=0;
	while(i<m_pSet->m_nFields)//根据数据库字段数插入相应的列
	{
		m_pSet->GetFieldInfo(i,fieldInfo);
		ctl.InsertColumn(i+1,fieldInfo.m_strName);
		i++;
	}
	m_bIsAsc = TRUE;
	m_bIsShowAll = TRUE;

	ShowTableData();
}

/////////////////////////////////////////////////////////////////////////////
// CAdressView printing

BOOL CAdressView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAdressView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAdressView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CAdressView diagnostics

#ifdef _DEBUG
void CAdressView::AssertValid() const
{
	CListView::AssertValid();
}

void CAdressView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CAdressDoc* CAdressView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAdressDoc)));
	return (CAdressDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAdressView message handlers

void CAdressView::ShowTableData()
{
	CListCtrl &ctl = GetListCtrl();
	ctl.DeleteAllItems();

	if((m_pSet->IsEOF())&&(m_pSet->IsBOF()))
		return;//若数据库为空,返回。

	m_pSet->MoveFirst();

	int i = 0;
	int nMaxWidthColumn0 = 0;
	int nMaxWidthColumn1 = 0;
	int nMaxWidthColumn2 = 0;
	int nMaxWidthColumn3 = 0;
	int nMaxWidthColumn4 = 0;
	int nMaxWidthColumn5 = 0;

	while(!m_pSet->IsEOF())
	{
		LV_ITEM lvItem;
		lvItem.mask = LVIF_TEXT;
		lvItem.iItem = i;
		lvItem.iSubItem = 0;

		CString str;
		str.Format("%d",i+1);
		lvItem.pszText = (LPTSTR)(LPCTSTR)str;
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn0)
			nMaxWidthColumn0 = ctl.GetStringWidth(lvItem.pszText);
		ctl.InsertItem(&lvItem);

		lvItem.iSubItem = 1;
		lvItem.pszText = (LPTSTR)(LPCTSTR)(m_pSet->m_column1);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn1)
			nMaxWidthColumn1 = ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);

		lvItem.iSubItem = 2;
		lvItem.pszText = (LPTSTR)(LPCTSTR)(m_pSet->m_column2);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn2)
			nMaxWidthColumn2 = ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);

		lvItem.iSubItem = 3;
		lvItem.pszText = (LPTSTR)(LPCTSTR)(m_pSet->m_column3);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn3)
			nMaxWidthColumn3 = ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);

		lvItem.iSubItem = 4;
		lvItem.pszText = (LPTSTR)(LPCTSTR)(m_pSet->m_column4);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn4)
			nMaxWidthColumn4 = ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);

		lvItem.iSubItem = 5;
		lvItem.pszText = (LPTSTR)(LPCTSTR)(m_pSet->m_column5);
		if(ctl.GetStringWidth(lvItem.pszText)>nMaxWidthColumn5)
			nMaxWidthColumn5 = ctl.GetStringWidth(lvItem.pszText);
		ctl.SetItem(&lvItem);

		i++;
		m_pSet->MoveNext();
	}
	ctl.SetColumnWidth(0,nMaxWidthColumn0+30);
	ctl.SetColumnWidth(1,nMaxWidthColumn1+30);
	ctl.SetColumnWidth(2,nMaxWidthColumn2+30);
	ctl.SetColumnWidth(3,nMaxWidthColumn3+30);
	ctl.SetColumnWidth(4,nMaxWidthColumn4+30);
	ctl.SetColumnWidth(5,nMaxWidthColumn5+30);
	
	m_pSet->MoveFirst();
}

void CAdressView::Sort(int i,BOOL isAsc)
{
	CDaoFieldInfo fieldInfo;
	
	m_pSet->GetFieldInfo(i,fieldInfo);

	if(isAsc)
	{
		m_pSet->m_strSort = fieldInfo.m_strName+" ASC";
		m_bIsAsc = TRUE;
	}
	else
	{
		m_pSet->m_strSort = fieldInfo.m_strName+" DESC";
		m_bIsAsc = FALSE;
	}
	m_pSet->Requery();
	
}

void CAdressView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here

	int i = pNMListView->iSubItem;
	if(i>0)
	{
		Sort(i-1,!m_bIsAsc);
		ShowTableData();
	}

	*pResult = 0;
}

void CAdressView::OnSetFont() 
{
	// TODO: Add your command handler code here
	CListCtrl &ctl = GetListCtrl();

	LOGFONT lf;
	GetFont()->GetLogFont(&lf);
	CFontDialog fdlg(&lf);
	fdlg.m_cf.rgbColors = ctl.GetTextColor();
	if(fdlg.DoModal()==IDOK)
	{
		m_pFont = new CFont;
		m_pFont->CreateFontIndirect(fdlg.m_cf.lpLogFont);
		SetFont(m_pFont);
		m_pSet->m_pCfgInfo->lfViewFont = *(fdlg.m_cf.lpLogFont);
		ctl.SetTextColor(fdlg.m_cf.rgbColors);
		m_pSet->m_pCfgInfo->lfViewFont = *(fdlg.m_cf.lpLogFont);
		m_pSet->UpdateCfgInfo();
	}
	
}

//添加记录
void CAdressView::OnRecordAdd() 
{
	// TODO: Add your command handler code here
	MessageBox("注意:姓名不能重复,否则加不进去!","警告",MB_ICONWARNING|MB_OK);

	CRecordAddDlg addDlg;
	if(addDlg.DoModal()==IDOK)
	{
		if(addDlg.m_strName.IsEmpty())
			return;
		addDlg.m_strName.TrimLeft();
		addDlg.m_strName.TrimRight();
		addDlg.m_strGender.TrimLeft();
		addDlg.m_strGender.TrimRight();
		addDlg.m_strAddress.TrimLeft();
		addDlg.m_strAddress.TrimRight();
		addDlg.m_strTelephone.TrimLeft();
		addDlg.m_strTelephone.TrimRight();
		addDlg.m_strRemark.TrimLeft();
		addDlg.m_strRemark.TrimRight();
		
		m_pSet->AddNew();
		m_pSet->m_column1 = addDlg.m_strName;
		m_pSet->m_column2 = addDlg.m_strGender;
		m_pSet->m_column3 = addDlg.m_strAddress;
		m_pSet->m_column4 = addDlg.m_strTelephone;
		m_pSet->m_column5 = addDlg.m_strRemark;
		m_pSet->Update();

		Sort(0,m_bIsAsc);
		ShowTableData();
	}
		
}


//查询记录
void CAdressView::OnRecordSearch() 
{
	// TODO: Add your command handler code here
	CRecordSearchDlg searchDlg;
	if(searchDlg.DoModal()==IDOK)
	{
		if(searchDlg.m_strSearch.IsEmpty())
			return;
		ASSERT(m_pSet->CanRestart());
		searchDlg.m_strSearch.MakeLower();
		m_bIsShowAll = FALSE;
		CDaoFieldInfo fieldInfo;
		m_pSet->GetFieldInfo(0,fieldInfo);
		m_pSet->m_strFilter = fieldInfo.m_strName+" = "+"'"+searchDlg.m_strSearch+"'";
		m_pSet->Requery();
		ShowTableData();
		
	}
}

//显示数据库中的全部数据
void CAdressView::OnRecordShowall() 
{
	// TODO: Add your command handler code here
	if(m_bIsShowAll)
		return;
	m_pSet->m_strFilter.Empty();
	m_pSet->Requery();
	ShowTableData();
	m_bIsShowAll = TRUE;

}

void CAdressView::OnUpdateRecordShowall(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_bIsShowAll)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable();

}

void CAdressView::OnRecordDel() 
{
	// TODO: Add your command handler code here
	CListCtrl &ctl = GetListCtrl();
	POSITION pos = ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		return;
	int nItem = ctl.GetNextSelectedItem(pos);
	m_pSet->Move(nItem);

	CString str1,str2;
	str1.Format("警告:下列有关“");
	str2.Format("”的记录删除后将无法恢复!确实要删除吗?");
	str1 = str1+m_pSet->m_column1+str2;
	if(AfxMessageBox(str1,MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
	{
		m_pSet->Delete();
		m_pSet->MoveFirst();
		ShowTableData();
	}
	else
		ShowTableData();
}

void CAdressView::OnUpdateRecordDel(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CListCtrl &ctl = GetListCtrl();
	POSITION pos = ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable();
}

void CAdressView::OnRecordEdit() 
{
	// TODO: Add your command handler code here
	CListCtrl &ctl = GetListCtrl();
	POSITION pos = ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		return;
	int nItem = ctl.GetNextSelectedItem(pos);
	m_pSet->Move(nItem);
	CRecordEditDlg editDlg;

	editDlg.m_strName = m_pSet->m_column1;
	editDlg.m_strGender = m_pSet->m_column2;
	editDlg.m_strAddress = m_pSet->m_column3;
	editDlg.m_strTelephone = m_pSet->m_column4;
	editDlg.m_strRemark = m_pSet->m_column5;

	if(editDlg.DoModal()==IDOK)
	{
		m_pSet->Edit();
		m_pSet->m_column1 = editDlg.m_strName;
		m_pSet->m_column2 = editDlg.m_strGender;
		m_pSet->m_column3 = editDlg.m_strAddress;
		m_pSet->m_column4 = editDlg.m_strTelephone;
		m_pSet->m_column5 = editDlg.m_strRemark;
		m_pSet->Update();
		ShowTableData();
	}
	else
		ShowTableData();

}

void CAdressView::OnUpdateRecordEdit(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CListCtrl &ctl = GetListCtrl();
	POSITION pos = ctl.GetFirstSelectedItemPosition();
	if(pos==NULL)
		pCmdUI->Enable(FALSE);
	else
		pCmdUI->Enable();

}

void CAdressView::OnExportdata() 
{
	// TODO: Add your command handler code here
	CString strFilter,strTitle;
	strFilter.Format("文本文件(*.txt)|*.txt|");
	CFileDialog flDlg(FALSE,"txt",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strFilter);
	strTitle.Format("导出数据库");
	flDlg.m_ofn.lpstrTitle = strTitle;

	if(flDlg.DoModal()==IDOK)
	{
		CString strFilePath = flDlg.GetFileName();
		CStdioFile file;
		if(!file.Open(strFilePath,CFile::modeCreate|CFile::modeReadWrite))
			return;
		long pos = m_pSet->GetAbsolutePosition();
		long count = m_pSet->GetRecordCount();
		if(count>0)
		{
			m_pSet->MoveFirst();
			CDaoFieldInfo fieldInfo;
			m_pSet->GetFieldInfo(0,fieldInfo);
			long length1 = fieldInfo.m_lSize;
			m_pSet->GetFieldInfo(1,fieldInfo);
			long length2 = fieldInfo.m_lSize;
			m_pSet->GetFieldInfo(2,fieldInfo);
			long length3 = fieldInfo.m_lSize;
			m_pSet->GetFieldInfo(3,fieldInfo);
			long length4 = fieldInfo.m_lSize;

			while(!m_pSet->IsEOF())
			{
				CString str1,str2,str3,str4,str5;
				str1 = m_pSet->m_column1;
				str2 = m_pSet->m_column2;
				str3 = m_pSet->m_column3;
				str4 = m_pSet->m_column4;
				str5 = m_pSet->m_column5;
				str1.TrimLeft();
				str2.TrimLeft();
				str2.TrimRight();
				str3.TrimLeft();
				str3.TrimRight();
				str4.TrimLeft();
				str4.TrimRight();
				str5.TrimLeft();
				str5.TrimRight();

				int n1 = str1.GetLength();
				if(n1<length1)
					for(int j=0;j<length1-n1;j++)
						str1+=" ";
				int n2 = str2.GetLength();
				if(n2<length2)
					for(int j=0;j<length2-n2;j++)
						str2+=" ";
				int n3 = str3.GetLength();
				if(n3<length3)
					for(int j=0;j<length3-n3;j++)
						str3+=" ";
				int n4 = str4.GetLength();
				if(n4<length4)
					for(int j=0;j<length4-n4;j++)
						str4+=" ";

				CString strWrite;
				strWrite = str1+str2+str3+str4+str5+"\n";
				file.WriteString(strWrite);
				m_pSet->MoveNext();
			}
			m_pSet->SetAbsolutePosition(pos);
			file.Close();
		}
	}

}

⌨️ 快捷键说明

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