?? bookview.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 + -