/* dlg1.cpp   Creating a simple dialog box */

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

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

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

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          = LoadIcon (hInstance, "DIALG1") ;
        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 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))   /* message loop */
    {
        TranslateMessage (&msg) ;   /* translate keyboard messages */
        DispatchMessage (&msg) ;    /* send message to WndProc() */
    }
    return 0 ;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                                        WPARAM wParam, LPARAM lParam)
{
    HINSTANCE      hInstance ;
    
    switch (wMessage)               /* process windows messages */
    {
        case WM_COMMAND:            /* menu items */
            switch (LOWORD(wParam))
            {
                case IDM_DIALOG:    /* run the dialog box procedure */
                    hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
                    DialogBox (hInstance, "FIRSTDIALOG", hWnd, (DLGPROC)DialogProc) ;
                    break ;
                case IDM_QUIT:
                    DestroyWindow (hWnd) ;  /* destory 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) ;
}

/* Dialog box message processing function. */

BOOL CALLBACK DialogProc (HWND hDlg, UINT wMessage, 
                                        WPARAM wParam, LPARAM lParam)
{
    switch (wMessage)       /* process messages */
    {
        case WM_INITDIALOG:
            return (TRUE) ;
        case WM_COMMAND:    /* controls in dialog box activated */
            switch (LOWORD(wParam))
            {
                case IDC_OK:                /* OK button */
                    EndDialog (hDlg, 0) ;   /* exit the dialog box */
                    return (TRUE) ;
                case IDC_NOTOK:             /* Not OK button */
                    MessageBeep (0) ;       /* just beep */
                    return (TRUE) ;   
            }
            break ;
    }
    return (FALSE) ;    /* return FALSE to signify message not processed */
}