/* scroll2.cpp    scrollbar attached to a window */

#include <windows.h>
#include "scroll2.h"

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                          LPSTR lpszCmdLine, int nCmdShow)
{
    HWND        hWnd ; 
    MSG         msg ; 
    WNDCLASS    wndclass ; 

        wndclass.style          = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc    = WndProc ;
        wndclass.cbClsExtra     = 0 ;
        wndclass.cbWndExtra     = 0 ;
        wndclass.hInstance      = hInstance ;
        wndclass.hIcon          = NULL ;
        wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName   = "MYMENU" ;
        wndclass.lpszClassName  = "MyClass" ;
        if (!RegisterClass (&wndclass))
            return 0 ;

    hWnd = CreateWindow ("MyClass", "Scrollbar Control Example 2", 
        WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hWnd, nCmdShow) ; 
    
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    return 0 ;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                                    WPARAM wParam, LPARAM lParam)
{
    static  int     nScrollPos = 0 ;
    HDC             hDC ;
    PAINTSTRUCT     ps ;
    
    switch (wMessage)       /* process windows messages */
    {
        case WM_CREATE:
            SetScrollRange (hWnd, SB_VERT, 0, MAXSCROLL, FALSE) ;
            SetScrollPos (hWnd, SB_VERT, 0, TRUE) ;
            break ;
 
       case WM_VSCROLL:    /* scroll bar was activated */

            switch (LOWORD(wParam)) /* check what part was pressed */
            {
                case SB_THUMBPOSITION:  /* user movded scroll thumb */
                    nScrollPos = HIWORD (wParam) ;
                    break ;
                case SB_LINEDOWN:       /* user clicked bottom arrow */
                    nScrollPos++ ;
                    nScrollPos = nScrollPos > MAXSCROLL ? MAXSCROLL : nScrollPos ;
                    break ;
                case SB_LINEUP:         /* user clicked top arrow */
                    nScrollPos-- ;
                    nScrollPos = nScrollPos < 0 ? 0 : nScrollPos ;
                    break ;
                case SB_PAGEDOWN:       /* user clicked bottom area */
                    nScrollPos += 10 ;
                    nScrollPos = nScrollPos > MAXSCROLL ? MAXSCROLL : nScrollPos ;
                    break ;
                case SB_PAGEUP:         /* user clicked upper area */
                    nScrollPos -= 10 ;
                    nScrollPos = nScrollPos < 0 ? 0 : nScrollPos ;
                    break ;
            }
            InvalidateRect (hWnd, NULL, TRUE) ; /* force a WM_PAINT */
                        /* move thumb, change static text */
            SetScrollPos (hWnd, SB_VERT, nScrollPos, TRUE) ;
            break ;

        case WM_PAINT:
            hDC = BeginPaint (hWnd, &ps) ;
            TextOut (hDC, 0, nScrollPos, "Line 1", 6) ;
            TextOut (hDC, 0, 50 + nScrollPos, "Line 2", 6) ;
            TextOut (hDC, 0, 100 + nScrollPos, "Line 3", 6) ;
            EndPaint (hWnd, &ps) ;
            break ;

        case WM_COMMAND:        /* menu item selected */
            switch (LOWORD(wParam))
            {
                case IDM_QUIT:
                    DestroyWindow (hWnd) ;  /* destroy window, */
                    break ;     /* terminating application */
            }
            break ;

        case WM_DESTROY:    /* stop application */
            PostQuitMessage (0) ;
            break ;

        default:        /* default windows message processing */
            return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
    }
    return (0) ;
}