home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / windows / printeps / epsdemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-06-06  |  6.3 KB  |  216 lines

  1. program EpsPrintDemo;
  2.  
  3. uses
  4.   Objects,WinTypes,WinProcs,OWindows,ODialogs,OPrinter,EpsPrint;
  5.  
  6. {$R EPSDEMO.RES}
  7. {$I EPSDEMO.INC}
  8.  
  9. type
  10.   tEpsDemo = object (tApplication)
  11.     procedure InitMainWindow; virtual;
  12.     end;
  13.  
  14.   pMainWindow = ^tMainWindow;
  15.   tMainWindow = object (tDlgWindow)
  16.     Printer: pEpsPrinter;
  17.     constructor Init (aParent: pWindowsObject; aTitle: pChar);
  18.     destructor Done; virtual;
  19.     procedure Ok (var Msg: tMessage); virtual id_First + id_OK;
  20.     end;
  21.  
  22.   pEpsPrintout = ^tEpsPrintout;
  23.   tEpsPrintout = object (tPrintout)
  24.     EpsImages: pCollection;
  25.     constructor Init (aTitle: pChar);
  26.     destructor Done; virtual;
  27.     procedure PrintPage (Page: Word; var Rect: tRect; Flags: Word); virtual;
  28.     end;
  29.  
  30. {--tEpsDemo------------------------------------------------------------------}
  31.  
  32. procedure tEpsDemo.InitMainWindow;
  33.  
  34. begin
  35. MainWindow := New (pMainWindow,Init (nil,MakeIntResource (drMainDialog)));
  36. end;
  37.  
  38. {--tMainWindow---------------------------------------------------------------}
  39.  
  40. { The main window is a dialog with two buttons: Ok and Cancel. When the
  41.   program is run, pressing Ok causes the Printer object to print itself;
  42.   pressing Cancel aborts. }
  43.  
  44. { During initialization, tMainWindow verifies that the default printer is
  45.   capable of printing PostScript, and aborts if not. }
  46.  
  47. constructor tMainWindow.Init (aParent: pWindowsObject; aTitle: pChar);
  48.  
  49. begin
  50. inherited Init (aParent,aTitle);
  51. Printer := New (pEpsPrinter,Init);
  52. if not Printer^.IsPostScriptCapable then
  53.   begin
  54.   Dispose (Printer,Done);
  55.   MessageBox (0,'The default printer driver does not support PostScript',nil,
  56.     MB_ICONSTOP + MB_OK);
  57.   Fail;
  58.   end;
  59. end;
  60.  
  61. destructor tMainWindow.Done;
  62.  
  63. begin
  64. Dispose (Printer,Done);
  65. inherited Done;
  66. end;
  67.  
  68. { The actual printing is taken care of in the Ok method. }
  69.  
  70. procedure tMainWindow.Ok (var Msg: tMessage);
  71.  
  72. var
  73.   Printout: pEpsPrintout;
  74.   Eps: pEps;
  75.  
  76. begin
  77.  
  78. { Initialize a tEpsPrintout object. }
  79.  
  80. Printout := New (PEpsPrintout,Init ('EPS Print Demo'));
  81.  
  82. { And a tEps object associated with the EPS file that we're going to print.
  83.   The image contained in EPSDEMO.EPS is a 20% gray rectangle, 8.0 inches
  84.   tall and 10.0 inches wide, with a narrow black border. Centered in the
  85.   rectangle is a smaller white rectangle containing the phrase "This is an
  86.   EPS image". EPSDEMO.EPS was created using CorelDRAW 3.0. EPSDEMOX.EPS is
  87.   identical to EPSDEMO.EPS, except that its preview header has been removed.
  88.   If you want to see what the original EPS image looks like, you can copy
  89.   EPSDEMOX.EPS directly to a PostScript printer. }
  90.  
  91. Eps := New (pEps,Init ('EPSDEMO.EPS'));
  92.  
  93. { The tEps initialization will fail if the tEps object could not be
  94.   initialized because (1) the EPS file could not be opened, or (2) the EPS
  95.   file does not contain a valid %%BoundingBox comment. }
  96.  
  97. if not Assigned (Eps) then
  98.   MessageBox (0,'The EPS file could not be initialized',nil,MB_ICONSTOP +
  99.     MB_OK)
  100. else begin
  101.  
  102. { If all goes well with initialization of the tEpsPrintout and tEps objects,
  103.   insert three copies of the EPS image into the printout. The first is 0.25x
  104.   original size, and positioned so that its lower left corner is 1 inch
  105.   above and 1 inch to the right of the lower left corner of the page. The
  106.   second is distorted, so that its width is 0.75x original size, its height
  107.   is 0.10x original size, it is rotated counterclockwise 90 degrees, and it
  108.   is centered on the page. The third is scaled identically to the first, but
  109.   it is rotated 180 degrees and positioned so that its upper right corner is
  110.   1 inch below and 1 inch to the left of the upper right corner of the page. }
  111.  
  112.   Printout^.EpsImages^.Insert (New (pImage,Init (Eps,0,1.0,1.0,2.5,2.0)));
  113.   Printout^.EpsImages^.Insert (New (pImage,Init (Eps,90,4.65,1.75,7.50,
  114.     0.80)));
  115.   Printout^.EpsImages^.Insert (New (pImage,Init (Eps,180,7.5,10.0,2.5,2.0)));
  116.   Printer^.Print (@Self,Printout);
  117.   Dispose (Eps,Done);
  118.   end;
  119. Dispose (Printout,Done);
  120. inherited Ok (Msg);
  121. end;
  122.  
  123. {--tEpsPrintout--------------------------------------------------------------}
  124.  
  125. constructor tEpsPrintout.Init (aTitle: pChar);
  126.  
  127. begin
  128. inherited Init (aTitle);
  129. EpsImages := New (pCollection,Init (5,0));
  130. end;
  131.  
  132. destructor tEpsPrintout.Done;
  133.  
  134. begin
  135. Dispose (EpsImages,Done);
  136. inherited Done;
  137. end;
  138.  
  139. procedure tEpsPrintout.PrintPage (Page: Word; var Rect: tRect; Flags: Word);
  140.  
  141.   procedure SendImage (Image: pImage); far;
  142.  
  143.   begin
  144.   Image^.Send (DC);
  145.   end;
  146.  
  147. var
  148.   ScaleX,ScaleY,MarginX,MarginY: Word;
  149.   Dimension,Offset: tPoint;
  150.   OldPen,NewPen: hPen;
  151.   L,R,T,B: Integer;
  152.  
  153. begin
  154.  
  155. { The first thing to do is to send the EPS images to the printer DC. }
  156.  
  157. EpsImages^.ForEach (@SendImage);
  158.  
  159. { Now that the EPS images have been taken care of, we can add various GDI
  160.   elements to the page (text, graphics, etc.); as an example, we will draw a
  161.   rectangular black frame, with a line width of 0.05 inch, centered on the
  162.   page, with 0.5 inch margins all around. First we need to get some
  163.   information concerning the logical units for this printer, as well as the
  164.   dimensions of the physical page and the location of the logical page
  165.   within the physical page. }
  166.  
  167. ScaleX := GetDeviceCaps (DC,LOGPIXELSX);  { units/inch }
  168. ScaleY := GetDeviceCaps (DC,LOGPIXELSY);
  169. Escape (DC,GETPHYSPAGESIZE,0,nil,@Dimension);
  170. Escape (DC,GETPRINTINGOFFSET,0,nil,@Offset);
  171.  
  172. { Create and select a solid black pen, 0.05 inch wide. }
  173.  
  174. NewPen := CreatePen (PS_SOLID,Round (0.05 * ScaleX),0);
  175. OldPen := SelectObject (DC,NewPen);
  176.  
  177. { Define the 0.5 inch margins and calculate the locations of the sides of
  178.   the rectangular frame. }
  179.  
  180. MarginX := Round (0.5 * ScaleX);
  181. MarginY := Round (0.5 * ScaleY);
  182. L := MarginX - Offset.X;
  183. R := Dimension.X - MarginX - Offset.X;
  184. T := MarginY - Offset.Y;
  185. B := Dimension.Y - MarginY - Offset.Y;
  186.  
  187. { Draw the rectangular frame. }
  188.  
  189. MoveTo (DC,L,T);
  190. LineTo (DC,R,T);
  191. LineTo (DC,R,B);
  192. LineTo (DC,L,B);
  193. LineTo (DC,L,T);
  194.  
  195. { Put back the original pen. }
  196.  
  197. SelectObject (DC,OldPen);
  198. DeleteObject (NewPen);
  199. end;
  200.  
  201. {--main----------------------------------------------------------------------}
  202.  
  203. { Here it is--what you've all been waiting for--the main block! }
  204.  
  205. var
  206.   EpsDemo: tEpsDemo;
  207.  
  208. begin
  209. EpsDemo.Init ('EPS Print Demo');
  210. if Assigned (EpsDemo.MainWindow) then
  211.   begin
  212.   EpsDemo.Run;
  213.   EpsDemo.Done;
  214.   end;
  215. end.
  216.