Available in LEADTOOLS Imaging Pro, Vector, Document, and Medical Imaging toolkits. |
Implementing Scrollbars: Step 3
Here is some sample code that you could use (the CalcClientMetrics function is defined below):
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);
/*---[CalcClientMetrics]-------------------------------------------------------
Parameters: HWND hWnd Window handle
int *nClientWidth Width in and width out
int *nClientHeight Height in and height out
int *nRightBarThickness Resulting thickness of the Y scrollbar
int *nBottomBarThickness Resulting thickness of the X scrollbar
ProtoType: EZFunc.h
Notes: Use this function to interpret the client area width and
height parameters from the WM_SIZE message. The incoming
parameters attempt to give us the correct client area, excluding
scrollbars. But, because while processing this message we
show or hide the scrollbars, the incoming parameters often are
not correct. Therefore, we must get the true client area
dimensions, including scrollbars, and get the thickness of
scrollbars. The caller can then make its own decisions about
the size of the client area.
--------------------------------------------------------------------------*/
void CalcClientMetrics (HWND hWnd,
int *nClientWidth,
int *nClientHeight,
int *nRightBarThickness,
int *nBottomBarThickness)
{
DWORD dwStyle, dwVersion;
int cxClient, cyClient, cxScroll, cyScroll;
/* Initialize local variables */
cxClient = *nClientWidth;
cyClient = *nClientHeight;
cxScroll = GetSystemMetrics(SM_CXVSCROLL);
cyScroll = GetSystemMetrics(SM_CYHSCROLL);
dwStyle = GetWindowLong(hWnd, GWL_STYLE);
/* Adjust for the difference in how newer WIN32 versions report
scrollbar thickness when there is a window border */
#ifdef WIN32
{
dwVersion = GetVersion();
if (LOBYTE(LOWORD(dwVersion)) >= 4)
{
cxScroll += GetSystemMetrics(SM_CXBORDER);
cyScroll += GetSystemMetrics(SM_CYBORDER);
}
}
#endif
/* If there is a window border, subtract it from the scrollbar thickness
*/
if (dwStyle & WS_BORDER)
{
cxScroll -= GetSystemMetrics(SM_CXBORDER);
cyScroll -= GetSystemMetrics(SM_CYBORDER);
}
/* Add the scrollbar thickness to the client area */
if (dwStyle & WS_VSCROLL)
cxClient += cxScroll;
if (dwStyle & WS_HSCROLL)
cyClient += cyScroll;
/* Update the caller's variables */
*nClientWidth = cxClient;
*nClientHeight = cyClient;
*nRightBarThickness = cxScroll;
*nBottomBarThickness = cyScroll;
}