home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programming Unleashed / Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso / chap09 / lineloop / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-21  |  1.7 KB  |  80 lines

  1. unit Main;
  2.  
  3. { Program copyright (c) 1995 by Charles Calvert }
  4. { Project Name: LINELOOP }
  5.  
  6. { Demonstrates how to use the Canvas MoveTo and LineTo
  7.   routines. Also shows how to handle PeekMessage loops,
  8.   which is all about sharing processor time with other
  9.   programs. More general info about coordinates, and
  10.   about using the RGB function to access the colors
  11.   available on your system.
  12.  
  13.   Note the comments above the call to YieldToOthers }
  14.  
  15. interface
  16.  
  17. uses
  18.   WinTypes, WinProcs,
  19.   Classes, Messages,
  20.   Graphics, Controls, Printers,
  21.   Forms, Menus;
  22.  
  23. type
  24.   TForm1 = class(TForm)
  25.     MainMenu1: TMainMenu;
  26.     Start1: TMenuItem;
  27.     Start2: TMenuItem;
  28.     Stop1: TMenuItem;
  29.     procedure BStartClick(Sender: TObject);
  30.     procedure BStopClick(Sender: TObject);
  31.   private
  32.     Draw: Boolean;
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.DFM}
  41.  
  42. { You can call either YieldToOthers, or the built
  43.   in function: Application.ProcessMessages }
  44. procedure YieldToOthers;
  45. var
  46.   Msg : TMsg;
  47. begin
  48.   while PeekMessage(Msg,0,0,0,PM_REMOVE) do begin
  49.     if (Msg.Message = WM_QUIT) then begin
  50.       exit;
  51.     end;
  52.     TranslateMessage(Msg);
  53.     DispatchMessage(Msg);
  54.   end;
  55. end;
  56.  
  57. procedure TForm1.BStartClick(Sender: TObject);
  58. var
  59.   R: TRect;
  60.   x, y: Integer;
  61. begin
  62.   Canvas.MoveTo(10, 10);
  63.   R := GetClientRect;
  64.   Draw := True;
  65.   while Draw do begin
  66.     x := Random(R.Right);
  67.     y := Random(R.Bottom);
  68.     Canvas.Pen.Color := RGB(Random(255), Random(255), Random(255));
  69.     Canvas.LineTo(X, Y);
  70.     YieldToOthers; { or Application.ProcessMessages }
  71.   end;
  72. end;
  73.  
  74. procedure TForm1.BStopClick(Sender: TObject);
  75. begin
  76.   Draw := False;
  77. end;
  78.  
  79. end.
  80.