/* scroll1.cpp  Creating a standalone child window scroll bar control */

#include <windows.h>
#include "scroll1.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 1", 
        WS_OVERLAPPEDWINDOW, 
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
        NULL, NULL, hInstance, NULL) ;
 
    ShowWindow (hWnd, nCmdShow) ;   /* display the window */    
    UpdateWindow (hWnd) ;
 
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ; 
        DispatchMessage (&msg) ;
    }
    return (msg.wParam) ;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                    WPARAM wParam, LPARAM lParam)
{
    static  HWND    hScroll, hStatic ;
    HINSTANCE       hInstance ;
    static  int     nScrollPos ;
    char            cBuf [256], cStaticBuf [128] ; /* to get/display...*/
                                                   /* SB thumb position */    
    switch (wMessage)       /* process windows messages */
    {
        case WM_CREATE:     /* application just starting up */
                hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
                    /* create the scroll bar control */
                hScroll = CreateWindow ("SCROLLBAR", "", 
                                        WS_CHILD | WS_VISIBLE | SBS_HORZ,
                                        80, 20, 180, 30,
                                        hWnd, (HMENU)SCROLL_ID, hInstance, NULL) ;

                    /* initialize scroll bar and display */
                SetScrollRange (hScroll, SB_CTL, 0, MAXSCROLL, FALSE) ;
                nScrollPos = MAXSCROLL/2 ;
                SetScrollPos (hScroll, SB_CTL, nScrollPos, FALSE) ;
                ShowScrollBar (hScroll, SB_CTL, SW_SHOW) ;

                    /* create static control to display int value */
                hStatic = CreateWindow ("STATIC", "",
                    WS_CHILD | SS_CENTER,
                    10, 20, 50, 40,
                    hWnd, (HMENU)STATIC_ID, hInstance, NULL) ;
                wsprintf (cBuf, "%d", nScrollPos) ;
                SetWindowText (hStatic, cBuf) ;
                ShowWindow (hStatic, SW_SHOWNORMAL) ;
                break ;

        case WM_HSCROLL:    /* 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 down (right) arrow */
                    nScrollPos++ ;
                    nScrollPos = nScrollPos > MAXSCROLL ? MAXSCROLL : nScrollPos ;
                    break ;
                case SB_LINEUP:         /* user clicked up (left) arrow */
                    nScrollPos-- ;
                    nScrollPos = nScrollPos < 0 ? 0 : nScrollPos ;
                    break ;
                case SB_PAGEDOWN:       /* user clicked down (right) area */
                    nScrollPos += 10 ;
                    nScrollPos = nScrollPos > MAXSCROLL ? MAXSCROLL : nScrollPos ;
                    break ;
                case SB_PAGEUP:         /* user clicked up (right) area */
                    nScrollPos -= 10 ;
                    nScrollPos = nScrollPos < 0 ? 0 : nScrollPos ;
                    break ;
            }
                        /* move thumb, change static text */
            SetScrollPos (hScroll, SB_CTL, nScrollPos, TRUE) ;
            wsprintf (cBuf, "%d", nScrollPos) ;
            SetWindowText (hStatic, cBuf) ;
            break ;

        case WM_COMMAND:    /* menu item or control selected */
            switch (LOWORD(wParam))
            {
                case IDM_GET:   /* show the static control's text */
                    GetWindowText (hStatic, cStaticBuf, 128) ;
                    wsprintf (cBuf, "The static control contains: %s.",
                              cStaticBuf) ;
                    MessageBox (hWnd, cBuf, "Message", 
                        MB_ICONINFORMATION | MB_OK) ;
                    break ;
                                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) ;
}