'Win32 scrolling examples

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



Solution 1:[1]

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);

Solution 2:[2]

Here's one, ScrollCall, (copy from page):.

ScrollCall is a demo program that takes a sample of Windows standard controls, along with a standard GDI image, and arranges them on a Device Context (or DC), in a window. Depending on the dimensions of the image, and the size of the containing window, horizontal and/or system scrollbars become visible, to enable scrolling for the image and controls. Thus ScrollCall is as at least as much focused on sizing as it is scrolling, and both offer unique challenges for the programmer.

ScrollCall features:

  • System scroll bars
  • Optional groupbox
  • Button to open images on the Device Context (DC)
  • Radio options for choice of window scroll function
  • Checkbox to stretch rather than scroll the image
  • Label Paint Mult with UpDown and Buddy to increase the wait times of WM_SIZE during sizing, thus reduced WM_PAINT processing
  • Right click for system snapshot of view in default or monitor attached to desktop
  • Double-click to print the visible part of the (mostly empty) client window to the DC, and back to the client window (experimental)
  • ScrollCall temporarily turns on SPI_SETDRAGFULLWINDOWS for the testing of the visual effects of dragging, if ever it was toggled off
  • Compatibility with AeroSnap sizing

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bkausbk
Solution 2 Laurie Stearn