/* devcaps.cpp   demonstrates GetDeviceCaps() function */

#include <windows.h>
#include <string.h>     /* needed for strtok() function */
#include "devcaps.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 ;
    HINSTANCE       hInstance ;
    HDC             hDCprint ;
    char            szPrinter [64], *szDriver, *szDevice, *szOutput;
     
    switch (wMessage)       /* process windows messages */
    {
        case WM_COMMAND:
            switch (wParam)                     /* menu items */
            {
                case IDM_QUIT:                  /* "Quit" menu item */
                    DestroyWindow (hWnd) ;      /* destroy window, */
                    break ;                     /* terminating application */
            }
            break ;

        case WM_PAINT:
                    /* Get a device context for the screen */
            BeginPaint (hWnd, &ps) ;
            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 */
            hDCprint = CreateDC (szDriver, szDevice, szOutput, NULL) ;
                    /* put headings on screen */
            OutStringTable (ps.hdc, hInstance, S_VIDEOTITLE, 100, 0) ;
            OutStringTable (ps.hdc, hInstance, S_PRINTERTITLE, 200, 0) ;
            OutStringTable (ps.hdc, hInstance, S_HORZSIZE, 0, 20 ) ;
            OutStringTable (ps.hdc, hInstance, S_VERTSIZE, 0, 40 ) ;
            OutStringTable (ps.hdc, hInstance, S_HORZRES, 0, 60 ) ;
            OutStringTable (ps.hdc, hInstance, S_VERTRES, 0, 80 ) ;
            OutStringTable (ps.hdc, hInstance, S_BITSPIXEL, 0, 100 ) ;
                    /* put values on screen */
            OutDevCapValue (ps.hdc, ps.hdc, HORZSIZE, 100, 20) ;
            OutDevCapValue (ps.hdc, hDCprint, HORZSIZE, 200, 20) ;
            OutDevCapValue (ps.hdc, ps.hdc, VERTSIZE, 100, 40) ;
            OutDevCapValue (ps.hdc, hDCprint, VERTSIZE, 200, 40) ;
            OutDevCapValue (ps.hdc, ps.hdc, HORZRES, 100, 60) ;
            OutDevCapValue (ps.hdc, hDCprint, HORZRES, 200, 60) ;
            OutDevCapValue (ps.hdc, ps.hdc, VERTRES, 100, 80) ;
            OutDevCapValue (ps.hdc, hDCprint, VERTRES, 200, 80) ;
            OutDevCapValue (ps.hdc, ps.hdc, BITSPIXEL, 100, 100) ; 
            OutDevCapValue (ps.hdc, hDCprint, BITSPIXEL, 200, 100) ;
                    /* get rid of device contexts */         
            DeleteDC (hDCprint) ;
            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) ;
}


    /* helper function to output a string table entry on a DC */

void OutStringTable (HDC hDC, HANDLE hInstance, int nString, 
    int nX, int nY)
{
    char cBuf [128] ;
    
    LoadString (hInstance, nString, cBuf, sizeof (cBuf)) ;
    TextOut (hDC, nX, nY, cBuf, lstrlen (cBuf)) ;
}


    /* helper function to display a device caps value on output DC */
    /* hDCout is DC for output, hDCdata is DC source for data */

void OutDevCapValue (HDC hDCout, HDC hDCdata, int nIndex, 
    int nX, int nY)
{
    char    cBuf [16] ;
    int     nValue ;
    
    nValue = GetDeviceCaps (hDCdata, nIndex) ;
    TextOut (hDCout, nX, nY, cBuf, wsprintf (cBuf, "%d", nValue)) ;
}