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
| unit Unit9;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm9 = class(TForm)
PaintBox1: TPaintBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
private
fDesktopScreen : TBitmap;
public
{ Déclarations publiques }
end;
var
Form9: TForm9;
implementation
{$R *.dfm}
var
R : TRect;
WR, HR, MX, MY : integer;
procedure GetDesktopScreenShot(BMP: TBitmap);
var
Buffer : TBitmap;
begin
Buffer := TBitmap.Create;
try
Buffer.Width := Screen.Width;
Buffer.Height := Screen.Height;
Buffer.PixelFormat := pf32bit;
BitBlt(Buffer.Canvas.Handle, 0, 0, Screen.Width, Screen.Height,
GetDC(GetDesktopWindow), 0, 0, SRCCOPY);
BMP.Assign(Buffer);
finally
Buffer.Free;
end;
end;
procedure TForm9.FormCreate(Sender: TObject);
begin
DoubleBuffered := true;
fDesktopScreen := TBitmap.Create;
GetDesktopScreenShot(fDesktopScreen);
end;
procedure TForm9.FormDestroy(Sender: TObject);
begin
fDesktopScreen.Free;
end;
procedure TForm9.FormResize(Sender: TObject);
begin
// calcul des dimensions des zones
WR := PaintBox1.Width div 10;
HR := PaintBox1.Height div 10;
PaintBox1.Invalidate;
end;
procedure TForm9.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
// dans quelle zone est le curseur ?
MX := X div WR;
MY := Y div HR;
caption := format('%d x %d',[MX, MY]);
PaintBox1.Invalidate;
end;
procedure TForm9.PaintBox1Paint(Sender: TObject);
begin
if assigned(fDesktopScreen) then
begin
// dessin de l'image redimensionée
SetStretchBltMode(PaintBox1.Canvas.Handle, HALFTONE);
StretchBlt(PaintBox1.Canvas.Handle, 0, 0, PaintBox1.Width, PaintBox1.Height,
fDesktopScreen.Canvas.Handle, 0, 0, fDesktopScreen.Width, fDesktopScreen.Height,
SRCCOPY);
PaintBox1.Canvas.Brush.Style := bsClear;
PaintBox1.Canvas.Pen.Color := clRed;
// s'assure d'être dans l'image
if MX < 0 then
MX := 0
else
if MX > PaintBox1.Width then
MX := PaintBox1.Width;
if MY < 0 then
MY := 0
else
if MY > PaintBox1.Height then
MY := PaintBox1.Height;
// calcul de la zone à afficher
R.Left := MX * WR;
R.Top := MY * HR;
R.Right := R.Left + WR;
R.Bottom := R.Top + HR;
// dessin de la zone
PaintBox1.Canvas.Rectangle(R);
end;
end;
end. |
Partager