Sign in Join
Wiki copytemporary

BGRABitmap tutorial 1/ru: what changed

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

OldNewLine
11Этот первый урок покажет Вам как использовать [[BGRABitmap]] библиотеку.
22
33Вы можете скачать библиотеку по ссылке [GitHub](https://github.com/bgrabitmap).
44
55### Создаём новый проект
66
77Создайте оконное приложение с помощью меню **Project \> New project**.
88
99The 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) :
1010
1111``` pascal
1212unit UMain;
1313
1414{$mode objfpc}{$H+}
1515
1616interface
1717
1818uses
1919 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;
2020
2121type
2222
2323 { TForm1 }
2424
2525 TForm1 = class(TForm)
2626 private
2727 { private declarations }
2828 public
2929 { public declarations }
3030 end;
3131
3232var
3333 Form1: TForm1;
3434
3535implementation
3636
3737initialization
3838 {$I UMain.lrs}
3939
4040end.
4141```
4242
4343If you do not find it, use **Ctrl-F12** to show file list.
4444
4545Save your project next to BGRABitmap library with menu **File \> Save all** (not necessarily in the same folder).
4646
4747### Add reference to BGRABitmap
4848
4949The 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.
5050
5151In the unit clause, add a reference to BGRABitmap and BGRABitmapTypes after Dialogs.
5252
5353``` pascal
5454uses
5555 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
5656 BGRABitmap, BGRABitmapTypes;
5757```
5858
5959#### Alternative ways of referencing
6060
6161##### By installing BGRABitmap package
6262
6363Open 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.
6464
6565If 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.
6666
6767##### By adding BGRABitmap units to the search path
6868
6969Another 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".
7070
7171If 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.
7272
7373If 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.
7474
7575### Add some drawing
7676
7777Add 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 :
7878
7979``` pascal
8080procedure TForm1.FormPaint(Sender: TObject);
8181var bmp: TBGRABitmap;
8282begin
8383 bmp := TBGRABitmap.Create(ClientWidth, ClientHeight, BGRABlack);
8484 bmp.FillRect(20, 20, 100, 40, BGRA(255,192,0), dmSet); //fill an orange rectangle
8585 bmp.Draw(Canvas, 0, 0, True); //render BGRABitmap on the form
8686 bmp.Free; //free memory
8787end;
8888```
8989
9090As 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.
9191
9292The 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.
9393
9494After that, there is a color parameter with red/green/blue components, and a drawing mode. dmSet means to simply replace the pixels.
9595
9696Do not forget to free the object after using it, to avoid a memory leak.
9797
9898### Resulting code
9999
100100You should obtain the following code:
101101
102102``` pascal
103103unit UMain;
104104
105105{$mode objfpc}{$H+}
106106
107107interface
108108
109109uses
110110 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
111111 BGRABitmap, BGRABitmapTypes;
112112
113113type
114114
115115 { TForm1 }
116116
117117 TForm1 = class(TForm)
118118 procedure FormPaint(Sender: TObject);
119119 private
120120 { private declarations }
121121 public
122122 { public declarations }
123123 end;
124124
125125var
126126 Form1: TForm1;
127127
128128implementation
129129
130130{ TForm1 }
131131
132132procedure TForm1.FormPaint(Sender: TObject);
133133var bmp: TBGRABitmap;
134134begin
135135 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRABlack);
136136 bmp.FillRect(20,20,100,40,BGRA(255,192,0),dmSet);
137137 bmp.Draw(Canvas,0,0,True);
138138 bmp.Free;
139139end;
140140
141141initialization
142142 {$I UMain.lrs}
143143
144144end.
145145```
146146
147147### Run the program
148148
149149You should obtain a window filled in black with an orange rectangle in it.
150150
151-<figure>
152-<img src="BGRATutorial1.png" title="BGRATutorial1.png" />
153-<figcaption>BGRATutorial1.png</figcaption>
154-</figure>
151+![](/wiki-images/e/ea/BGRATutorial1.png)
155152
156153[[BGRABitmap tutorial 2|Go to next tutorial (image loading)]]
157154
158155---
159156*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