home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap13 / Print1 / Print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.8 KB  |  128 lines

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