home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / blx21.zip / OWLPRT.ARJ / PRTCPP.ARJ / RULER.CPP < prev    next >
C/C++ Source or Header  |  1992-02-19  |  4KB  |  190 lines

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