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

📄 dictview.cpp

📁 这个程序详细介绍了用C++设计基于数据库启动的电子辞典
💻 CPP
字号:
// dictView.cpp : implementation of the CDictView class
//

#include "stdafx.h"
#include "dict.h"

#include "dictSet.h"
#include "dictDoc.h"
#include "dictView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDictView

IMPLEMENT_DYNCREATE(CDictView, CRecordView)

BEGIN_MESSAGE_MAP(CDictView, CRecordView)
	//{{AFX_MSG_MAP(CDictView)
	ON_COMMAND(ID_ADDRECORD, OnAddrecord)
	ON_COMMAND(ID_DELETERECORD, OnDeleterecord)
	ON_UPDATE_COMMAND_UI(ID_DELETERECORD, OnUpdateDeleterecord)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDictView construction/destruction

CDictView::CDictView()
	: CRecordView(CDictView::IDD)
{
	//{{AFX_DATA_INIT(CDictView)
	m_pSet = NULL;
	m_sentence = _T("");
	m_translate = _T("");
	m_word = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
   	m_bAdding = FALSE;
}

CDictView::~CDictView()
{
}

void CDictView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDictView)
	DDX_Text(pDX, IDC_SENTENCE, m_sentence);
	DDX_Text(pDX, IDC_TRANSLATE, m_translate);
	DDX_Text(pDX, IDC_WORD, m_word);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

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

}

/////////////////////////////////////////////////////////////////////////////
// CDictView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDictView diagnostics

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

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

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

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


/////////////////////////////////////////////////////////////////////////////
// CDictView message handlers

void CDictView::OnAddrecord() 
{
	// TODO: Add your command handler code here

	
	//将第一个字段改为可读写
//	CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_WORD);
//	int result = pCtrl->SetReadOnly(FALSE);
//	m_pSet->m_word="chang";
	CRecordsetStatus rStatus;
	m_pSet->GetStatus(rStatus);
	if(rStatus.m_lCurrentRecord>=0){
         UpdateData(TRUE);//从对话框得到数据
		 if(!m_bAdding ){
			 m_pSet->Edit();}
	  	 
		 m_pSet-> Update(); //将新记录存入数据库
		 m_pSet->Requery(); //重建记录集
	  m_pSet->MoveLast();
	}
	m_bAdding = TRUE;
   // UpdateData(TRUE); 
    m_pSet->SetFieldNull(NULL);
   	 m_pSet->AddNew();
	UpdateData(FALSE);
}

void CDictView::OnDeleterecord() 
{
	// TODO: Add your command handler code here
		try{
		m_pSet->Delete();
	}
	catch (CDBException* e){
		AfxMessageBox(e->m_strError);
		e->Delete();
		m_pSet->MoveFirst();
		UpdateData(FALSE);
		return;
	}
	if (m_pSet->IsEOF())
		m_pSet->MoveLast();
	if (m_pSet->IsBOF())
		m_pSet->SetFieldNull(NULL);

	UpdateData(FALSE);
}

 

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

BOOL CDictView::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
    if (m_bAdding){
		m_bAdding = FALSE;
		UpdateData(TRUE);
		//调用Update
		if (m_pSet->CanUpdate())
			m_pSet->Update();
		//将增加的记录移动最后
		if (!m_pSet->IsEOF())
			m_pSet->MoveLast();
		m_pSet->Requery();
		UpdateData(FALSE);
	 
		return TRUE;
	}
 
	
	 
}

⌨️ 快捷键说明

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