/* print1.cpp   example with simple output to printer */

#include <windows.h>
#include <string.h>     /* needed for strtok() function */
#include "print1.h"

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    HWND        hWnd ;      /* the window's "handle" */
    MSG         msg ;       /* a message structure */
    WNDCLASS    wndclass ;  /* window class structure */
    char        cBuf [128] ; /* character buffer */

        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  = GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName   = "MYMENU" ;
        wndclass.lpszClassName  = "MyClass" ;
                        /* register the window class */
        if (!RegisterClass (&wndclass))
            return 0 ;

    LoadString (hInstance, S_PROGRAMCAPTION, cBuf, sizeof (cBuf)) ;

    hWnd = CreateWindow ("MyClass", cBuf, 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)
{
    PAINTSTRUCT     ps ;
    HDC             hDC ;
    HANDLE          hInstance ;
    char            szPrinter [64], *szDriver, *szDevice, *szOutput ;
    static DOCINFO  di = { sizeof(DOCINFO), "Print1: Printing", NULL };
    
    switch (wMessage)       /* process windows messages */
    {
        case WM_COMMAND:
            switch (LOWORD(wParam))             /* menu items */
            {
                case IDM_PRINT:                 /* Output to printer */
                    hInstance =(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
                            /* parse win.ini line to get printer info */
                    GetProfileString ("windows","device","",szPrinter,64);
                    szDevice = strtok (szPrinter, ",");
                    szDriver = strtok (NULL, ",");
                    szOutput = strtok (NULL, ",");
                            /* create a device context for the printer */
                    hDC = CreateDC (szDriver, szDevice, szOutput, NULL);
                    if (StartDoc (hDC, &di) > 0)        /* Output to printer */
                        if (StartPage (hDC) > 0)
                        {
                            OutputStuff (hDC, hInstance);
                            if (EndPage (hDC) > 0)
                                 EndDoc(hDC);
                        }
                    DeleteDC (hDC);
                    break;   

                case IDM_QUIT:                  /* "Quit" menu item */
                    DestroyWindow (hWnd);       /* destroy window, */
                    break;                      /* terminating application*/
            }
            break;
 
       case WM_PAINT:
            BeginPaint (hWnd, &ps) ;
            hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
            OutputStuff (ps.hdc, hInstance) ;   /* do output to screen */
            EndPaint (hWnd, &ps) ;
            break ;

        case WM_DESTROY:                    /* stop application */
            PostQuitMessage (0) ;
            break ;

        default:        /* default windows message processing */
            return (DefWindowProc (hWnd, wMessage, wParam, lParam)) ;
    }
    return (0) ;
}


/* output text and rectangle to dc */

void OutputStuff (HDC hDC, HANDLE hInstance)
{
    char cBuf [128] ;
    
    LoadString (hInstance, S_OUTTEXT, cBuf, sizeof (cBuf)) ;
    TextOut (hDC, 0, 0, cBuf, lstrlen (cBuf)) ;
    Rectangle (hDC, 0, 40, 200, 100) ;
}