Win32 scrolling examples

Christopher picture Christopher · May 2, 2010 · Viewed 7.3k times · Source

Could anyone point me to (or provide?) some nice, clear examples of how to implement scrolling in Win32? Google brings up a lot of stuff, obviously, but most examples seem either too simple or too complicated for me to be sure that they demonstrate the right way of doing things. I use LispWorks CAPI (cross-platform Common Lisp GUI lib) in my current project, and on Windows I have a hard-to-figure-out bug relating to scrolling; basically I want to do some tests directly via the Win32 API to see if I can shed some light on the situation.

Many thanks, Christopher

Answer

bkausbk picture bkausbk · Jan 17, 2011

I think you are talking for an example how to handle WM_VSCROLL/WM_HSCROLL event. If so first step is to handle that event. You shouldn't use the HIWORD(wParam) value of that call but use GetScrollInfo, GetScrollPos, and GetScrollRange functions instead.

Following is an example code snipped by MSDN - Using Scroll Bars. xCurrentScroll is determined before by calling GetScrollPos() for example.

int xDelta;     // xDelta = new_pos - current_pos  
int xNewPos;    // new position 
int yDelta = 0; 

switch (LOWORD(wParam)) { 
    // User clicked the scroll bar shaft left of the scroll box. 
    case SB_PAGEUP: 
        xNewPos = xCurrentScroll - 50; 
        break; 

    // User clicked the scroll bar shaft right of the scroll box. 
    case SB_PAGEDOWN: 
        xNewPos = xCurrentScroll + 50; 
        break; 

    // User clicked the left arrow. 
    case SB_LINEUP: 
        xNewPos = xCurrentScroll - 5; 
        break; 

    // User clicked the right arrow. 
    case SB_LINEDOWN: 
        xNewPos = xCurrentScroll + 5; 
        break; 

    // User dragged the scroll box. 
    case SB_THUMBPOSITION: 
        xNewPos = HIWORD(wParam); 
        break; 

    default: 
        xNewPos = xCurrentScroll; 
} 

[...]

// New position must be between 0 and the screen width. 
xNewPos = max(0, xNewPos); 
xNewPos = min(xMaxScroll, xNewPos); 

[...]

// Reset the scroll bar. 
si.cbSize = sizeof(si); 
si.fMask  = SIF_POS; 
si.nPos   = xCurrentScroll; 
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);