📄 booksview.cpp
字号:
// booksView.cpp : implementation of the CBooksView class
//
#include "stdafx.h"
#include "books.h"
#include "booksSet.h"
#include "booksDoc.h"
#include "booksView.h"
#include "SearchDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBooksView
IMPLEMENT_DYNCREATE(CBooksView, CRecordView)
BEGIN_MESSAGE_MAP(CBooksView, CRecordView)
//{{AFX_MSG_MAP(CBooksView)
ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
ON_UPDATE_COMMAND_UI(ID_RECORD_DELETE, OnUpdateRecordDelete)
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()
/////////////////////////////////////////////////////////////////////////////
// CBooksView construction/destruction
CBooksView::CBooksView()
: CRecordView(CBooksView::IDD)
{
//{{AFX_DATA_INIT(CBooksView)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CBooksView::~CBooksView()
{
}
void CBooksView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBooksView)
DDX_FieldText(pDX, IDC_AUTHOR, m_pSet->m_author, m_pSet);
DDX_FieldText(pDX, IDC_NAME, m_pSet->m_name, m_pSet);
DDX_FieldText(pDX, IDC_NUMBER, m_pSet->m_number, m_pSet);
DDX_FieldText(pDX, IDC_PRICE, m_pSet->m_price, m_pSet);
DDX_FieldText(pDX, IDC_PUBLISH, m_pSet->m_publish, m_pSet);
//}}AFX_DATA_MAP
}
BOOL CBooksView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CBooksView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_booksSet;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CBooksView printing
BOOL CBooksView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CBooksView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CBooksView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CBooksView diagnostics
#ifdef _DEBUG
void CBooksView::AssertValid() const
{
CRecordView::AssertValid();
}
void CBooksView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CBooksDoc* CBooksView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBooksDoc)));
return (CBooksDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBooksView database support
CRecordset* CBooksView::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// CBooksView message handlers
void CBooksView::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;
m_pSet->AddNew();
m_pSet->m_number=m_lNewID;
m_pSet->Update();
m_pSet->Requery();
m_pSet->MoveLast();
UpdateData(FALSE);
}
void CBooksView::OnRecordDelete()
{
// 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 CBooksView::OnUpdateRecordDelete(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(!m_pSet->IsEOF());
}
void CBooksView::OnRecordSearch()
{
// TODO: Add your command handler code here
DoFilter("name");
}
void CBooksView::DoFilter(CString col)
{
CSearchDlg 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 + -