home *** CD-ROM | disk | FTP | other *** search
-
- unit Picform;
-
- { Sample program to show how graphics can be saved and loaded }
-
- interface
-
- uses WinTypes, WinProcs, Classes, Graphics, Controls, Forms, Menus,
- ExtCtrls, Dialogs;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- FileMenu: TMenuItem;
- FileSave: TMenuItem;
- FileLoad: TMenuItem;
- FileClear: TMenuItem;
- FileExit: TMenuItem;
- Image1: TImage; { The TImage component provides a drawing
- area on the form. Its Align property has
- been set to alClient, so it completely
- fills the form. Its Stretch property
- is set to True so the bitmap will
- be resized with the window }
-
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- FileSaveAs: TMenuItem;
- procedure FileExitClick(Sender: TObject);
- procedure FileSaveClick(Sender: TObject);
- procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure FileLoadClick(Sender: TObject);
- procedure FileClearClick(Sender: TObject);
- procedure FileSaveAsClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- public
- Drawing: Boolean;
- CurrentFile : string;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- { The next three procedures deal with the drawing. You need to understand
- that each object has a drawing surface which is accessed using its
- Canvas property. The MoveTo function moves the focus to the XY
- coordinates. The LineTo method draws a line on the canvas from
- the current drawing position. The boolean variable, Drawing, is used
- to turn the 'pen' on and off }
-
- procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- Drawing := True;
- Image1.Canvas.MoveTo(X, Y);
- end;
-
- procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin
- Drawing := False;
- end;
-
- procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- begin
- if Drawing then Image1.Canvas.LineTo(X, Y);
- end;
-
- procedure TForm1.FileExitClick(Sender: TObject);
- begin
- Close;
- end;
-
- { To save and load graphics, you can use SaveToFile and LoadFromFile
- just as when saving and loading text. You need to specify the
- Picture property of the image for this to work, however }
- procedure TForm1.FileSaveClick(Sender: TObject);
- begin
- if CurrentFile <> '' then { if file is unnamed, call SaveAs method }
- Image1.Picture.SaveToFile(CurrentFile)
- else
- FileSaveAsClick(Sender);
- end;
-
- procedure TForm1.FileSaveAsClick(Sender: TObject);
- begin
- if SaveDialog1.Execute then
- begin { when a name has been given, call Save method }
- CurrentFile := SaveDialog1.FileName;
- FileSaveClick(Sender);
- end;
- end;
-
- procedure TForm1.FileLoadClick(Sender: TObject);
- begin
- if OpenDialog1.Execute then
- begin
- CurrentFile := OpenDialog1.FileName;
- Image1.Picture.LoadFromFile(CurrentFile);
- end;
- end;
-
- procedure TForm1.FileClearClick(Sender: TObject);
- var { Create and size a bitmap drawing area }
- Bitmap: TBitmap;
- begin
- Bitmap := TBitmap.Create;
- Bitmap.Width := Image1.Width;
- Bitmap.Height := Image1.Height;
- Image1.Picture.Graphic := Bitmap;
- CurrentFile := '';
- end;
-
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- FileClearClick(Sender);
- end;
-
-
- end.
-