home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP15 / PRINT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  4.3 KB  |  138 lines

  1. /*-------------------------------------------------------------------
  2.    PRINT.C -- Common routines for Print1, Print2, Print3, and Print4
  3.   -------------------------------------------------------------------*/
  4.  
  5. #include <windows.h>
  6.  
  7. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  8. BOOL PrintMyPage (HWND) ;
  9.  
  10. extern HINSTANCE hInst ;
  11. extern char      szAppName[] ;
  12. extern char      szCaption[] ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16.      {
  17.      HWND       hwnd ;
  18.      MSG        msg ;
  19.      WNDCLASSEX wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      hInst = hInstance ;
  37.  
  38.      hwnd = CreateWindow (szAppName, szCaption,
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.           {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.           }
  52.      return msg.wParam ;
  53.      }
  54.  
  55. HDC GetPrinterDC (void)
  56.      {
  57.      PRINTER_INFO_5 pinfo5[3] ; 
  58.      DWORD          dwNeeded, dwReturned ;
  59.  
  60.      if (EnumPrinters (PRINTER_ENUM_DEFAULT, NULL, 5, (LPBYTE) pinfo5,
  61.                        sizeof (pinfo5), &dwNeeded, &dwReturned))
  62.           return CreateDC (NULL, pinfo5[0].pPrinterName, NULL, NULL) ;
  63.  
  64.      return 0 ;            // EnumPrinters failed, so return null hdc
  65.      }
  66.  
  67. void PageGDICalls (HDC hdcPrn, int cxPage, int cyPage)
  68.      {
  69.      static char szTextStr[] = "Hello, Printer!" ;
  70.  
  71.      Rectangle (hdcPrn, 0, 0, cxPage, cyPage) ;
  72.  
  73.      MoveToEx (hdcPrn, 0, 0, NULL) ;
  74.      LineTo   (hdcPrn, cxPage, cyPage) ;
  75.      MoveToEx (hdcPrn, cxPage, 0, NULL) ;
  76.      LineTo   (hdcPrn, 0, cyPage) ;
  77.  
  78.      SaveDC (hdcPrn) ;
  79.  
  80.      SetMapMode       (hdcPrn, MM_ISOTROPIC) ;
  81.      SetWindowExtEx   (hdcPrn, 1000, 1000, NULL) ;
  82.      SetViewportExtEx (hdcPrn, cxPage / 2, -cyPage / 2, NULL) ;
  83.      SetViewportOrgEx (hdcPrn, cxPage / 2,  cyPage / 2, NULL) ;
  84.  
  85.      Ellipse (hdcPrn, -500, 500, 500, -500) ;
  86.  
  87.      SetTextAlign (hdcPrn, TA_BASELINE | TA_CENTER) ;
  88.  
  89.      TextOut (hdcPrn, 0, 0, szTextStr, sizeof (szTextStr) - 1) ;
  90.  
  91.      RestoreDC (hdcPrn, -1) ;
  92.      }
  93.  
  94. LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  95.      {
  96.      static int   cxClient, cyClient ;
  97.      HDC          hdc ;
  98.      HMENU        hMenu ;
  99.      PAINTSTRUCT  ps ;
  100.  
  101.      switch (msg)
  102.           {
  103.           case WM_CREATE :
  104.                hMenu = GetSystemMenu (hwnd, FALSE) ;
  105.                AppendMenu (hMenu, MF_SEPARATOR, 0, NULL) ;
  106.                AppendMenu (hMenu, 0, 1, "&Print") ;
  107.                return 0 ;
  108.  
  109.           case WM_SIZE :
  110.                cxClient = LOWORD (lParam) ;
  111.                cyClient = HIWORD (lParam) ;
  112.                return 0 ;
  113.  
  114.           case WM_SYSCOMMAND :
  115.                if (wParam == 1)
  116.                     {
  117.                     if (PrintMyPage (hwnd))
  118.                          MessageBox (hwnd, "Could not print page!",
  119.                               szAppName, MB_OK | MB_ICONEXCLAMATION) ;
  120.                     return 0 ;
  121.                     }
  122.                break ;
  123.  
  124.           case WM_PAINT :
  125.                hdc = BeginPaint (hwnd, &ps) ;
  126.  
  127.                PageGDICalls (hdc, cxClient, cyClient) ;
  128.  
  129.                EndPaint (hwnd, &ps) ;
  130.                return 0 ;
  131.  
  132.           case WM_DESTROY :
  133.                PostQuitMessage (0) ;
  134.                return 0 ;
  135.           }
  136.      return DefWindowProc (hwnd, msg, wParam, lParam) ;
  137.      }
  138.