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

📄 ex_a9view.cpp

📁 Visual C++应用教程-源代码 本书在复习C++基础知识后
💻 CPP
字号:
// Ex_A9View.cpp : implementation of the CEx_A9View class
//

#include "stdafx.h"
#include "Ex_A9.h"

#include "Ex_A9Doc.h"
#include "Ex_A9View.h"
#include "StuInfoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEx_A9View

IMPLEMENT_DYNCREATE(CEx_A9View, CListView)

BEGIN_MESSAGE_MAP(CEx_A9View, CListView)
	//{{AFX_MSG_MAP(CEx_A9View)
	ON_COMMAND(ID_STUINFO_ADD, OnStuinfoAdd)
	ON_WM_RBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CEx_A9View construction/destruction

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

}

CEx_A9View::~CEx_A9View()
{
}

BOOL CEx_A9View::PreCreateWindow(CREATESTRUCT& cs)
{
	cs.style |= LVS_REPORT | LVS_SHOWSELALWAYS ;	
	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEx_A9View drawing

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

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

	CListCtrl& m_ListCtrl = GetListCtrl();
	m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_pConnection.CreateInstance(__uuidof(Connection)); 	// 初始化Connection指针
	m_pConnection->ConnectionString
		="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\student.mdb;"; 								
		// 此句代码在一行书写
	m_pConnection->ConnectionTimeout = 30; 					// 允许连接超时时间,单位为秒
	HRESULT  hr = m_pConnection->Open("","","",0); 
	if (hr != S_OK) 
		MessageBox("无法连接指定的数据库!");
	DispAllRec();
}

/////////////////////////////////////////////////////////////////////////////
// CEx_A9View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx_A9View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx_A9View message handlers

void CEx_A9View::DispAllRec()
{
	CListCtrl& m_ListCtrl = GetListCtrl();
	// 删除列表中所有行和列表头
	m_ListCtrl.DeleteAllItems();
	int nColumnCount = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
	for (int i=0; i<nColumnCount; i++)
		m_ListCtrl.DeleteColumn(0);

	_CommandPtr pCmd;
	pCmd.CreateInstance(__uuidof(Command));			// 初始化Command指针
	pCmd->ActiveConnection = m_pConnection;			// 指向已有的连接
	CString strText = "SELECT * FROM student ORDER BY studentno";
	pCmd->CommandText = _bstr_t(strText);

	_RecordsetPtr pSet;
	pSet.CreateInstance(__uuidof(Recordset));	// 初始化Recordset指针
	pSet = pCmd->Execute(NULL, NULL, adCmdText );	
	// 建立列表控件的列表头
	FieldsPtr flds = pSet->GetFields();			// 获取当前表的字段指针
	_variant_t Index;
	Index.vt = VT_I2;
	m_ListCtrl.InsertColumn(0, "序号", LVCFMT_LEFT, 40 );
	for (i = 0; i < (int)flds->GetCount(); i++)   {
		Index.iVal=i;
		int nWidth = flds->GetItem(Index)->GetDefinedSize()*9;
		if ( nWidth < 40 ) nWidth = 40;
		m_ListCtrl.InsertColumn(i+1, (LPSTR)flds->GetItem(Index)->GetName(), 
			LVCFMT_LEFT,  nWidth);
	}
	// 显示记录
	_bstr_t str, value;
	int nItem = 0;
	CString strItem;
	while(!pSet->adoEOF){
		strItem.Format("%d", nItem+1);
		m_ListCtrl.InsertItem(nItem, strItem );
		for (i = 0; i < (int)flds->GetCount(); i++)  {
			Index.iVal=i;
			str = flds->GetItem(Index)->GetName();
			value = pSet->GetCollect(str);
			m_ListCtrl.SetItemText( nItem, i+1, (LPCSTR)value );
		}
		pSet->MoveNext();
		nItem++;
	}
	pSet->Close();	
}

