home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap15 / print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.9 KB  |  129 lines

  1. /*-------------------------------------------------------------------
  2.    PRINT.C -- Common Routines for Print1, Print2, Print3, and Print4
  3.   -------------------------------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <string.h>
  7.  
  8. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  9. BOOL PrintMyPage (HWND) ;
  10.  
  11. extern HANDLE hInst ;
  12. extern char szAppName [] ;
  13. extern char szCaption [] ;
  14.  
  15. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  16.                     LPSTR lpszCmdLine, int nCmdShow)
  17.      {
  18.      HWND     hwnd ;
  19.      MSG      msg ;
  20.      WNDCLASS wndclass ;
  21.  
  22.      if (!hPrevInstance) 
  23.           {
  24.           wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  25.           wndclass.lpfnWndProc   = WndProc ;
  26.           wndclass.cbClsExtra    = 0 ;
  27.           wndclass.cbWndExtra    = 0 ;
  28.           wndclass.hInstance     = hInstance ;
  29.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  30.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  31.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  32.           wndclass.lpszMenuName  = NULL ;
  33.           wndclass.lpszClassName = szAppName ;
  34.  
  35.           RegisterClass (&wndclass) ;
  36.           }
  37.  
  38.      hInst = hInstance ;
  39.  
  40.      hwnd = CreateWindow (szAppName, szCaption,
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           NULL, NULL, hInstance, NULL) ;
  45.  
  46.      ShowWindow (hwnd, nCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.  
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.           {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.           }
  54.      return msg.wParam ;
  55.      }
  56.  
  57. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  58.      {
  59.      HMENU hMenu ;
  60.  
  61.      switch (message)
  62.           {
  63.           case WM_CREATE:
  64.                hMenu = GetSystemMenu (hwnd, FALSE);
  65.                AppendMenu (hMenu, MF_SEPARATOR, 0, NULL) ;
  66.                AppendMenu (hMenu, 0, 1, "&Print") ;
  67.                return 0 ;
  68.  
  69.           case WM_SYSCOMMAND:
  70.                if (wParam == 1)
  71.                     {
  72.                     if (PrintMyPage (hwnd))
  73.                          MessageBox (hwnd, "Could not print page",
  74.                               szAppName, MB_OK | MB_ICONEXCLAMATION) ;
  75.                     return 0 ;
  76.                     }
  77.                break ;
  78.  
  79.           case WM_DESTROY:
  80.                PostQuitMessage (0) ;
  81.                return 0 ;
  82.           }
  83.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  84.      }
  85.  
  86. HDC GetPrinterDC (void)
  87.      {
  88.      static char szPrinter [80] ;
  89.      char        *szDevice, *szDriver, *szOutput ;
  90.  
  91.      GetProfileString ("windows", "device", ",,,", szPrinter, 80) ;
  92.  
  93.      if ((szDevice = strtok (szPrinter, "," )) &&
  94.          (szDriver = strtok (NULL,      ", ")) && 
  95.          (szOutput = strtok (NULL,      ", ")))
  96.           
  97.                return CreateDC (szDriver, szDevice, szOutput, NULL) ;
  98.  
  99.      return 0 ;
  100.      }
  101.  
  102. void PageGDICalls (HDC hdcPrn, short cxPage, short cyPage)
  103.      {
  104.      static char szTextStr [] = "Hello Printer!" ;
  105.      DWORD       dwExtent ;
  106.  
  107.      Rectangle (hdcPrn, 0, 0, cxPage, cyPage) ;
  108.  
  109.      MoveTo (hdcPrn, 0, 0) ;
  110.      LineTo (hdcPrn, cxPage, cyPage) ;
  111.      MoveTo (hdcPrn, cxPage, 0) ;
  112.      LineTo (hdcPrn, 0, cyPage) ;
  113.  
  114.      SaveDC (hdcPrn) ;
  115.  
  116.      SetMapMode (hdcPrn, MM_ISOTROPIC) ;
  117.      SetWindowExt   (hdcPrn, 1000, 1000) ;
  118.      SetViewportExt (hdcPrn, cxPage / 2, -cyPage / 2) ;
  119.      SetViewportOrg (hdcPrn, cxPage / 2,  cyPage / 2) ;
  120.  
  121.      Ellipse (hdcPrn, -500, 500, 500, -500) ;
  122.  
  123.      dwExtent = GetTextExtent (hdcPrn, szTextStr, sizeof szTextStr - 1) ;
  124.      TextOut (hdcPrn, LOWORD (dwExtent) / 2, HIWORD (dwExtent) / 2,
  125.               szTextStr, sizeof szTextStr - 1) ;
  126.  
  127.      RestoreDC (hdcPrn, -1) ;
  128.      }
  129.