Implementing Scrollbars: Step 7

In the MainWndProc function, add code to process WM_HSCROLL and WM_VSCROLL messages.

   case WM_HSCROLL:
      /* Udpate the global rClientSize rectangle */
      GetClientRect(hWnd,&rClientSize);
      /* See how much the user wants to scroll */
      switch (LOWORD(wParam))
      {
      /* Decrement by 10 percent */
      case SB_LINELEFT:
         nScrollInc = - XScrollRange / 10;
         break;
      /* Increment by 10 percent */
      case SB_LINERIGHT:
         nScrollInc = XScrollRange / 10;
         break;
      /* Decrement by client size */
      case SB_PAGELEFT:
         nScrollInc = - rClientSize.right;
         break;
      /* Increment by client size */
      case SB_PAGERIGHT:
         nScrollInc = rClientSize.right;
         break;
      /* Calculate the change in the THUMB position */
      case SB_THUMBPOSITION:
         nScrollInc = HIWORD(wParam) - XScrollPosition;
         break;
      /* No increment; so exit */
      default:
         return (0);
      }
      /* Limit the scroll increment to the amount available */
      if (nScrollInc > 0)
         nScrollInc = min(nScrollInc, (XScrollRange - XScrollPosition));
      else
         nScrollInc = max(nScrollInc, -XScrollPosition);
      /* Update the global display rectangle */
      OffsetRect (&rLeadDest,-nScrollInc, 0);
      /* Scroll the client area */
      ScrollWindow (hWnd,-nScrollInc, 0, NULL, NULL);
      /* Calculate the new scroll position */
      XScrollPosition += nScrollInc;
      /* Prevent a background erase of the area uncovered by scrolling */
      if (nScrollInc > 0)
      {
         SetRect(&ScrollRect, rClientSize.right - nScrollInc, 0,
            rClientSize.right, rClientSize.bottom);
      }
      else
      {
         SetRect(&ScrollRect, 0, 0, -nScrollInc, rClientSize.bottom);
      }
      ValidateRect(hWnd, &ScrollRect);
      InvalidateRect(hWnd, &ScrollRect, FALSE);
      /* Set the new scroll position */
      SetScrollPos (hWnd, SB_HORZ, XScrollPosition, TRUE);
      /* Update the window */
      UpdateWindow (hWnd);
      return (0);
   case WM_VSCROLL:
      /* Udpate the global rClientSize rectangle */
      GetClientRect(hWnd,&rClientSize);
      /* See how much the user wants to scroll */
      switch (LOWORD(wParam))
      {
      /* Decrement by 10 percent */
      case SB_LINEUP:
         nScrollInc = - YScrollRange / 10;
         break;
      /* Increment by 10 percent */
      case SB_LINEDOWN:
         nScrollInc = YScrollRange / 10;
         break;
      /* Decrement by client size */
      case SB_PAGEUP:
         nScrollInc = - rClientSize.bottom;
         break;
      /* Increment by client size */
      case SB_PAGEDOWN:
         nScrollInc = rClientSize.bottom;
         break;
      /* Calculate the change in the THUMB position */
      case SB_THUMBPOSITION:
         nScrollInc = HIWORD(wParam) - YScrollPosition;
         break;
      /* No increment; so exit */
      default:
         return (0);
      }
      /* Limit the scroll increment to the amount available */
      if (nScrollInc > 0)
         nScrollInc = min(nScrollInc, (YScrollRange - YScrollPosition));
      else
         nScrollInc = max(nScrollInc, -YScrollPosition);
      /* Update the global display rectangle */
      OffsetRect (&rLeadDest, 0, -nScrollInc);
      /* Scroll the client area */
      ScrollWindow (hWnd, 0, -nScrollInc, NULL, NULL);
      /* Prevent a background erase of the area uncovered by scrolling */
      if (nScrollInc > 0)
      {
         SetRect(&ScrollRect, 0, rClientSize.bottom - nScrollInc,
            rClientSize.right, rClientSize.bottom );
      }
      else
      {
         SetRect(&ScrollRect, 0, 0, rClientSize.right,-nScrollInc);
      }
      ValidateRect(hWnd, &ScrollRect);
      InvalidateRect(hWnd, &ScrollRect, FALSE);
      /* Calculate the new scroll position */
      YScrollPosition += nScrollInc;
      /* Set the new scroll position */
      SetScrollPos (hWnd, SB_VERT, YScrollPosition, TRUE);
      /* Update the window */
      UpdateWindow (hWnd);
      return (0);