Implementing Scrollbars: Step 6
In the MainWndProc function, add code to process WM_SIZE messages. This code shows or hides the scrollbars, as needed, and it adjusts the scrollbar ranges and positions.
case WM_SIZE:
/* Avoid processing other WM_SIZE messages while we are processing this one */
if (fSizeInUse == TRUE)
return (0);
fSizeInUse = TRUE;
/* Turn off scrolling and exit if the window is minimized, or
if there is no bitmap */
if ((wParam == SIZE_MINIMIZED) || (!LeadBitmap.Flags.Allocated))
{
SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
SetScrollRange(hWnd, SB_HORZ, 0, 0, FALSE);
fSizeInUse = FALSE;
return (0);
}
/* Initialize the client height and width using message parameters */
ClientHeight = HIWORD(lParam);
ClientWidth = LOWORD(lParam);
/* Update the values, separating client information from scrollbar information */
CalcClientMetrics (hWnd,
&ClientWidth,
&ClientHeight,
&RightBarThickness,
&BottomBarThickness);
/* Use the offsets of the display to initialize scroll positions */
XScrollPosition = -rLeadDest.left;
YScrollPosition = -rLeadDest.top;
/* Calculate the range and scroll position of the vertical scroll bar */
if (ClientHeight >= DisplayHeight)
{ /* We do not need a vertical scroll bar */
/* Use the full display height, and turn off the scroll bar */
rLeadDest.bottom = DisplayHeight;
rLeadDest.top = 0;
SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
}
else
{ /* We need a vertical scrollbar */
/* The range and position of the vertical scrollbar depend on
whether there is also a horizontal scrollbar */
if (ClientWidth >= DisplayWidth)
{ /* There will not be a horizontal scroll bar */
/* Calculate the range without the horizontal scrollbar */
YScrollRange = DisplayHeight - ClientHeight;
/* If the expected scroll position would leave blank space,
adjust the scroll position and reposition the display rectangle */
if (YScrollPosition > YScrollRange)
{ /* Reposition the display rectangle */
OffsetRect (&rLeadDest, 0, YScrollPosition - YScrollRange);
/* Adjust the scroll position */
YScrollPosition = YScrollRange;
}
}
else
{ /* There will also be a horizontal scroll bar */
/* Calculate the scroll range, including the scrollbar */
YScrollRange = DisplayHeight - ClientHeight + BottomBarThickness;
}
SetScrollRange(hWnd, SB_VERT, 0, YScrollRange, FALSE);
SetScrollPos(hWnd, SB_VERT, YScrollPosition, TRUE);
}
/* Calculate the range and scroll position of the horizontal scroll bar */
if (ClientWidth >= DisplayWidth)
{ /* We do not need a horizontal scroll bar */
/* Use the full display width, and turn off the scroll bar */
rLeadDest.right = DisplayWidth;
rLeadDest.left = 0;
SetScrollRange(hWnd, SB_HORZ, 0, 0, FALSE);
}
else
{ /* We need a horizontal scrollbar */
/* The range and position of the horizontal scrollbar depend on
whether there is also a vertical scrollbar */
if (ClientHeight >= DisplayHeight)
{ /* There will not be a vertical scroll bar */
/* Calculate the range without the vertical scrollbar */
XScrollRange = DisplayWidth - ClientWidth;
/* If the expected scroll position would leave blank space,
adjust the scroll position and reposition the display rectangle */
if (XScrollPosition > XScrollRange)
{ /* Reposition the display rectangle */
OffsetRect (&rLeadDest, XScrollPosition - XScrollRange, 0);
/* Adjust the scroll position */
XScrollPosition = XScrollRange;
}
}
else
{ /* There will also be a vertical scroll bar */
/* Calculate the scroll range, including the scrollbar */
XScrollRange = DisplayWidth - ClientWidth + RightBarThickness;
}
SetScrollRange(hWnd, SB_HORZ, 0, XScrollRange, FALSE);
SetScrollPos(hWnd, SB_HORZ, XScrollPosition, TRUE);
}
fSizeInUse = FALSE;
return (0);