6.9 Add a Member Function to Handle the WM_PAINT Message

This section explains the eleventh step in writing Phone Book: add a message-handler function to handle the WM_PAINT member function.

·To add the OnPaint message-handler:

Add the following OnPaint message-handler function to the CMainWindow section of your VIEW.CPP file below the OnKeyDown message handler and above Save:

// CMainWindow::OnPaint

// This routine does all the painting for the screen.

//

void CMainWindow::OnPaint()

{

CPaintDC dc( this );

// Set the Text and background colors for the DC also create a

// Brush

CBrush bBack;

dc.SetTextColor( GetSysColor( COLOR_WINDOWTEXT ) );

dc.SetBkColor( GetSysColor( COLOR_WINDOW ) );

bBack.CreateSolidBrush( GetSysColor( COLOR_WINDOW ) );

// Compute the lines that need to be redrawn

int nStart = max( 0, m_nVscrollPos + dc.m_ps.rcPaint.top /

m_cyChar - 1 );

int nEnd = min( (int)m_people.GetCount(),

m_nVscrollPos + ( dc.m_ps.rcPaint.bottom / m_cyChar+1 ) );

// Create a rect the width of the display.

CRect area( 0, 0, m_cxClient, 0 );

CString szDisplay;

CPerson* pCurrent;

int x,y;

for ( ;nStart < nEnd; nStart++ )

{

// if the current line is the select line then change the

// colors to the highlight text colors.

if ( m_nSelectLine == nStart )

{

bBack.DeleteObject();

bBack.CreateSolidBrush( GetSysColor( COLOR_HIGHLIGHT ) );

dc.SetTextColor( GetSysColor( COLOR_HIGHLIGHTTEXT ) );

dc.SetBkColor( GetSysColor( COLOR_HIGHLIGHT ) );

}

// x is the number of characters horz scrolled * the width of

// char. y is the current line no. - number of lines scrolled

// times the height of a line.

x = m_cxChar * ( -m_nHscrollPos );

y = m_cyChar * ( nStart - m_nVscrollPos );

// Set the rect to y and y + the height of the line. Fill the

// rect with the background color.

area.top = y;

area.bottom = y+ m_cyChar;

dc.FillRect( area, &bBack );

// Get the person and build a string with his name.

pCurrent = m_people.GetPerson( nStart );

szDisplay = " " + pCurrent -> GetLastName() + ", " +

pCurrent -> GetFirstName();

// Set the dc to write using the point as the left top of the

// character. Write the name.

dc.SetTextAlign( TA_LEFT | TA_TOP );

dc.TextOut ( x, y,szDisplay, szDisplay.GetLength() );

// Write the phone number right aligned.

szDisplay = pCurrent -> GetPhoneNumber();

dc.SetTextAlign ( TA_RIGHT | TA_TOP );

dc.TextOut ( x + SIZENAME * m_cxCaps, y, szDisplay,

szDisplay.GetLength() );

// Write the time.

szDisplay = pCurrent ->

GetModTime().Format( "%m/%d/%y %H:%M" );

dc.TextOut ( x + ( SIZENAME + SIZEPHONE ) * m_cxCaps, y,

szDisplay, szDisplay.GetLength() );

// If this is the select line then we need to reset the dc

// colors back to the original colors.

if ( m_nSelectLine == nStart )

{

bBack.DeleteObject();

bBack.CreateSolidBrush( GetSysColor( COLOR_WINDOW ) );

dc.SetTextColor( GetSysColor( COLOR_WINDOWTEXT ) );

dc.SetBkColor( GetSysColor( COLOR_WINDOW ) );

}

}

}

OnPaint paints the database's lines of data in the window.

To continue the tutorial, see “Add Utility Member Functions” on page 238. For more information about the OnPaint member function that you just added, see the following discussion.