Listing 8

// Add to SCROLL.CPP
long max(long a, long b)
{
  if (a > b) return a; else return b;
}

// Add to SCROLL.CPP
long min(long a, long b)
{
  if (a < b) return a; else return b;
}

// Replace SCROLL.CPP's Paint() with this
void TScrollWin::Paint(HDC PaintDC, PAINTSTRUCT _FAR &ps)
{
  HBRUSH oldBrush;         // Saved DC brush
  int x1, y1, x2, y2;      // Ellipse coordinates
  int row, col;            // Row and column numbers
  int startRow, endRow;    // Rows in update region

  startRow = (int)max(0,
    1 + (Scroller->YPos + ps.rcPaint.top / Scroller->YUnit - 1));
  endRow = (int)min(NUMROWS,
    1 + (Scroller->YPos + ps.rcPaint.bottom / Scroller->YUnit));
  for (row = startRow; row < endRow; row++) {
    for (col = 0; col < NUMCOLS; col++) {
      GetCoords(row, col, x1, y1, x2, y2);
      oldBrush = SelectObject(PaintDC, colBrushes[col]);
      Rectangle(PaintDC, x1, y1, x2, y2);
      SelectObject(PaintDC, rowBrushes[row]);
      Ellipse(PaintDC, x1 + 4, y1 + 4, x2 - 4, y2 - 4);
      SelectObject(PaintDC, oldBrush);
    }
  }
}
