home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / docdemos / stream2.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  7KB  |  285 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9.  
  10. { Load and display a collection of graphical objects from a stream:
  11.   ellipses, rectangles and pie slices. This collection was created
  12.   and put on a stream by another program (STREAM1.PAS). }
  13.  
  14. program Stream2;
  15.  
  16. uses
  17.   WObjects, WinTypes, WinProcs, Strings;
  18.  
  19. const
  20.   em_Stream = 100;
  21.  
  22. { ********************************** }
  23. { ******  Graphical Objects  ******* }
  24. { ********************************** }
  25.  
  26. type
  27.   PGraphObject = ^TGraphObject;
  28.   TGraphObject = object(TObject)
  29.     Rect: TRect;
  30.     constructor Init(Bounds: TRect);
  31.     constructor Load(var S: TStream);
  32.     procedure Draw(DC: HDC); virtual;
  33.     procedure Store(var S: TStream); virtual;
  34.   end;
  35.  
  36.   PGraphEllipse = ^TGraphEllipse;
  37.   TGraphEllipse = object(TGraphObject)
  38.     procedure Draw(DC: HDC); virtual;
  39.   end;
  40.  
  41.   PGraphRect = ^TGraphRect;
  42.   TGraphRect = object(TGraphObject)
  43.     procedure Draw(DC: HDC); virtual;
  44.   end;
  45.  
  46.   PGraphPie = ^TGraphPie;
  47.   TGraphPie = object(TGraphObject)
  48.     ArcStart, ArcEnd: TPoint;
  49.     constructor Init(Bounds: TRect);
  50.     constructor Load(var S: TStream);
  51.     procedure Draw(DC: HDC); virtual;
  52.     procedure Store(var S: TStream); virtual;
  53.   end;
  54.  
  55. { TGraphObject }
  56. constructor TGraphObject.Init(Bounds: TRect);
  57. var
  58.   Height, Width: Word;
  59. begin
  60.   TObject.Init;
  61.   with Bounds do
  62.   begin
  63.     Height := Random(Bottom - Top) div 2 + 10;
  64.     Width := Random(Right - Left) div 3 + 15;
  65.   end;
  66.   with Rect do
  67.   begin
  68.     Left := Random(Bounds.Right - Bounds.Left - Width);
  69.     Right := Left + Width;
  70.     Top := Random(Bounds.Bottom - Bounds.Top - Height);
  71.     Bottom := Top + Height;
  72.   end;
  73. end;
  74.  
  75. constructor TGraphObject.Load(var S: TStream);
  76. begin
  77.   S.Read(Rect, SizeOf(Rect));
  78. end;
  79.  
  80. procedure TGraphObject.Draw(DC: HDC);
  81. begin
  82.   Abstract;
  83. end;
  84.  
  85. procedure TGraphObject.Store(var S: TStream);
  86. begin
  87.   S.Write(Rect, SizeOf(Rect));
  88. end;
  89.  
  90. { TGraphEllipse }
  91. procedure TGraphEllipse.Draw(DC: HDC);
  92. begin
  93.   with Rect do
  94.     Ellipse(DC, Left, Top, Right, Bottom);
  95. end;
  96.  
  97. { TGraphRect }
  98. procedure TGraphRect.Draw(DC: HDC);
  99. begin
  100.   with Rect do
  101.     Rectangle(DC, Left, Top, Right, Bottom);
  102. end;
  103.  
  104. { TGraphPie }
  105. constructor TGraphPie.Init(Bounds: TRect);
  106. var Height, Width: Word;
  107. begin
  108.   TGraphObject.Init(Bounds);
  109.   with Bounds do
  110.   begin
  111.     Height := Random(Bottom - Top);
  112.     Width := Random(Right - Left);
  113.  
  114.     ArcStart.X := Random(Right - Left - Width);
  115.     ArcEnd.X := ArcStart.X + Width;
  116.     ArcStart.Y := Random(Bottom - Top - Height);
  117.     ArcEnd.Y := ArcStart.Y + Height;
  118.   end;
  119. end;
  120.  
  121. constructor TGraphPie.Load(var S: TStream);
  122. begin
  123.   TGraphObject.Load(S);
  124.   S.Read(ArcStart, SizeOf(ArcStart));
  125.   S.Read(ArcEnd, SizeOf(ArcEnd));
  126. end;
  127.  
  128. procedure TGraphPie.Draw;
  129. begin
  130.   with Rect do
  131.     Pie(DC, Left, Top, Right, Bottom,
  132.       ArcStart.X, ArcStart.Y, ArcEnd.X, ArcEnd.Y);
  133. end;
  134.  
  135. procedure TGraphPie.Store(var S: TStream);
  136. begin
  137.   TGraphObject.Store(S);
  138.   S.Write(ArcStart, SizeOf(ArcStart));
  139.   S.Write(ArcEnd, SizeOf(ArcEnd));
  140. end;
  141.  
  142.  
  143. { ********************************** }
  144. { **  Stream Registration Records ** }
  145. { ********************************** }
  146.  
  147. const
  148.   RGraphEllipse: TStreamRec = (
  149.     ObjType: 150;
  150.     VmtLink: Ofs(TypeOf(TGraphEllipse)^);
  151.     Load: @TGraphEllipse.Load;
  152.     Store: @TGraphEllipse.Store);
  153.  
  154.   RGraphRect: TStreamRec = (
  155.     ObjType: 151;
  156.     VmtLink: Ofs(TypeOf(TGraphRect)^);
  157.     Load: @TGraphRect.Load;
  158.     Store: @TGraphRect.Store);
  159.  
  160.   RGraphPie: TStreamRec = (
  161.     ObjType: 152;
  162.     VmtLink: Ofs(TypeOf(TGraphPie)^);
  163.     Load: @TGraphPie.Load;
  164.     Store: @TGraphPie.Store);
  165.  
  166. procedure StreamRegistration;
  167. begin
  168.   RegisterType(RCollection);
  169.   RegisterType(RGraphEllipse);
  170.   RegisterType(RGraphRect);
  171.   RegisterType(RGraphPie);
  172. end;
  173.  
  174. { ********************************** }
  175. { *********  Graph Window  ********* }
  176. { ********************************** }
  177. type
  178.   { Define a TApplication descendant }
  179.   TGraphApp = object(TApplication)
  180.     procedure InitMainWindow; virtual;
  181.     procedure Error(ErrorCode: Integer); virtual;
  182.   end;
  183.  
  184.   PGraphWindow = ^TGraphWindow;
  185.   TGraphWindow = object(TWindow)
  186.     GraphicsList: PCollection;
  187.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  188.     destructor Done; virtual;
  189.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  190.     procedure SetupWindow; virtual;
  191.   end;
  192.  
  193.  
  194. { TGraphApp }
  195. procedure TGraphApp.InitMainWindow;
  196. begin
  197.   MainWindow := New(PGraphWindow,
  198.     Init(nil, 'Collection of Graphical Objects'));
  199. end;
  200.  
  201. { TGraphWindow }
  202. constructor TGraphWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  203. begin
  204.   TWindow.Init(AParent, ATitle);
  205.   GraphicsList := nil;
  206. end;
  207.  
  208. procedure TGraphWindow.SetupWindow;
  209. var
  210.   Bounds: TRect;
  211.   I: Integer;
  212.   P: PGraphObject;
  213.   GraphicsStream: TBufStream;
  214. begin
  215.   TWindow.SetupWindow;
  216.   StreamRegistration;                        { Register all streams }
  217.  
  218.   { Load collection from stream }
  219.   GraphicsStream.Init('GRAPH.STM', stOpen, 1024);         { Open stream }
  220.   GraphicsList := PCollection(GraphicsStream.Get);        { Load collection }
  221.   if GraphicsStream.Status <> 0 then
  222.     Status := em_Stream;
  223.   GraphicsStream.Done;                                    { Shut down stream }
  224. end;
  225.  
  226. destructor TGraphWindow.Done;
  227. begin
  228.   if GraphicsList <> nil then
  229.     Dispose(GraphicsList, Done);         { Delete collection }
  230.   TWindow.Done;
  231. end;
  232.  
  233. procedure TGraphWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  234.  
  235. { Nest the iterator method inside Paint so it can access the DC }
  236. procedure DrawAll(C: PCollection); far;
  237.  
  238. { Nested, far procedure. Receives one
  239.   collection element--a GraphObject, and
  240.   calls that elements Draw method.
  241. }
  242.  
  243. procedure CallDraw(P : PGraphObject); far;
  244. begin
  245.   P^.Draw(PaintDC);                            { Call Draw method }
  246. end;
  247.  
  248. begin { DrawAll }
  249.   C^.ForEach(@CallDraw);              { Draw each object }
  250. end;
  251.  
  252. begin
  253.   DrawAll(GraphicsList)
  254. end;
  255.  
  256. procedure TGraphApp.Error(ErrorCode: Integer);
  257. var
  258.   ErrorString: array[0..25] of Char;
  259. begin
  260.   case ErrorCode of
  261.     em_Stream:
  262.       MessageBox(0, 'Error loading GRAPHICS.STM (run STREAM1.PAS first).',
  263.         'Application Error', mb_Ok);
  264.   else
  265.     WVSPrintF(ErrorString, 'Error code = %d', ErrorCode);
  266.     MessageBox(0, ErrorString, 'Application Error', mb_Ok);
  267.   end;
  268. end;
  269.  
  270.  
  271. { ********************************** }
  272. { **********  Main Program ********* }
  273. { ********************************** }
  274.  
  275. { Declare a variable of type TGraphApp }
  276. var
  277.   GraphApp: TGraphApp;
  278.  
  279. { Run the GraphApp }
  280. begin
  281.   GraphApp.Init('GraphApp');
  282.   GraphApp.Run;
  283.   GraphApp.Done;
  284. end.
  285.