home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / STREAM1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.8 KB  |  274 lines

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