Cazax
2009-02-07 14:20:44
TImages
A TImage is used in forms to show pictures. I'll explain its most important properties:
Canvas: The canvas of the image, not the picture. The difference will be explained later. What can you do with it? you can draw shapes, change colors, etc.
Width: The width of the image.
Height: The height of the image.
Picture: The picture of the image. It's a type(TPicture) which its important properties are:
Height: The height of the picture.
Width: The width of the picture.
Bitmap: The picture. It has the following properties:
Canvas: The canvas of the picture, not the image. The difference between them is that if you draw in its canvas, this will not affect the TImage. So for example if you draw on the TImage canvas, that will affect every picture on it. Example:You load a picture, draw on the TImage canvas, load another picture and what you draw will be still there, because you drawed on the TImage canvas, not the picture's. To draw on a canvas you have the following procedure:
Arc, Chord, Ellipse, LineTo, MoveTo, Pie, PolyBezier, PolyBezierTo, PolyLine, Rectangle, RoundRect, TextHeight, TextOut and TextWidth.
Height: The height of the bitmap.
Width: The width of the bitmap.
TransparentColor: The transparent color of the bitmap. An example would be: Picture.TransparentColor := FrmDesign.Color, so the transparent color will be the same as the form color.
TOpenDialog/TSaveDialog
TOpenDialogs/TSaveDialog are used to load files(any type). Important properties:
Execute: When called it will popup the load dialog. Returns true if user loaded a file.
Filter: The filter is used to define the extension
OpenDialog1.Filter := '|Bitmap|*.bmp|PNG|*.png';With that the user is only allowed to load/save files(images) with the extension .bmp or .png.FileName: The location of the file loaded/saved. Example: C:\Program Files\SCAR 3.15\Scripts\Example.bmp
TFontDialog
They are used to choose a text font, size and other details(color, bold, italic, etc). Important properties:
Execute: Popups the font dialog. Returns true if values were changed.
Font: What the user selected as font, size, color, etc. It has the following properties:
Color: The font color.
Size: The font size.
Style: The font style(italic, bold, underline).
TColorDialog
Used to load a custom/personalized color. Important properties:
Execute: Popups the color dialog. Returns true of color was selected.
Color: The color selected(in numbers).
Applying all the concepts above:
program New;
var
// InitForm variables.
frmDesign : TForm;
Label1 : TLabel;
Image1 : TImage;
Button1, Button2, Button3, Button4 : TButton;
OpenDialog1 : TOpenDialog;
SaveDialog1 : TSaveDialog;
FontDialog1 : TFontDialog;
ColorDialog1 : TColorDialog;
procedure LoadImage(Sender : TObject);
var
Bmp, W, H : Integer;
Canvas : TCanvas;
begin
If OpenDialog1.Execute Then //If the load dialog executed
Begin
Try
Bmp := LoadBitmap(OpenDialog1.FileName); //Loads the image from the image location
Except
Exit; //If the load failed then exit
End;
Canvas := GetBitmapCanvas(Bmp); // We get the bitmap canvas
GetBitmapSize(Bmp, W, H); //We get the bitmap size
If (W < 418) And (H < 329) Then // If the picture won't fuck the form then
begin
Image1.Width := W; //We resize the width
Image1.Height := H; //We resize the height
end;
SafeCopyCanvas(Canvas, Image1.Canvas, 0, 0, W, H, 0, 0, W, H); //We paste the loaded bitmap onto the picture
FreeBitmap(Bmp);
End;
End;
procedure SaveImage(Sender : TObject);
Var
Bmp : Integer;
Begin
If SaveDialog1.Execute Then // If the save dialog executed
Begin
Bmp := BitmapFromString(Image1.Width, Image1.Height, ''); //We create a temporary bitmap
SafeCopyCanvas(Image1.Canvas, GetBitmapCanvas(bmp), 0, 0, Image1.Width, Image1.Height, 0, 0, Image1.Width, Image1.Height); //We paste the picture's canvas onto the remporary bitmap
SaveBitmap(Bmp, SaveDialog1.FileName + '.bmp');//we save our temprary bitmap as a .bmp
FreeBitmap(Bmp);
End;
End;
procedure ChooseColor(Sender : TObject);
Begin
If ColorDialog1.Execute Then // If the color dialog executed
frmDesign.Color := ColorDialog1.Color; //We set the background color of the form to the selected one
End;
procedure ChangeFont(Sender : TObject);
begin
If FontDialog1.Execute Then //If the font dialog executed
Label1.Font := FontDialog1.Font; //We set the title's font to the selected one
end;
// This form was parsed using DFM Form Parser v.26c Beta by Ron.
// Generated from tutorial.dfm.
procedure InitForm;
var
TimeInitForm : Integer;
begin
TimeInitForm := GetSystemTime;
frmDesign := CreateForm;
with frmDesign do
begin
Position := poScreenCenter;
Width := 492;
Height := 552;
Caption := 'Tutorial Example';
Color := clBtnFace;
Font.Color := clWindowText;
Font.Height := -14;
Font.Name := 'MS Sans Serif';
Font.Style := [];
PixelsPerInch := 120;
end;
Label1 := TLabel.Create(frmDesign);
with Label1 do
begin
Parent := frmDesign;
Left := 240;
Top := 24;
Width := 101;
Height := 16;
Caption := 'Tutorial Example';
end;
Image1 := TImage.Create(frmDesign);
with Image1 do
begin
Parent := frmDesign;
Left := 24;
Top := 168;
Width := 417;
Height := 329;
end;
Button1 := TButton.Create(frmDesign);
with Button1 do
begin
Parent := frmDesign;
Left := 16;
Top := 120;
Width := 121;
Height := 33;
Caption := 'Change Title Font';
TabOrder := 8;
end;
Button1.OnClick := @ChangeFont;
Button2 := TButton.Create(frmDesign);
with Button2 do
begin
Parent := frmDesign;
Left := 152;
Top := 120;
Width := 129;
Height := 33;
Caption := 'Load Image';
TabOrder := 9;
end;
Button2.OnClick := @LoadImage;
Button3 := TButton.Create(frmDesign);
with Button3 do
begin
Parent := frmDesign;
Left := 296;
Top := 120;
Width := 145;
Height := 33;
Caption := 'Save Image';
TabOrder := 10;
end;
Button3.OnClick := @SaveImage;
Button4 := TButton.Create(frmDesign);
with Button4 do
begin
Parent := frmDesign;
Left := 8;
Top := 48;
Width := 169;
Height := 33;
Caption := 'Choose Background color';
TabOrder := 11;
end;
Button4.OnClick := @ChooseColor;
OpenDialog1 := TOpenDialog.Create(frmDesign);
OpenDialog1.Filter := 'Bitmap|*.bmp|PNG|*.png|';
SaveDialog1 := TSaveDialog.Create(frmDesign);
SaveDialog1.Filter := 'Bitmap|*.bmp|PNG|*.png|';
FontDialog1 := TFontDialog.Create(frmDesign);
with FontDialog1 do
begin
Font.Color := clWindowText;
Font.Height := -13;
Font.Name := 'MS Sans Serif';
Font.Style := [];
end;
ColorDialog1 := TColorDialog.Create(frmDesign);
end;
procedure SafeInitForm;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('InitForm', v);
end;
procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;
procedure SafeShowInitFormModal;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowInitFormModal', v);
end;
procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;
begin
MainInitForm;
end.^run it on SCAR. I explained everything on it.Now you can add some style to your forms :)