home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 8.ddi / OWLPRINT.ZIP / RULER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.5 KB  |  203 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. // Printing program example using Application Framework Libraries (OWL)
  4. // This application displays and prints a RULER using OWL and printer classes.
  5.  
  6.  
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10.  
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #ifdef __cplusplus
  15. }
  16. #endif
  17.  
  18.  
  19. #include <owl.h>
  20.  
  21. #include "printer.h"
  22. #include "ids.h"
  23.  
  24. class TRulerApp : public TApplication
  25. {
  26. public:
  27.         TRulerApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.     LPSTR lpCmdLine, int nCmdShow)
  29.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  30.     {}
  31.   virtual void InitMainWindow(void);
  32. };
  33.  
  34.   _CLASSDEF(TRulerWin)
  35.   class TRulerWin : public TWindow
  36.   {
  37.                 PTPrinter Printer;    // Pointer to a TPrinter object
  38.   public:
  39.     TRulerWin(PTWindowsObject AParent, LPSTR ATitle,
  40.         PTModule AModule = NULL);
  41.     ~TRulerWin( void );
  42.     virtual void CMFilePrint(RTMessage Msg)
  43.         = [CM_FIRST + CM_FILEPRINT];
  44.     virtual void CMFilePrinterSetup(RTMessage Msg)
  45.         = [CM_FIRST + CM_FILEPRINTERSETUP];
  46.     virtual void CMFileExit(RTMessage Msg)
  47.         = [CM_FIRST + CM_FILEEXIT];
  48.     virtual void Paint(HDC PaintDC, PAINTSTRUCT& PaintInfo);
  49.   };
  50.  
  51.   _CLASSDEF(TRulerOut)
  52.   class TRulerOut : public TPrintout
  53.   {
  54.   public:
  55.     TRulerOut(Pchar ATitle) : TPrintout(ATitle) {}
  56.     virtual void PrintPage(HDC DC, WORD Page, POINT Size, LPRECT Rect,
  57.         WORD Flags);
  58.     void SetBanding( BOOL b ) { Banding = b; }
  59.   };
  60.  
  61.  
  62. // Display or print a ruler
  63.  
  64. void ShowRuler( HDC DC )
  65. {
  66. const UnitsPerInch = 100;      // Display scale 0.01 inches per unit
  67. const NumInches = 6;           // Size of ruler in inches
  68. const MarksPerInch = 4;        // Number of markers for each inch
  69. const LargeMarkerSize = (0.50 * UnitsPerInch);  // Size of labeled markers
  70. const SmallMarkerSize = (0.25 * UnitsPerInch);  // Size of small markers
  71.  
  72. int I;                         // For-loop control variable
  73. int X, X1, Y1, X2, Y2;         // Coordinates for displaying ruler
  74. char S[2];                     // Holds ruler digits in text form
  75.  
  76.   SaveDC(DC);
  77.   SetMapMode(DC, MM_LOENGLISH);
  78.   X1 = (0.50 * UnitsPerInch);
  79.   Y1 = X1;
  80.   X2 = X1 + (NumInches * UnitsPerInch);
  81.   Y2 = Y1 + (1 * UnitsPerInch);
  82.   Rectangle(DC, X1, -Y1, X2, -Y2);
  83.   Y2 = Y1 + LargeMarkerSize;
  84.   for (I = 1;  I <= NumInches - 1; I++ )
  85.   {
  86.     X = X1 + (I * UnitsPerInch);
  87.     MoveTo(DC, X, -Y1);
  88.     LineTo(DC, X, -Y2);
  89.     itoa(I, S, 10);
  90.     TextOut(DC, X, -Y2, S, strlen(S));
  91.   }
  92.   Y2 = Y1 + SmallMarkerSize;
  93.   for ( I = 0; I <= ((NumInches * MarksPerInch) - 1); I++ )
  94.   {
  95.     X = X1 + ((I * UnitsPerInch) / MarksPerInch);
  96.     MoveTo(DC, X, -Y1);
  97.     LineTo(DC, X, -Y2);
  98.   }
  99.   RestoreDC(DC, -1);
  100. }
  101.  
  102.  
  103. // TRulerApp
  104.  
  105.  
  106. // Initialize TRulerApp's main window
  107.  
  108. void TRulerApp::InitMainWindow(void)
  109. {
  110.   MainWindow = new TRulerWin(NULL, "Ruler Demonstration");
  111. }
  112.  
  113.  
  114.  
  115. // TRulerWin
  116.  
  117.  
  118. // Construct a TRulerWin object
  119.  
  120. TRulerWin::TRulerWin(PTWindowsObject AParent, LPSTR ATitle, PTModule AModule)
  121.         : TWindow( AParent, ATitle, AModule )
  122. {
  123.     AssignMenu(ID_MENU);
  124.     Attr.X = GetSystemMetrics(SM_CXSCREEN) / 8;
  125.     Attr.Y = GetSystemMetrics(SM_CYSCREEN) / 8;
  126.     Attr.H = Attr.Y * 6;
  127.     Attr.W = Attr.X * 6;
  128.     Printer = new TPrinter;
  129. }
  130.  
  131.  
  132. // Destroy a TRulerWin object
  133.  
  134. TRulerWin::~TRulerWin( void )
  135. {
  136.     delete Printer;
  137. }
  138.  
  139.  
  140. // Execute File:Print command
  141.  
  142. void TRulerWin::CMFilePrint(RTMessage)
  143. {
  144.   PTRulerOut Printout = 0;
  145.  
  146.   if ( Printer )
  147.   {
  148.     Printout = new TRulerOut("Ruler Test");
  149.     if ( Printout )
  150.     {
  151.       Printout->SetBanding( TRUE );
  152.       Printer->Print(this, Printout);
  153.       delete Printout;
  154.     }
  155.   }
  156. }
  157.  
  158.  
  159. // Execute File:Printer-setup command
  160.  
  161. void TRulerWin::CMFilePrinterSetup(RTMessage)
  162. {
  163.   if ( Printer )
  164.         Printer->Setup(this);
  165. }
  166.  
  167.  
  168. // Execute File:Exit command
  169.  
  170. void TRulerWin::CMFileExit(RTMessage)
  171. {
  172.   CloseWindow();
  173. }
  174.  
  175.  
  176. // Paint window's contents on screen
  177.  
  178. void TRulerWin::Paint(HDC PaintDC, PAINTSTRUCT&)
  179. {
  180.   ShowRuler(PaintDC);
  181. }
  182.  
  183.  
  184. // TRulerOut
  185.  
  186.  
  187. // Print page (or pages)
  188.  
  189. void TRulerOut::PrintPage(HDC DC, WORD, POINT, LPRECT, WORD)
  190. {
  191.   ShowRuler(DC);
  192. }
  193.  
  194.  
  195. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  196.   LPSTR lpCmdLine, int nCmdShow)
  197. {
  198.   TRulerApp RulerApp("Ruler", hInstance, hPrevInstance,
  199.     lpCmdLine, nCmdShow);
  200.   RulerApp.Run();
  201.   return ( RulerApp.Status );
  202. }
  203.