/* edit1.cpp   creating and using an edit control */

#include <windows.h>
#include "edit1.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 = "edit1" ;
          if (!RegisterClass (&wndclass))
              return FALSE ;

    hWnd = CreateWindow ("edit1", "Edit 1", 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  HWND    hEdit ;
    HINSTANCE       hInstance ;
    char            cBuf []= "                                      " ;
    int             i, nLines, nLineIndex, nLineLong ;
    HDC             hDC ;

    switch (wMessage)       /* process windows messages */
    {
        case WM_CREATE:     /* make edit control here */
            hInstance =(HINSTANCE) GetWindowLong (hWnd, GWL_HINSTANCE) ;
            hEdit = CreateWindow ("EDIT", "",
                    WS_CHILD | ES_MULTILINE | WS_VSCROLL | WS_VISIBLE |
                    ES_AUTOVSCROLL | WS_BORDER,
                    10, 10, 150, 100, hWnd, (HMENU)EDIT_ID, hInstance, NULL) ;
            break ;

        case WM_COMMAND:    /* menu item or control selected */
            switch (LOWORD(wParam))
            {
                case IDM_GET:       /* show the edit control's text */
                                    /* find out the number of lines */
                    nLines = (int)SendMessage(hEdit,EM_GETLINECOUNT,0,0);
                    hDC = GetDC (hWnd) ;
                    for (i = 0 ; i < nLines ; i++)  /* for each line */
                    {
                                    /* copy line of text to cBuf[] */
                        SendMessage (hEdit, EM_GETLINE, i, (LPARAM)cBuf) ;

                                    /* get the char pos of start of line */
                        nLineIndex = (int) SendMessage (hEdit, EM_LINEINDEX,
                                                          i, 0) ;

                                    /* get the length of the line */
                        nLineLong = (int) SendMessage (hEdit, EM_LINELENGTH,
                                                          nLineIndex, 0) ;

                                    /* display the line below edit cntl */
                        TextOut (hDC, 10, 130 + (18 * i), cBuf, nLineLong) ;

                    }
                    ReleaseDC (hWnd, hDC) ;
                    break ;

                case IDM_QUIT:
                    DestroyWindow (hWnd) ;  /* destroy window, */
                    break ;     /* terminating application */
            }
            break ;

        case WM_DESTROY: 
            PostQuitMessage (0) ;
            break ;

        default: 
            return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
    }
    return (0) ;
}