BGRABitmap tutorial 3/fr
│ Deutsch (de) │ English (en) │ español (es) │ français (fr) │
Accueil | Tutoriel 1 | Tutoriel 2 | Tutoriel 3 | Tutoriel 4 | Tutoriel 5 | Tutoriel 6 | Tutoriel 7 | Tutoriel 8 | Tutoriel 9 | Tutoriel 10 | Tutoriel 11 | Tutoriel 12 | Edit
Ce tutorial montre comment dessiner avec la souris.
Création d'un nouveau projet
Créez un nouveau projet et ajouter la référence à BGRABitmap, de la même façon que dans le premier tutorial.
Création d'une nouvelle image
Ajoutez une variable privée à la fenêtre principale pour stocker l'image : <delphi> TForm1 = class(TForm)
private { private declarations } image: TBGRABitmap; public { public declarations } end; </delphi>
Créez l'image quand la fenêtre est créée. Pour faire cela, double-cliquez sur la fenêtre. Une procédure devrait apparaitre dans l'éditeur de code. Ajoutez l'instruction de création : <delphi>procedure TForm1.FormCreate(Sender: TObject); begin
image := TBGRABitmap.Create(640,480,BGRAWhite); //crée une image 640x480
end; </delphi>
Dessin de l'image
Ajouter un gestionnaire OnPaint. Pour cela, cliquez sur la fenêtre, allez dans l'inspecteur d'objet, dans l'onglet événement et double-cliquez sur la ligne OnPaint. Ensuite, ajoutez le code suivant : <delphi>procedure TForm1.FormPaint(Sender: TObject); begin
PaintImage;
end; </delphi>
Ajoutez la procédure PaintImage : <delphi>procedure TForm1.PaintImage; begin
image.Draw(Canvas,0,0,True);
end; </delphi>
Après avoir écrit cela, mettez le curseur texte sur PaintImage et pressez Ctrl-Shift-C pour ajouter la déclaration à l'interface.
Gestion de la souris
Avec l'inspecteur d'objet, ajouter des gestionnaires pour les évènements MouseDown et MouseMove : <delphi>procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then DrawBrush(X,Y);
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then DrawBrush(X,Y);
end;</delphi>
Ajoutez la procédure DrawBrush : <delphi>procedure TForm1.DrawBrush(X, Y: Integer); const radius = 5; begin
image.GradientFill(X-radius,Y-radius, X+radius,Y+radius, BGRABlack,BGRAPixelTransparent, gtRadial, PointF(X,Y), PointF(X+radius,Y), dmDrawWithTransparency);
PaintImage;
end;</delphi>
Après avoir écrit cela, mettez le curseur texte sur DrawBrush et pressez Ctrl-Shift-C pour ajouter la déclaration à l'interface.
Cette procédure dessine un gradient radial (gtRadial) :
- le rectangle encadrant est (X-radius,Y-radius, X+radius,Y+radius).
- le centre est noir, le bord est transparent
- le centre est à (X,Y) et le bord à (X+radius,Y)
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 FormCreate(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormPaint(Sender: TObject); private { private declarations } image: TBGRABitmap; procedure DrawBrush(X, Y: Integer); procedure PaintImage; public { public declarations } end;
var
Form1: TForm1;
implementation
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject); begin
image := TBGRABitmap.Create(640,480,BGRAWhite);
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then DrawBrush(X,Y);
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then DrawBrush(X,Y);
end;
procedure TForm1.FormPaint(Sender: TObject); begin
PaintImage;
end;
procedure TForm1.DrawBrush(X, Y: Integer); const radius = 20; begin
image.GradientFill(X-radius,Y-radius, X+radius,Y+radius, BGRABlack,BGRAPixelTransparent,gtRadial, PointF(X,Y), PointF(X+radius,Y), dmDrawWithTransparency);
PaintImage;
end;
procedure TForm1.PaintImage; begin
image.Draw(Canvas,0,0,True);
end;
initialization
{$I UMain.lrs}
end.</delphi>
Exécution du programme
Vous pouvez dessiner sur la fenêtre.
Previous tutorial (image loading) | Next tutorial (direct pixel access)