basic delphi programming
This tutorial is a rough guide of how to use the graphics functions in Delphi 7.0 (This should work with minor modifications in Kylix 3 for Linux)
- The basic code with a blank form in Delphi is this:-
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm) Procedures are defined here!
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1; Your Variables Go here!
implementation
{$R *.dfm} Procedures go here!
end.
- On the left of your screen is the object inspector. Click on your form. Then in the object inspector select the events tab. We want the mouse to draw a line whenever it is over the form. We can use OnMouseMove - double
click and in your code window you will get a new procedure like this...
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
end;
Your code goes between the begin and end; End; has a semicolon to show it is not the last end and it only applies to the procedure. The procedure has given us the integers X and Y which are the mouse positions. As in
C you must end each line with a semicolon. Because Delphi is object orientated everything is in a heirachy.
- To draw on the "canvas" use...
Canvas.lineto(X,Y);
Delphi provides you with a menu exery time you put a full stop in so you can see the available options. If we put the above code into the FormMouseMove procedure then a line would follow the mouse. Supposing we
want to set the default color for a line. Click on your form. Then in the object inspector select the events tab. Double click OnCreate. You are provided with a function again. Type Canvas. and a pull-down menu will appear.
Select Brush. Your code should now read canvas.brush. . Once again a pull-down will appear so select color from it.... canvas.brush.color .
- To set a variable in Delphi use := so for example if we use a color dialog to select the color when the form is created (To put the code for a dialog into your program from the drawing toolbar select the dialog tab and then
click on it and draw it on the form - this will be invisible when the program is run). Then add this code to your procedure....
Colordialog1.execute;
Canvas.Brush.Color:=colordialog1.color;
You can of course put this code under any procedure. You can also set the color manually by using Canvas.Brush.Color:=ClRed .
|