Bizarre, il n'y a rien de particulier dans cette application créée avec Delphi 2005...
Bon, alors, il ne te reste plus qu'à créer toi même la fiche:
Crée un nouveau projet.
Dans les propriétés de la feuille, modifie la propriété Name en fPrincipale.
Défini le type suivant dans la section interface:
TSensDeplacement = Set of (sdHaut, sdDroite, sdBas, sdGauche);
Défini les variables globales:
1 2 3 4 5 6 7
|
var
BoutonGPresse: Boolean;
BoutonDPresse: Boolean;
posDepart: TPoint;
BaseImage: TPoint;
SensDeplacement: TSensDeplacement; |
Dans l'événement OnCreate, mets le code:
1 2 3 4 5 6 7 8 9 10
| procedure TfPrincipale.FormCreate(Sender: TObject);
begin
AutoScroll:=False;
Caption:='Sélection dans TImage (clic-droit pour sélectionner, clic -gauche pour déplacer l''image)';
imgTest.AutoSize:=True;
imgTest.Left:=0;
imgTest.Top:=0;
shpSelection.BringToFront;
odOuvrir.Filter:= 'Images [*.bmp, *.jpg]|*.bmp; *.jpg';
end; |
Dans l'évenement OnShow:
1 2 3 4 5 6 7 8
| procedure TfPrincipale.FormShow(Sender: TObject);
begin
DoubleBuffered:=True;
if odOuvrir.Execute=True then
imgTest.Picture.LoadFromFile(odOuvrir.FileName)
else
Application.Terminate;
end; |
Place sur la feuille un TImage nommé imgTest
Dans l'événement OnMouseDown du TImage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| procedure TfPrincipale.imgTestMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
shpSelection.Width:=0;
shpSelection.Height:=0;
GetCursorPos(posDepart);
case Button of
mbLeft:
begin
BoutonGPresse:=True;
end;
mbRight:
begin
BoutonDPresse:=True;
posDepart.X:=X;
posDepart.Y:=Y;
shpSelection.Left:=imgTest.Left+X;
shpSelection.Top:=imgTest.Top+Y;
posDepart.X:=shpSelection.Left;
posDepart.Y:=shpSelection.Top;
end;
end;
end; |
Dans l'événement OnMouseMove du TImage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| procedure TfPrincipale.imgTestMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
const
Delta = 10;
var
posCourante: TPoint;
begin
GetCursorPos(posCourante);
if BoutonGPresse=True then with imgTest do
begin
Left:=BaseImage.X+(posCourante.X-posDepart.X);
Top:=BaseImage.Y+(posCourante.Y-posDepart.Y);
end;
if BoutonDPresse=True then with shpSelection do
begin
Width:=BaseImage.X+(X-posDepart.X);
height:=BaseImage.Y+(Y-posDepart.Y);
SensDeplacement:=[];
if BaseImage.X+X<Delta then
SensDeplacement:=[sdGauche];
if BaseImage.Y+Y<Delta then
SensDeplacement:=SensDeplacement+[sdHaut];
if BaseImage.X+X>fPrincipale.ClientWidth-Delta then
SensDeplacement:=SensDeplacement+[sdDroite];
if BaseImage.Y+Y>fPrincipale.ClientHeight-Delta then
SensDeplacement:=SensDeplacement+[sdBas];
tmrDeplace.Enabled:=SensDeplacement<>[];
end;
end; |
Dans l'événement OnMouseUp du TImage:
1 2 3 4 5 6 7 8
| procedure TfPrincipale.imgTestMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BoutonGPresse:=False;
BoutonDPresse:=False;
BaseImage.X:=imgTest.Left;
BaseImage.Y:=imgTest.Top;
end; |
Place un TShape sur la feuille et nomme-le shpSelection
Dans le OnMouseMove du TShape:
1 2 3 4 5 6
| procedure TfPrincipale.shpSelectionMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
begin
shpSelection.Width:=shpSelection.Left+(X-posDepart.X);
shpSelection.height:=shpSelection.Top+(Y-posDepart.Y);
end; |
Place un TTimer sur la feuille et nomme-le tmrDeplace
Dans l'événement OnTimer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| procedure TfPrincipale.tmrDeplaceTimer(Sender: TObject);
const
Delta = 10;
begin
if (sdHaut in SensDeplacement) and (imgTest.Top<0) then
begin
Inc(BaseImage.Y, Delta);
Inc(posDepart.Y, Delta);
imgTest.Top:=imgTest.Top+Delta;
shpSelection.Top:=shpSelection.Top+Delta;
shpSelection.Width:=shpSelection.Width+Delta;
end;
if (sdDroite in SensDeplacement)
and (imgTest.Left>(ClientWidth-imgTest.Width)) then
begin
Dec(BaseImage.X, Delta);
Dec(posDepart.X, Delta);
imgTest.Left:=imgTest.Left-Delta;
shpSelection.Left:=shpSelection.Left-Delta;
shpSelection.Width:=shpSelection.Width+Delta;
end;
if (sdBas in SensDeplacement)
and (imgTest.Top>(ClientHeight-imgTest.Height)) then
begin
Dec(BaseImage.Y, Delta);
Dec(posDepart.Y, Delta);
imgTest.Top:=imgTest.Top-Delta;
shpSelection.Top:=shpSelection.Top-Delta;
shpSelection.Width:=shpSelection.Width-Delta;
end;
if (sdGauche in SensDeplacement) and (imgTest.Left<0) then
begin
Inc(BaseImage.X, Delta);
Inc(posDepart.X, Delta);
imgTest.Left:=imgTest.Left+Delta;
shpSelection.Left:=shpSelection.Left+Delta;
shpSelection.Width:=shpSelection.Width+Delta;
end;
end; |
Pour terminer, place un TOpenDialog sur la feuille en le nommant odOuvrir
Enregistre l'unité sous uPrincipale et le projet sous un nom quelconque. Si je n'ai rien omis, tu devrais pouvoir l'exécuter...
Partager