/* static.cpp   Creating static child window controls */

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

HINSTANCE hIn;     /* global hInstance for use in creating a child window */

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                             LPSTR lpszCmdLine, int nCmdShow)
{
    HWND        hWnd ;      /* the window's "handle" */
    MSG         msg ;       /* a message structure */
    WNDCLASS    wndclass ;  /* window class structure */

    hIn = hInstance;   /* app's running instance--a global */

        wndclass.style          = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc    = WndProc ;
        wndclass.cbClsExtra     = 0 ;
        wndclass.cbWndExtra     = 0 ;
        wndclass.hInstance      = hInstance ;
        wndclass.hIcon          = LoadIcon (hInstance, "MYICON") ;
        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", "Static Control Example",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hWnd, nCmdShow) ;
    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  HINSTANCE  hStaticText, hStaticIcon, hStaticRect ;
    HINSTANCE          hInstance ;

    switch (wMessage)       /* process windows messages */
    {
        case WM_CREATE:     /* program is just starting */
              /* get the instance handle */
              hInstance = (HINSTANCE) GetWindowLong (hWnd, GWL_HINSTANCE) ;
  
              /* create three static controls--3 ways of getting hInstance */

              hStaticText = CreateWindow ("STATIC","This is static text.",
                  WS_CHILD | SS_CENTER, 10, 10, 130, 60,
                  hWnd, NULL, hInstance, NULL) ;

              hStaticIcon = CreateWindow ("STATIC", "MYICON",
                  WS_CHILD | SS_ICON, 150, 10, 0, 0, /*Myicon size used*/
                  hWnd, NULL, hIn, NULL) ; /* use global hIn */

              hStaticRect = CreateWindow ("STATIC", "",
                  WS_CHILD |  SS_GRAYRECT, 10, 80, 200, 30,
                  hWnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
  
              /* display the controls */
              ShowWindow (hStaticText, SW_SHOWNORMAL) ;
              ShowWindow (hStaticIcon, SW_SHOWNORMAL) ;
              ShowWindow (hStaticRect, SW_SHOWNORMAL) ;
              break ;

        case WM_COMMAND:
            switch (LOWORD(wParam))     /* menu items */
            {
                case IDM_CHANGE:    /* change static text */
                    SetWindowText (hStaticText,
                        "New Text Via SetWindowText().") ;
                    break ;

                case IDM_SEND:      /* change static text */
                    SendMessage (hStaticText, WM_SETTEXT, 0,
                        (LPARAM)"New Text Via SendMessage().") ;
                    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) ;
}