/* print2.cpp    Output to printer using LOMETRIC mapping mode */

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

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 */
    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  = (HBRUSH)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 ;
    HINSTANCE       hInstance ;
    char            szPrinter [64], *szDriver, *szDevice, *szOutput ;
    static DOCINFO  di = { sizeof(DOCINFO), "Print2: 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) ;
}


/* OutputStuff() -- sets DC mapping mode, attributes, moves paper/screen, 
and prints text, a surrounding rectangle, and an ellipse */

void OutputStuff (HDC hDC, HANDLE hInstance)
{
    char    cBuf [128] ;
    SIZE    textsize ;
    HBRUSH  hOldBrush, hNewBrush ;
   
    SetMapMode (hDC, MM_LOMETRIC) ;  /* change map mode */
    SetWindowOrgEx (hDC,0,150,NULL); /* move screen/paper up 15 mm. */
        /* use null brush so center of rectangle is transparent */
    SelectObject (hDC, GetStockObject (NULL_BRUSH)) ;
        /* align characters based on bottom left */
    SetTextAlign (hDC, TA_LEFT | TA_BOTTOM) ;

    MoveToEx (hDC, -30,0, NULL);    /* coordinate system x-axis */
    LineTo (hDC, 30,0);
    MoveToEx(hDC, 0,-30, NULL);    /* coordinate system y-axis */
    LineTo (hDC, 0,30);

    LoadString (hInstance, S_OUTTEXT, cBuf, sizeof (cBuf)) ;
    TextOut (hDC, 100, 50, cBuf, lstrlen (cBuf)) ;

    GetTextExtentPoint (hDC, cBuf, lstrlen (cBuf), &textsize) ;
    Rectangle (hDC, 100, 50, textsize.cx + 100, textsize.cy + 50) ;

    hNewBrush = CreateSolidBrush (RGB (0, 0, 0)) ;   /* black*/
    hOldBrush = SelectObject (hDC, hNewBrush) ;
    Ellipse (hDC, 100, 50, 200, -50) ;
    SelectObject (hDC, hOldBrush) ;
    DeleteObject (hNewBrush) ;    
}