/* dlgexample.cpp, uses global variables to get data from */
/* a dialog box with an edit control     */

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

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
                    WPARAM wParam, LPARAM lParam);

BOOL CALLBACK MyDlgProc (HWND hDlg, UINT wMessage,
                        WPARAM wParam, LPARAM lParam);

/* Global variables */
    char name[]="                    ";    /* name entered by user */

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 */

    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" ;
            /* register the parent window class */
    if (!RegisterClass (&wndclass))
        return (0) ;

    hWnd = CreateWindow ("MyClass","Dialog Box Example 2",
    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))   /* message loop */
    {
        TranslateMessage (&msg) ;   /* translate keyboard messages */
        DispatchMessage (&msg) ;    /* send message to WndProc() */
    }
    return (msg.wParam) ;
}


LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
                    WPARAM wParam, LPARAM lParam)
{
    HINSTANCE       hInstance ;
    int             nRetVal = 0 ;
    char            cBuf[30];    
    /* A blank string to erase strings displayed earlier */
    char            blanks[]="                                                  ";
    HDC                hDC;
    
    switch (wMessage)           /* process windows messages */
    {
    case WM_COMMAND:            /* menu items */
        switch (LOWORD(wParam))
        {
        case ID_GETNAME:     /* Run the information dialog procedure */
            hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
            nRetVal = DialogBox (hInstance,"MYDLG",hWnd,(DLGPROC)MyDlgProc) ;  // start dlg box
            hDC=GetDC(hWnd);  /* Get a DC to write on */
            if (nRetVal==1)   /* User made a choice */
            {    
                //lstrcat(cBuf,0);    /* Null terminate the buffer */  
                TextOut(hDC,10,10,"Name: ",6);
                TextOut(hDC,60,10,blanks,lstrlen(blanks));
                TextOut(hDC,60,10,name,lstrlen(name));
            }
            else     /* User made no choice */
            {
                TextOut(hDC,10,10,blanks,lstrlen(blanks));
                TextOut(hDC,10,50,blanks,lstrlen(blanks));
                TextOut(hDC,10,10,"No response!",12);
            }
            ReleaseDC(hWnd,hDC);
            break ;
        }
        break ;

    case WM_DESTROY:            /* stop application */
        PostQuitMessage (0) ;
        break ;
    default:            /* default windows message processing */
        return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
    }
    return (0) ;
}

/* The information-retrieving dialog box procedure */
BOOL CALLBACK MyDlgProc (HWND hDlg, UINT wMessage,
                            WPARAM wParam, LPARAM lParam)
{
    char    cBuf[] = "                   ";
    int        i;
    
    switch (wMessage)       /* process messages */
    {
    case WM_INITDIALOG:
        SendDlgItemMessage(hDlg, IDC_STATIC1, WM_SETTEXT, 0, (LPARAM)"Enter name");
        SendDlgItemMessage(hDlg, IDC_EDIT1, WM_SETTEXT, 0, (LPARAM)"");
        return (TRUE);
    case WM_COMMAND:    /* controls in dialog box activated */
        switch (LOWORD(wParam))
        {
        case IDOK:                    /* OK button */
            SendDlgItemMessage (hDlg,IDC_EDIT1,EM_GETLINE,0,(LPARAM)cBuf);
            lstrcpy(name,cBuf);
            EndDialog (hDlg, 1) ;    /* exit the dialog box */
            return (TRUE) ;
        case IDCANCEL:                /* Cancel button */
            EndDialog (hDlg, 0) ;   /* exit the dialog box */
            return (TRUE) ;
        }
        break ;
    }
}