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.
LRESULT WINAPI MainWndProc (HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { static BOOL fSizeInUse=FALSE; /* Flag to avoid processing multiple WM_SIZE messages */ switch (Message) { HANDLE_MSG (hWnd, WM_CREATE, Window_OnCreate); HANDLE_MSG (hWnd, WM_TIMER, Window_OnTimer); HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand); HANDLE_MSG (hWnd, WM_PALETTECHANGED, Window_OnPaletteChanged); HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE, Window_OnQueryNewPalette); HANDLE_MSG (hWnd, WM_ACTIVATE, Window_OnActivate); HANDLE_MSG (hWnd, WM_PALETTEISCHANGING, Window_OnPaletteChanging); HANDLE_MSG (hWnd, WM_SYSCOLORCHANGE, Window_SysColorChange); HANDLE_MSG (hWnd, WM_PAINT, Window_OnPaint); HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy); 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) || (!Data.BitmapHandle.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 - DisplayWidth; /* 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); } return DefWindowProc (hWnd, Message, wParam, lParam); }