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

📄 bookview.cpp

📁 一个简单的图书管理系统
💻 CPP
字号:
// bookView.cpp : implementation of the CBookView class
//

#include "stdafx.h"
#include "book.h"

#include "bookSet.h"
#include "bookDoc.h"
#include "bookView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBookView

IMPLEMENT_DYNCREATE(CBookView, CRecordView)

BEGIN_MESSAGE_MAP(CBookView, CRecordView)
	//{{AFX_MSG_MAP(CBookView)
	ON_COMMAND(ID_RECORD_DEL, OnRecordDel)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_UPDATE_COMMAND_UI(ID_RECORD_DEL, OnUpdateRecordDel)

	ON_COMMAND(ID_RECORD_SEARCH, OnRecordSearch)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBookView construction/destruction

CBookView::CBookView()
	: CRecordView(CBookView::IDD)
{
	//{{AFX_DATA_INIT(CBookView)
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CBookView::~CBookView()
{
}

void CBookView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBookView)
	DDX_FieldText(pDX, IDC_AUTHOR, m_pSet->m_author, m_pSet);
	DDX_FieldText(pDX, IDC_BOOKNAME, m_pSet->m_price, m_pSet);
	DDX_FieldText(pDX, IDC_NUMBER, m_pSet->m_number, m_pSet);
	DDX_FieldText(pDX, IDC_PUBLISH, m_pSet->m_publish, m_pSet);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CBookView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_bookSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CBookView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CBookView diagnostics

#ifdef _DEBUG
void CBookView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CBookView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CBookView database support
CRecordset* CBookView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CBookView message handlers

BOOL CBookView::OnMove(UINT nIDMoveCommand) 
{
	return CRecordView::OnMove(nIDMoveCommand);
}

void CBookView::OnRecordDel() 
{
	// TODO: Add your command handler code here
	CRecordsetStatus m_cStatus;
	try{
		m_pSet->Delete();
	}
	catch(CDBException* m_pEx)
	{
		AfxMessageBox(m_pEx->m_strError);
		m_pEx->Delete();
		//失败的话,将记录指针移到第一个记录
		m_pSet->MoveFirst();
		UpdateData(FALSE);
		return;
	}
	m_pSet->GetStatus(m_cStatus);
	if(m_cStatus.m_lCurrentRecord==0)
		//删除了最后一个记录
		m_pSet->MoveFirst();
	else
		m_pSet->MoveNext();
	UpdateData(FALSE);
	
}

void CBookView::OnRecordAdd() 
{
	// TODO: Add your command handler code here
		CRecordset * pSet=OnGetRecordset();//获取指向数据集的指针
	if(pSet->CanUpdate() && !pSet->IsDeleted())//确认对数据集的任何修改均已经保存
	{
		pSet->Edit();
		if(!UpdateData())
			return;
		pSet->Update();
	}

	long m_lNewID=m_pSet->GetMaxID()+1;	//获取新的ID值
	m_pSet->AddNew();					//添加一个新记录
	m_pSet->m_number=m_lNewID;			//设置新的ID标识
	m_pSet->Update();					//保存新的记录
	m_pSet->Requery();					//刷新数据集
	m_pSet->MoveLast();					//游标移到最后一条记录
	UpdateData(FALSE);					//更新表单
}

void CBookView::OnUpdateRecordDel(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
pCmdUI->Enable(!m_pSet->IsEOF());	
}

void CBookView::OnRecordUpdate() 
{
	// TODO: Add your command handler code here
		m_pSet->Edit();
	UpdateData(TRUE);
	if(m_pSet->CanUpdate())
		m_pSet->Update();
}

void CBookView::OnUpdateRecordUpdate(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!m_pSet->IsEOF());	
}

void CBookView::OnRecordSearch() 
{
	// TODO: Add your command handler code here
	DoFilter("author");
}

void CBookView::DoFilter(CString col)
{
	CSearchDialog dlg;
	int result=dlg.DoModal();
	if(result==IDOK)
	{
		CString str=col+"='"+dlg.m_namesearch+"'";
		m_pSet->Close();
		m_pSet->m_strFilter=str;
		m_pSet->Open();
		int recCount=m_pSet->GetRecordCount();
		if(recCount==0)
		{
			MessageBox("No matching records.");
			m_pSet->Close();
			m_pSet->m_strFilter;
			m_pSet->Open();
		}
	UpdateData(FALSE);
	}
}

⌨️ 快捷键说明

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