Move - worked example
key : purple - original code / blue - new code
Step 1 :
Continue with the program names - simpy edit the previous code.
Step 2 :
Add back and forward buttons to the FormView. The following code within the handler functions should be added :
void
CFormNameView::OnBack()
{
((CNamesDoc*)m_pDocument)->MoveCurrentBack();
}
void
CFormNameView::OnForward()
{
((CNamesDoc*)m_pDocument)->MoveCurrentForward();
}
Step 3 :
Add MoveCurrentBack() and MoveCurrentForward() functions to the Document class, (these should have public access) :
void
CNamesDoc::MoveCurrentBack()
{
if( !m_NameList.IsEmpty() )
{m_NameList.GetPrev(m_nCurrentPosition);
if( m_nCurrentPosition == NULL )
{AfxMessageBox("End of list reached");
m_nCurrentPosition = m_NameList.GetHeadPosition();}
CName * pCurrentName = m_NameList.GetAt(m_nCurrentPosition);
UpdateAllViews(NULL,HINT_CURRENT_NAME,pCurrentName);}
}
void
CNamesDoc::MoveCurrentForward()
{
if( !m_NameList.IsEmpty() )
{m_NameList.GetNext(m_nCurrentPosition);
if( m_nCurrentPosition == NULL )
{AfxMessageBox("End of list reached");
m_nCurrentPosition = m_NameList.GetTailPosition();}
CName * pCurrentName = m_NameList.GetAt(m_nCurrentPosition);
UpdateAllViews(NULL,HINT_CURRENT_NAME,pCurrentName);}
}
Step 4:
Define the hint within the Document's header :
#include
<afxtempl.h>
#include "Name.h"
const
int HINT_CURRENT_NAME = 1;
Step 5:
Edit the OnDraw() code within the standard view to allow the current item to be displayed in a different colour :
while(pos!=NULL)
{
CName * pName = ((CNamesDoc*)m_pDocument)->GetNextItem(pos);
// to change the colour of the current item
if( pName == ((CNamesDoc*)m_pDocument)->GetCurrentItem() )
{pDC->SetTextColor(RGB(200, 0, 0));
}
pDC->TextOut(ptText.x, ptText.y, pName->GetFullName());
ptText.y += nLineHeight;
// to change back the previous items to black
pDC->SetTextColor(RGB(0, 0, 0));
}
Click here for the basic exercise
Click here for the source code
Click here to return to menu