Difference between revisions of "BGRABitmap tutorial"
(→Add some drawing: corrected OnPaint handler creation) |
(→Run the program: wiki) |
||
Line 124: | Line 124: | ||
You should obtain a window filled in black with an orange rectangle in it. | You should obtain a window filled in black with an orange rectangle in it. | ||
− | [[BGRABitmap tutorial 2|Go to next tutorial]] | + | [[BGRABitmap tutorial 2|Go to next tutorial (image loading)]] |
Revision as of 14:30, 12 March 2011
This first tutorial shows you how to use BGRABitmap library.
Create a new project
Create an windowed application with menu Project > New project.
The main form unit should look like this : <delphi>unit UMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;
type
{ TForm1 }
TForm1 = class(TForm) private { private declarations } public { public declarations } end;
var
Form1: TForm1;
implementation
initialization
{$I UMain.lrs}
end.</delphi>
If you do not find it, use Ctrl-F12 to show file list.
Save your project next to BGRABitmap library with menu File > Save all.
Add reference to BGRABitmap
In the unit clause, add a reference to BGRABitmap and BGRABitmapTypes after Dialogs.
<delphi>uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, BGRABitmap, BGRABitmapTypes;</delphi>
And go to compiler options with menu Project > Compiler options. In other unit files path, add the relative path to BGRABitmap. For example, if BGRABitmap is in a folder next to your project, the relative path could be "..\BGRABitmap".
If you copy BGRABitmap files in the same folder as your project, then you do not need to add such search path. However, it is not recommended because if you have multiple projects using the library, it could become a repetitive task to update to a new version of the library.
If you are lost with relative path, you can also add the relative path by adding the BGRABitmap unit to your project. To do so, open within your project the file bgrabitmap.pas. Then use menu Project > Add file to project. Lazarus will ask if you want to add the file and the new directory to the project.
Add some drawing
Add a painting event. To do this, click on the form, then go to the object inspector, in the event tab, and double click on the OnPaint line. Lazarus will add automatically a FormPaint handler to the main form unit. Add for example the following code inside it : <delphi>procedure TForm1.FormPaint(Sender: TObject); var bmp: TBGRABitmap; begin
bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack); bmp.FillRect(20, 20, 100, 40, BGRA(255,192,0), dmSet); //fill an orange rectangle bmp.Draw(Canvas, 0, 0, True); //render BGRABitmap on the form bmp.Free; //free memory
end;</delphi>
As you can see, you need to define a TBGRABitmap variable and create it. There are several constructors for TBGRABitmap. The one used here creates a bitmap of size ClientWidth x ClientHeight and filled with black. ClientWidth and ClientHeight are form properties that return the available space for drawing inside the form.
The FillRect procedure takes usual parameters for drawing a rectangle, that is the upper-left corner followed by the lower-right corner plus 1. It means that the pixel at (100,40) is excluded from the rectangle.
After that, there is a color parameter with red/green/blue components, and a drawing mode. dmSet means to simply replace the pixels.
Do not forget to free the object after using it, to avoid a memory leak.
Resulting code
You should obtain the following code: <delphi>unit UMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, BGRABitmap, BGRABitmapTypes;
type
{ TForm1 }
TForm1 = class(TForm) procedure FormPaint(Sender: TObject); private { private declarations } public { public declarations } end;
var
Form1: TForm1;
implementation
{ TForm1 }
procedure TForm1.FormPaint(Sender: TObject); var bmp: TBGRABitmap; begin
bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRABlack); bmp.FillRect(20,20,100,40,BGRA(255,192,0),dmSet); bmp.Draw(Canvas,0,0,True); bmp.Free;
end;
initialization
{$I UMain.lrs}
end.</delphi>
Run the program
You should obtain a window filled in black with an orange rectangle in it.