📄 dbbooksview.cpp
字号:
// DBBooksView.cpp : implementation of the CDBBooksView class
//
#include "stdafx.h"
#include "DBBooks.h"
#include "DBBooksSet.h"
#include "DBBooksDoc.h"
#include "DBBooksView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDBBooksView
IMPLEMENT_DYNCREATE(CDBBooksView, CRecordView)
BEGIN_MESSAGE_MAP(CDBBooksView, CRecordView)
//{{AFX_MSG_MAP(CDBBooksView)
ON_BN_CLICKED(IDC_BTAPPEND, OnBtappend)
ON_BN_CLICKED(IDC_BTTOP, OnBttop)
ON_BN_CLICKED(IDC_BTLAST, OnBtlast)
ON_BN_CLICKED(IDC_BTNEXT, OnBtnext)
ON_BN_CLICKED(IDC_BTPREV, OnBtprev)
ON_BN_CLICKED(IDC_BTEDIT, OnBtedit)
ON_BN_CLICKED(IDC_BTSEEK, OnBtseek)
ON_BN_CLICKED(IDC_BTDELETE, OnBtdelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
long CDBBooksView::SumRecords()
{
long s=0;
if(m_pSet->GetRecordCount()>=1)
{ m_pSet->MoveFirst();
while(!m_pSet->IsEOF()) {++s;m_pSet->MoveNext();}
}
return s;
}
////////////////////////////////////////////
long CDBBooksView::DBGetAbsolutePosition()
{return pnum;} //pnum:当前记录号
//////////////////////////////////////
void CDBBooksView::DBMoveFirst()
{pnum=1;m_pSet->MoveFirst();}
//////////////////////////////////
void CDBBooksView::DBMovePrev()
{pnum--;m_pSet->MovePrev();}
///////////////////////////////////
void CDBBooksView::DBMoveNext()
{pnum++;m_pSet->MoveNext();}
////////////////////////////////////////
void CDBBooksView::DBMoveLast()
{pnum=tnum;m_pSet->MoveLast();}
/////////////////////////////////////
void CDBBooksView::DBSetAbsolutePosition(const long pos)
{
if(pos>=1 &&pos<=tnum)
{pnum=pos;m_pSet->SetAbsolutePosition(pos);}
}
////////////////////////////////
void CDBBooksView::DBAddRecord(const CString &xname, const CString &xnote)
{
m_pSet->AddNew();
++tnum;
m_pSet->m_name=xname;
m_pSet->m_note=xnote;
m_pSet->Update();
pnum=tnum;m_pSet->MoveLast();
}
//////////////////////////////////////
void CDBBooksView::DBUpdateRecord(const CString &xname,const CString &xnote)
{m_pSet->Edit();
m_pSet->m_name=xname;
m_pSet->m_note=xnote;
m_pSet->Update();
}
//////////////////////////////
void CDBBooksView::DBDeleteRecord()
{
if(pnum>=1 && pnum<=tnum)
{
m_pSet->Delete();
if(tnum>1)
{
if(pnum=tnum)
{pnum--;m_pSet->SetAbsolutePosition(pnum);}
else m_pSet->SetAbsolutePosition(pnum);
}
else pnum=0;
--tnum; //删除一条记录总数减1
}
}
/////////////////////////////////////////////
void CDBBooksView::PutDataToDialog()
{
if(tnum==0)
{
m_name=m_note="";m_pos="0/0";
UpdateData(FALSE);
GetDlgItem(IDC_BTTOP)->EnableWindow(FALSE);
GetDlgItem(IDC_BTPREV)->EnableWindow(FALSE);
GetDlgItem(IDC_BTNEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_BTLAST)->EnableWindow(FALSE);
GetDlgItem(IDC_BTDELETE)->EnableWindow(FALSE);
GetDlgItem(IDC_BTSEEK)->EnableWindow(FALSE);
GetDlgItem(IDC_BTEDIT)->EnableWindow(FALSE);
return;
}
GetDlgItem(IDC_BTDELETE)->EnableWindow(TRUE);
GetDlgItem(IDC_BTSEEK)->EnableWindow(TRUE);
GetDlgItem(IDC_BTEDIT)->EnableWindow(TRUE);
m_name=m_pSet->m_name;m_note=m_pSet->m_note;
char buf[32];sprintf(buf,"%d/%d",pnum,tnum);
m_pos=buf;UpdateData(FALSE);//映射到编辑框中
if(pnum>1)
{
GetDlgItem(IDC_BTTOP)->EnableWindow(TRUE);
GetDlgItem(IDC_BTPREV)->EnableWindow(TRUE);
}
else
{
GetDlgItem(IDC_BTTOP)->EnableWindow(FALSE);
GetDlgItem(IDC_BTPREV)->EnableWindow(FALSE);
}
if(pnum<tnum)
{
GetDlgItem(IDC_BTLAST)->EnableWindow(TRUE);
GetDlgItem(IDC_BTNEXT)->EnableWindow(TRUE);
}
else
{
GetDlgItem(IDC_BTLAST)->EnableWindow(FALSE);
GetDlgItem(IDC_BTNEXT)->EnableWindow(FALSE);
}
}
///////////////////////////////
bool CDBBooksView::DBFindRecord(const CString &xname)
{
if(tnum)
{
long pos=DBGetAbsolutePosition();
DBMoveFirst();
while(!m_pSet->IsEOF())
{
CString name=m_pSet->m_name;
name.TrimLeft();name.TrimRight();
if(name==xname) return true;
DBMoveNext();
}
DBSetAbsolutePosition(pos);
}
return false;
}
/////////////////////////////////////
bool CDBBooksView::GetDataFromDialog(bool AppendFlag)
{
if(AppendFlag)
{
if(DBFindRecord(getdataobj.m_name))
{
MessageBox("此书名已存在!","提示",MB_OK+MB_ICONEXCLAMATION);
return false;
}
else { DBAddRecord(getdataobj.m_name,getdataobj.m_note);}
}
else DBUpdateRecord(getdataobj.m_name,getdataobj.m_note);
return true;
}
/////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// CDBBooksView construction/destruction
CDBBooksView::CDBBooksView()
: CRecordView(CDBBooksView::IDD)
{
//{{AFX_DATA_INIT(CDBBooksView)
m_pSet = NULL;
m_name = _T("");
m_note = _T("");
m_pos = _T("");
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CDBBooksView::~CDBBooksView()
{
}
void CDBBooksView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDBBooksView)
DDX_Text(pDX, IDC_EDNAME, m_name);
DDX_Text(pDX, IDC_EDNOTE, m_note);
DDX_Text(pDX, IDC_EDPOS, m_pos);
//}}AFX_DATA_MAP
}
BOOL CDBBooksView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CDBBooksView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_dBBooksSet;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
tnum=SumRecords();
if (tnum) DBMoveFirst();
else pnum=0;
PutDataToDialog();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////
// CDBBooksView diagnostics
#ifdef _DEBUG
void CDBBooksView::AssertValid() const
{
CRecordView::AssertValid();
}
void CDBBooksView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CDBBooksDoc* CDBBooksView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDBBooksDoc)));
return (CDBBooksDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDBBooksView database support
CRecordset* CDBBooksView::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// CDBBooksView message handlers
void CDBBooksView::OnBtappend()
{
// TODO: Add your control notification handler code here
getdataobj.m_name="";getdataobj.m_note="NULL";
getdataobj.AppendFlag=true;
if(getdataobj.DoModal()==IDOK)
{
GetDataFromDialog(true);
PutDataToDialog();
}
}
//////////////////////////////////
void CDBBooksView::OnBttop()
{
// TODO: Add your control notification handler code here
DBMoveFirst();PutDataToDialog();
}
void CDBBooksView::OnBtlast()
{
// TODO: Add your control notification handler code here
DBMoveLast();PutDataToDialog();
}
void CDBBooksView::OnBtnext()
{
// TODO: Add your control notification handler code here
DBMoveNext();PutDataToDialog();
}
void CDBBooksView::OnBtprev()
{
// TODO: Add your control notification handler code here
DBMovePrev();PutDataToDialog();
}
void CDBBooksView::OnBtedit()
{
// TODO: Add your control notification handler code here
getdataobj.m_name=m_name;getdataobj.m_note=m_note;
getdataobj.AppendFlag=false;
if(getdataobj.DoModal()==IDOK)
{
GetDataFromDialog(false);PutDataToDialog();UpdateData(FALSE);
}
// TODO: Add your control notification handler code here
}
void CDBBooksView::OnBtseek()
{
// TODO: Add your control notification handler code here
if(getnameobj.DoModal()==IDOK)
{
if(!DBFindRecord(getnameobj.m_name))
MessageBox("此书名不存在!","提示",MB_OK+MB_ICONEXCLAMATION);
PutDataToDialog();
}
}
void CDBBooksView::OnBtdelete()
{
// TODO: Add your control notification handler code here
if (MessageBox("真的要删除这条记录吗?","确认",
MB_YESNO+MB_ICONEXCLAMATION)==IDYES)
{DBDeleteRecord();PutDataToDialog();UpdateData(FALSE);}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -