Sign in Join
Wiki copytemporary

BGRABitmap tutorial 1/ru: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: BGRABitmap tutorial 1/ru

OldNewLine
1+Этот первый урок покажет Вам как использовать [[BGRABitmap]] библиотеку.
2+
3+Вы можете скачать библиотеку по ссылке [GitHub](https://github.com/bgrabitmap).
4+
5+### Создаём новый проект
6+
7+Создайте оконное приложение с помощью меню **Project \> New project**.
8+
9+The main form unit should look like this (the 2015 Linux version cannot process the below files, for working samples see the test-apps ("test 4 lcl") in the BGRA source tar-ball) :
10+
11+``` pascal
12+unit UMain;
13+
14+{$mode objfpc}{$H+}
15+
16+interface
17+
18+uses
19+ Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;
20+
21+type
22+
23+ { TForm1 }
24+
25+ TForm1 = class(TForm)
26+ private
27+ { private declarations }
28+ public
29+ { public declarations }
30+ end;
31+
32+var
33+ Form1: TForm1;
34+
35+implementation
36+
37+initialization
38+ {$I UMain.lrs}
39+
40+end.
41+```
42+
43+If you do not find it, use **Ctrl-F12** to show file list.
44+
45+Save your project next to BGRABitmap library with menu **File \> Save all** (not necessarily in the same folder).
46+
47+### Add reference to BGRABitmap
48+
49+The first time you use BGRABitmap, open bgrabitmappack.lpk with Lazarus and click on "Use \> Add to Project". Then, if you need to add the reference in another project, you can do it through the Project inspector and click on "Add... \> New condition" and choose BGRABitmapPack.
50+
51+In the unit clause, add a reference to BGRABitmap and BGRABitmapTypes after Dialogs.
52+
53+``` pascal
54+uses
55+ Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
56+ BGRABitmap, BGRABitmapTypes;
57+```
58+
59+#### Alternative ways of referencing
60+
61+##### By installing BGRABitmap package
62+
63+Open bgrabitmappack.lpk as a package. Make sure it is possible to install it by going in the package window into "Options \> IDE Integration" and setting the package type to "designtime and runtime". Then in the package window, click on install. A dialog pops up, asking if the package must be added to Lazarus and if it needs to be compiled again. Choose Yes twice.
64+
65+If everything is fine, Lazarus restarts and BGRABitmap units are available. If it does not work, you can simply add BGRABitmap to the search path without compiling Lazarus.
66+
67+##### By adding BGRABitmap units to the search path
68+
69+Another way is to add BGRABitmap units to the search path of the project. To do this, 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".
70+
71+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.
72+
73+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.
74+
75+### Add some drawing
76+
77+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 :
78+
79+``` pascal
80+procedure TForm1.FormPaint(Sender: TObject);
81+var bmp: TBGRABitmap;
82+begin
83+ bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack);
84+ bmp.FillRect(20, 20, 100, 40, BGRA(255,192,0), dmSet); //fill an orange rectangle
85+ bmp.Draw(Canvas, 0, 0, True); //render BGRABitmap on the form
86+ bmp.Free; //free memory
87+end;
88+```
89+
90+As you can see, you need to define a [[TBGRABitmap class|TBGRABitmap]] variable and create it. There are several constructors for [[TBGRABitmap class|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.
91+
92+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.
93+
94+After that, there is a color parameter with red/green/blue components, and a drawing mode. dmSet means to simply replace the pixels.
95+
96+Do not forget to free the object after using it, to avoid a memory leak.
97+
98+### Resulting code
99+
100+You should obtain the following code:
101+
102+``` pascal
103+unit UMain;
104+
105+{$mode objfpc}{$H+}
106+
107+interface
108+
109+uses
110+ Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
111+ BGRABitmap, BGRABitmapTypes;
112+
113+type
114+
115+ { TForm1 }
116+
117+ TForm1 = class(TForm)
118+ procedure FormPaint(Sender: TObject);
119+ private
120+ { private declarations }
121+ public
122+ { public declarations }
123+ end;
124+
125+var
126+ Form1: TForm1;
127+
128+implementation
129+
130+{ TForm1 }
131+
132+procedure TForm1.FormPaint(Sender: TObject);
133+var bmp: TBGRABitmap;
134+begin
135+ bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRABlack);
136+ bmp.FillRect(20,20,100,40,BGRA(255,192,0),dmSet);
137+ bmp.Draw(Canvas,0,0,True);
138+ bmp.Free;
139+end;
140+
141+initialization
142+ {$I UMain.lrs}
143+
144+end.
145+```
146+
147+### Run the program
148+
149+You should obtain a window filled in black with an orange rectangle in it.
150+
151+<figure>
152+<img src="BGRATutorial1.png" title="BGRATutorial1.png" />
153+<figcaption>BGRATutorial1.png</figcaption>
154+</figure>
155+
156+[[BGRABitmap tutorial 2|Go to next tutorial (image loading)]]
157+
158+---
159+*From the Free Pascal wiki, page "BGRABitmap tutorial 1/ru", as it stood on 2020-02-09 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history