The vertical and horizontal scrollbars are using the same variable for the scroll position (iHscrollpos). Check out my comments in blue.
Code:
LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static RECT rect;
static int iHscrollPos, iVscrollPos, cxChar, cyChar,cxCaps, cxClient, cyClient;
HCURSOR hCursor ;
PAINTSTRUCT ps ;
SCROLLINFO si;
TEXTMETRIC tm ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
SetScrollRange (hwnd, SB_HORZ, 0, 100, FALSE);
SetScrollPos (hwnd, SB_HORZ, iHscrollPos, TRUE);
//horiz position is set, but what about vertical?
ReleaseDC (hwnd, hdc) ;
return 0;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0;
case WM_HSCROLL:
switch(LOWORD(wParam))
{
case SB_LINERIGHT:
iHscrollPos += 1;
break;
case SB_LINELEFT:
iHscrollPos -= 1;
break;
case SB_THUMBPOSITION:
iHscrollPos = HIWORD (wParam);
break;
default:
break;
}
iHscrollPos = max(0, min(iHscrollPos,100));
if(iHscrollPos != GetScrollPos(hwnd, SB_HORZ))
{
SetScrollPos(hwnd, SB_HORZ, iHscrollPos, TRUE);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_VSCROLL: //to get all the vertical scroll bar information
switch (LOWORD (wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break;
case SB_BOTTOM:
si.nPos = si.nMax ;
break;
case SB_LINEUP:
//iHscrollPos -= 1;
si.nPos -= 1 ;
break;
case SB_LINEDOWN:
//iHscrollPos += 1;
si.nPos += 1 ;
break;
case SB_PAGEUP:
si.nPos -= si.nPage ;
break ;
case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;
default:
break ;
}
si.fMask = SIF_POS ;
iHscrollPos = max(0, min(iHscrollPos,100));
if(iHscrollPos != GetScrollPos(hwnd, SB_VERT))
{
SetScrollPos(hwnd, SB_VERT, iHscrollPos, TRUE);
//why is the vert using the horiz variable?
//This is probably why they run in parallel to each other
InvalidateRect(hwnd,NULL,TRUE);
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}