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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| {-----------------------------------------------------------------------------
Unit Name: Unit1
Author: Propriétaire
Date: 08-mars-2005
Purpose: Dessiner un rectangle de focalisation au dessus des contrôles
contenus par un contrôle conteneur (ici un TPanel)
History:
-----------------------------------------------------------------------------}
unit Unit1;
{!$Define Debugging}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
Panel8: TPanel;
Panel9: TPanel;
Panel10: TPanel;
Panel11: TPanel;
Panel12: TPanel;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Canvas temporaire servant à dessiner le rectangle de focalisation }
Canvas0: TCanvas;
{Flag indiquant qu'une capture est en cours}
Capturing: boolean;
{Flag indiquant s'il y a déjà eu une capture}
Captured: boolean;
{Coordonnées du point de l'angle supérieur gauche de la capture}
StartPlace: TPoint;
{Coordonnées du point de l'angle inférieur droit de la capture}
EndPlace: TPoint;
{ La fonction MakeRect renvoie les coordonnées d'un rectangle délimité
soit soit le couple de points (Pt1, Pt2) ou (Pt2, Pt1).
Inverse, si nécessaire les angles supérieur gauche (Pt1) et inférieur
droit (Pt2). Un contrôle est aussi effectué pour que le rectangle de
focalisation ne sorte pas des limites de Control.}
function MakeRect(Pt1, Pt2: TPoint; Control: TControl): TRect;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
//Pour qu'un simple clic ne dessine pas un point sur Panel1
StartPlace := Point(-1, -1);
end;
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Canvas0 := TCanvas.Create;
Canvas0.Handle := GetDc(0);
if Captured then
//Effacement de l'ancien rectangle de sélection
DrawFocusRect(Canvas0.Handle, MakeRect(StartPlace, EndPlace, Panel1));
//Détermination du point de départ du dessin
StartPlace := Panel1.ClientToScreen(Point(X, Y));
EndPlace := StartPlace;
//Dessin du nouveau rectangle de sélection
DrawFocusRect(Canvas0.Handle, MakeRect(StartPlace, EndPlace, Panel1));
Capturing := true;
Captured := true;
end;
procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
{Ne sert que pour l'utilisation de la fonction IntersectRect}
OutRect: TRect;
{Contient les coordonnées du composant à examiner}
Rect1: TRect;
{Contient les coordonnées du rectangle de focalisation}
Rect2: TRect;
i: integer;
APanel: TPanel;
begin
if Capturing then
begin
//Effacement de l'ancien rectangle de sélection
DrawFocusRect(Canvas0.Handle, MakeRect(StartPlace, EndPlace, Panel1));
{Actualiser les coordonnées du rectangle de focalisation}
EndPlace := Panel1.ClientToScreen(Point(X, Y));
{On examine chaque contrôle enfant de Panel1}
for i := 0 to Panel1.ControlCount - 1 do
if Panel1.Controls[i] is TPanel then
begin
APanel := Panel1.Controls[i] as TPanel;
//Rect1 contient les coordonnées du bouton examiné
Rect1 := APanel.BoundsRect;
Rect1.TopLeft := APanel.ClientToScreen(Point(0,0));
Rect1.BottomRight := APanel.ClientToScreen(Point(APanel.Width, APanel.Height));
{$IfDef Debugging}
APanel.Caption := Format('T=%d, L=%d, B=%D, R=%d', [Rect1.Top,
Rect1.Left, Rect1.Bottom, Rect1.Right]);
{$EndIf}
//Rect2 contient les coordonnées du cadre de sélection
Rect2 := MakeRect(StartPlace, EndPlace, Panel1);
if IntersectRect(OutRect, Rect1, Rect2) then
APanel.Color := clYellow
else
APanel.Color := clBtnFace;
{forcer un redessinement des composants avant le dessin
du rectangle de focalisation}
Update;
{$IfDef Debugging}
StatusBar1.SimpleText :=
Format('Top : %d - Left : %d - Bottom : %d - Right : %d', [Rect2.Top,
Rect2.Left, Rect2.Bottom, Rect2.Right]);
{$EndIf}
end;
//Dessin du nouveau rectangle de sélection
DrawFocusRect(Canvas0.Handle, MakeRect(StartPlace, EndPlace, Panel1));
end;
end;
procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ReleaseDC(0, Canvas0.Handle);
Canvas0.Free;
Capturing := false;
end;
function TForm1.MakeRect(Pt1, Pt2: TPoint; Control: TControl): TRect;
var
Bounds: TRect;
begin
{On vérifie s'il faut inverser les coordonnées des angles
supérieur gauche et inférieur droit}
if pt1.x < pt2.x then
begin
Result.Left := pt1.x;
Result.Right := pt2.x;
end
else
begin
Result.Left := pt2.x;
Result.Right := pt1.x;
end;
if pt1.y < pt2.y then
begin
Result.Top := pt1.y;
Result.Bottom := pt2.y;
end
else
begin
Result.Top := pt2.y;
Result.Bottom := pt1.y;
end;
{pour limiter le dessin du rectangle de focalisation
au contrôle Control.Cette partie peut être rendue facultative}
{Traduction des limites en coordonnées écran}
Bounds.TopLeft := Control.ClientToScreen(Point(0, 0));
Bounds.BottomRight := Control.ClientToScreen(Point(Control.Width, Control.Height));
{Vérification des limites}
if Result.Left < Bounds.Left then
Result.Left := Bounds.Left;
if Result.Top < Bounds.Top then
Result.Top := Bounds.Top;
if Result.Right > Bounds.Right then
Result.Right := Bounds.Right;
if Result.Bottom > Bounds.Bottom then
Result.Bottom := Bounds.Bottom;
end;
end. |
Partager