void CEx_A9View::OnStuinfoAdd() 
{
	CStuInfoDlg dlg;
	if (dlg.DoModal() != IDOK) return;
	// 向student表添加新的记录,为了防止相同的记录添加,这里先来判断
	_CommandPtr pCmd;
	pCmd.CreateInstance(__uuidof(Command));			// 初始化Command指针
	pCmd->ActiveConnection = m_pConnection;			// 指向已有的连接
	CString strText;
	strText.Format( "SELECT * FROM student WHERE studentname='%s' AND studentno='%s'",
		dlg.m_strName, dlg.m_strNO );
	pCmd->CommandText = _bstr_t(strText);
	_RecordsetPtr pSet;
	pSet.CreateInstance(__uuidof(Recordset));		// 初始化Recordset指针
	pSet = pCmd->Execute(NULL, NULL, adCmdText );	
	if (!pSet->adoEOF) {
		MessageBox(dlg.m_strName + " 学生记录已添加过!", "重复添加");
		pSet->Close();
		return;
	}
	// 处理性别字符串
	CString strSex, strTime;
	if ( dlg.m_strSex == "男" ) strSex = "True";
	else strSex = "False";
	// 处理时间字符串
	strTime = dlg.m_tBirth.Format("%Y-%m-%d");	// 按年-月-日格式
	// 添加记录
	strText.Format( "INSERT INTO student(studentname,studentno,xb,birthday,special) \
		VALUES('%s','%s',%s,'%s','%s')",
		dlg.m_strName, dlg.m_strNO, strSex, strTime, dlg.m_strSpec );
	pCmd->CommandText = _bstr_t(strText);	
	pCmd->Execute(NULL, NULL, adCmdText );	
	DispAllRec();
}

void CEx_A9View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CListView::OnRButtonDown(nFlags, point);
	CListCtrl& m_ListCtrl = GetListCtrl();
	UINT uFlags;
	int nItem = m_ListCtrl.HitTest(point, &uFlags);
	if (uFlags & LVHT_ONITEMLABEL)	{
		CStuInfoDlg dlg;
		dlg.m_strOKText	= "修改";				// 按钮标题
		dlg.m_strName	= m_ListCtrl.GetItemText( nItem, 1);	
		dlg.m_strNO		= m_ListCtrl.GetItemText( nItem, 2);	
		// 处理性别
		CString strSex, strTime;
		strSex = m_ListCtrl.GetItemText( nItem, 3);
		if (strSex=="0") strSex = "女";
		else strSex = "男";
		dlg.m_strSex = strSex;
		// 处理时间,由字符串转换成时间
		strTime = m_ListCtrl.GetItemText( nItem, 4);
		COleVariant vt(strTime);
		vt.ChangeType(VT_DATE);
		COleDateTime tm = vt;
		dlg.m_tBirth	= CTime(tm.GetYear(), tm.GetMonth(), tm.GetDay(), 0, 0, 0);
		dlg.m_strSpec	= m_ListCtrl.GetItemText( nItem, 5);
		if (IDOK == dlg.DoModal())	{	// 修改
			// 处理性别字符串
			CString strSex, strTime;
			if ( dlg.m_strSex == "男" ) strSex = "True";
			else strSex = "False";
			// 处理时间字符串
			strTime = dlg.m_tBirth.Format("%Y-%m-%d");		// 按年-月-日格式
			// 更新记录
			_CommandPtr pCmd;
			pCmd.CreateInstance(__uuidof(Command));			
			pCmd->ActiveConnection = m_pConnection;	
			CString strText;
			strText.Format( "UPDATE student SET xb=%s,birthday='%s',special='%s'\
				WHERE studentname='%s' AND studentno='%s'",
				strSex, strTime, dlg.m_strSpec, dlg.m_strName, dlg.m_strNO );
			pCmd->CommandText = _bstr_t(strText);
			pCmd->Execute(NULL, NULL, adCmdText );	
			DispAllRec();
		}
	}
	
}

⌨️ 快捷键说明

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