📄 ex16cview.cpp
字号:
// Ex16cView.cpp : implementation of the CEx16cView class
//
#include "stdafx.h"
#include "Ex16c.h"
#include "student.h"
#include "Ex16cDoc.h"
#include "Ex16cView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CEx16cView
IMPLEMENT_DYNCREATE(CEx16cView, CFormView)
BEGIN_MESSAGE_MAP(CEx16cView, CFormView)
ON_COMMAND(ID_STUDENT_HOME, OnStudentHome)
ON_COMMAND(ID_STUDENT_DELETE, OnStudentDelete)
ON_COMMAND(ID_STUDENT_END, OnStudentEnd)
ON_COMMAND(ID_STUDENT_INSERT, OnStudentInsert)
ON_COMMAND(ID_STUDENT_NEXT, OnStudentNext)
ON_COMMAND(ID_STUDENT_PREVIOUS, OnStudentPrevious)
ON_UPDATE_COMMAND_UI(ID_STUDENT_HOME, OnUpdateStudentHome)
ON_UPDATE_COMMAND_UI(ID_STUDENT_DELETE, OnUpdateStudentDelete)
ON_UPDATE_COMMAND_UI(ID_STUDENT_END, OnUpdateStudentEnd)
ON_UPDATE_COMMAND_UI(ID_STUDENT_NEXT, OnUpdateStudentNext)
ON_UPDATE_COMMAND_UI(ID_STUDENT_PREVIOUS, OnUpdateStudentPrevious)
ON_BN_CLICKED(IDC_CLEAR, OnBnClickedClear)
END_MESSAGE_MAP()
// CEx16cView construction/destruction
CEx16cView::CEx16cView()
: CFormView(CEx16cView::IDD)
, m_nGrade(0)
, m_strName(_T(""))
, m_position(NULL)
{
TRACE("Entering CEx16cView constructor\n");
}
CEx16cView::~CEx16cView()
{
}
void CEx16cView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Text(pDX, IDC_GRADE, m_nGrade);
DDX_Text(pDX, IDC_NAME, m_strName);
}
BOOL CEx16cView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CEx16cView::OnInitialUpdate()
{
TRACE("Entering CEx16cView::OnInitialUpdate\n");
m_pList = GetDocument()->GetList();
CFormView::OnInitialUpdate();
}
// CEx16cView diagnostics
#ifdef _DEBUG
void CEx16cView::AssertValid() const
{
CFormView::AssertValid();
}
void CEx16cView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CEx16cDoc* CEx16cView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx16cDoc)));
return (CEx16cDoc*)m_pDocument;
}
#endif //_DEBUG
// CEx16cView message handlers
void CEx16cView::OnStudentHome()
{
TRACE("Entering CEx16cView::OnStudentHome\n");
// need to deal with list empty condition
if (!m_pList->IsEmpty()) {
m_position = m_pList->GetHeadPosition();
GetEntry(m_position);
}
}
void CEx16cView::OnUpdateStudentHome(CCmdUI *pCmdUI)
{
// called during idle processing and when Student menu drops down
POSITION pos;
// enables button if list not empty and not at home already
pos = m_pList->GetHeadPosition();
pCmdUI->Enable((m_position != NULL) && (pos != m_position));
}
void CEx16cView::OnStudentDelete()
{
// deletes current entry and positions to next one or head
POSITION pos;
TRACE("Entering CEx16cView::OnStudentDelete\n");
if ((pos = m_position) != NULL) {
m_pList->GetNext(pos);
if (pos == NULL) {
pos = m_pList->GetHeadPosition();
TRACE("GetHeadPos = %ld\n", pos);
if (pos == m_position) {
pos = NULL;
}
}
GetEntry(pos);
CStudent* ps = m_pList->GetAt(m_position);
m_pList->RemoveAt(m_position);
delete ps;
m_position = pos;
GetDocument()->SetModifiedFlag();
GetDocument()->UpdateAllViews(this);
}
}
void CEx16cView::OnUpdateStudentDelete(CCmdUI *pCmdUI)
{
// called during idle processing and when Student menu drops down
pCmdUI->Enable(m_position != NULL);
}
void CEx16cView::OnStudentEnd()
{
TRACE("Entering CEx16cView::OnStudentEnd\n");
if (!m_pList->IsEmpty()) {
m_position = m_pList->GetTailPosition();
GetEntry(m_position);
}
}
void CEx16cView::OnUpdateStudentEnd(CCmdUI *pCmdUI)
{
// called during idle processing and when Student menu drops down
POSITION pos;
// enables button if list not empty and not at end already
pos = m_pList->GetTailPosition();
pCmdUI->Enable((m_position != NULL) && (pos != m_position));
}
void CEx16cView::OnStudentInsert()
{
TRACE("Entering CEx16cView::OnStudentInsert\n");
InsertEntry(m_position);
GetDocument()->SetModifiedFlag();
GetDocument()->UpdateAllViews(this);
}
void CEx16cView::OnStudentNext()
{
POSITION pos;
TRACE("Entering CEx16cView::OnStudentNext\n");
if ((pos = m_position) != NULL) {
m_pList->GetNext(pos);
if (pos) {
GetEntry(pos);
m_position = pos;
}
}
}
void CEx16cView::OnUpdateStudentNext(CCmdUI *pCmdUI)
{
OnUpdateStudentEnd(pCmdUI);
}
void CEx16cView::OnStudentPrevious()
{
POSITION pos;
TRACE("Entering CEx16cView::OnStudentPrevious\n");
if ((pos = m_position) != NULL) {
m_pList->GetPrev(pos);
if (pos) {
GetEntry(pos);
m_position = pos;
}
}
}
void CEx16cView::OnUpdateStudentPrevious(CCmdUI *pCmdUI)
{
OnUpdateStudentHome(pCmdUI);
}
void CEx16cView::OnUpdate(CView* /*pSender*/,
LPARAM /*lHint*/, CObject* /*pHint*/)
{
// called by OnInitialUpdate and by UpdateAllViews
TRACE("Entering CEx16cView::OnUpdate\n");
m_position = m_pList->GetHeadPosition();
GetEntry(m_position); // initial data for view
}
void CEx16cView::ClearEntry()
{
m_strName = "";
m_nGrade = 0;
UpdateData(FALSE);
((CDialog*) this)->GotoDlgCtrl(GetDlgItem(IDC_NAME));
}
void CEx16cView::GetEntry(POSITION position)
{
if (position) {
CStudent* pStudent = m_pList->GetAt(position);
m_strName = pStudent->m_strName;
m_nGrade = pStudent->m_nGrade;
}
else {
ClearEntry();
}
UpdateData(FALSE);
}
void CEx16cView::InsertEntry(POSITION position)
{
if (UpdateData(TRUE)) {
// UpdateData returns FALSE if it detects a user error
CStudent* pStudent = new CStudent;
pStudent->m_strName = m_strName;
pStudent->m_nGrade = m_nGrade;
m_position = m_pList->InsertAfter(m_position, pStudent);
}
}
void CEx16cView::OnBnClickedClear()
{
TRACE("Entering CEx16cView::OnBnClickedClear\n");
ClearEntry();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -