home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1996 May / PCPLUS115.ISO / pcplus / delphi / graphics / picform.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-01-03  |  3.7 KB  |  132 lines

  1.  
  2. unit Picform;
  3.  
  4. { Sample program to show how graphics can be saved and loaded }
  5.  
  6. interface
  7.  
  8. uses WinTypes, WinProcs, Classes, Graphics, Controls, Forms, Menus,
  9.   ExtCtrls, Dialogs;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     MainMenu1: TMainMenu;
  14.     FileMenu: TMenuItem;
  15.     FileSave: TMenuItem;
  16.     FileLoad: TMenuItem;
  17.     FileClear: TMenuItem;
  18.     FileExit: TMenuItem;
  19.     Image1: TImage;       { The TImage component provides a drawing
  20.                             area on the form. Its Align property has
  21.                             been set to alClient, so it completely
  22.                             fills the form. Its Stretch property
  23.                             is set to True so the bitmap will
  24.                             be resized with the window }
  25.  
  26.     OpenDialog1: TOpenDialog;
  27.     SaveDialog1: TSaveDialog;
  28.     FileSaveAs: TMenuItem;
  29.     procedure FileExitClick(Sender: TObject);
  30.     procedure FileSaveClick(Sender: TObject);
  31.     procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
  32.       Shift: TShiftState; X, Y: Integer);
  33.     procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
  34.       Shift: TShiftState; X, Y: Integer);
  35.     procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  36.       Y: Integer);
  37.     procedure FileLoadClick(Sender: TObject);
  38.     procedure FileClearClick(Sender: TObject);
  39.     procedure FileSaveAsClick(Sender: TObject);
  40.     procedure FormCreate(Sender: TObject);
  41.   public
  42.     Drawing: Boolean;
  43.     CurrentFile : string;
  44.   end;
  45.  
  46. var
  47.   Form1: TForm1;
  48.  
  49. implementation
  50.  
  51. {$R *.DFM}
  52.  
  53. { The next three procedures deal with the drawing. You need to understand
  54. that each object has a drawing surface which is accessed using its
  55. Canvas property. The MoveTo function moves the focus to the XY
  56. coordinates. The LineTo method draws a line on the canvas from
  57. the current drawing position. The boolean variable, Drawing, is used
  58. to turn the 'pen' on and off }
  59.  
  60. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  61.   Shift: TShiftState; X, Y: Integer);
  62. begin
  63.   Drawing := True;
  64.   Image1.Canvas.MoveTo(X, Y);
  65. end;
  66.  
  67. procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  68.   Shift: TShiftState; X, Y: Integer);
  69. begin
  70.   Drawing := False;
  71. end;
  72.  
  73. procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  74.   Y: Integer);
  75. begin
  76.   if Drawing then Image1.Canvas.LineTo(X, Y);
  77. end;
  78.  
  79. procedure TForm1.FileExitClick(Sender: TObject);
  80. begin
  81.   Close;
  82. end;
  83.  
  84. { To save and load graphics, you can use SaveToFile and LoadFromFile
  85.   just as when saving and loading text. You need to specify the
  86.   Picture property of the image for this to work, however }
  87. procedure TForm1.FileSaveClick(Sender: TObject);
  88. begin
  89.   if CurrentFile <> '' then   { if file is unnamed, call SaveAs method }
  90.       Image1.Picture.SaveToFile(CurrentFile)
  91.   else
  92.       FileSaveAsClick(Sender);
  93. end;
  94.  
  95. procedure TForm1.FileSaveAsClick(Sender: TObject);
  96. begin
  97.   if SaveDialog1.Execute then
  98.   begin         { when a name has been given, call Save method }
  99.     CurrentFile := SaveDialog1.FileName;
  100.     FileSaveClick(Sender);
  101.   end;
  102. end;
  103.  
  104. procedure TForm1.FileLoadClick(Sender: TObject);
  105. begin
  106.   if OpenDialog1.Execute then
  107.   begin
  108.     CurrentFile := OpenDialog1.FileName;
  109.     Image1.Picture.LoadFromFile(CurrentFile);
  110.   end;
  111. end;
  112.  
  113. procedure TForm1.FileClearClick(Sender: TObject);
  114. var          { Create and size a bitmap drawing area }
  115.   Bitmap: TBitmap;
  116. begin
  117.   Bitmap := TBitmap.Create;
  118.   Bitmap.Width := Image1.Width;
  119.   Bitmap.Height := Image1.Height;
  120.   Image1.Picture.Graphic := Bitmap;
  121.   CurrentFile := '';
  122. end;
  123.  
  124.  
  125. procedure TForm1.FormCreate(Sender: TObject);
  126. begin
  127.   FileClearClick(Sender);
  128. end;
  129.  
  130.  
  131. end.
  132.