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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
btnFill: TButton;
edFillCount: TEdit;
PaintBox1: TPaintBox;
rgrpCaptureMode: TRadioGroup;
ckbCaptureSizeStrict: TCheckBox;
procedure btnFillClick(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure rgrpCaptureModeClick(Sender: TObject);
procedure ckbCaptureSizeStrictClick(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnFillClick(Sender: TObject);
var
I: Integer;
begin
ListBox1.Items.BeginUpdate();
try
for I := 0 to StrToInt(edFillCount.Text) do
ListBox1.Items.Add(I.ToString());
finally
ListBox1.Items.EndUpdate();
end;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
procedure CaptureByCopyRect(AListBox: TListBox; ACanvas: TCanvas; AWidth, AHeight: Integer);
var
VRect: TRect;
begin
VRect := Rect(0, 0, AWidth, AHeight);
ACanvas.CopyRect(VRect, AListBox.Canvas, VRect);
end;
procedure CaptureByWindowDC(AListBox: TListBox; ACanvas: TCanvas; AWidth, AHeight: Integer);
var
SourceHDC: HDC;
VRect: TRect;
SourceCanvas: TCanvas;
DestBitmap: TBitmap;
begin
SourceHDC := GetWindowDC(AListBox.Handle);
try
SourceCanvas := TCanvas.Create;
try
SourceCanvas.Handle := SourceHDC;
DestBitmap := TBitmap.Create;
try
DestBitmap.Width := AWidth;
DestBitmap.Height := AHeight;
VRect := Rect(0, 0, AWidth, AHeight);
DestBitmap.Canvas.CopyRect(VRect, SourceCanvas, VRect);
ACanvas.Draw(0, 0, DestBitmap);
finally
FreeAndNil(DestBitmap);
end;
finally
FreeAndNil(SourceCanvas);
end;
finally
ReleaseDC(AListBox.Handle, SourceHDC);
end;
end;
procedure CaptureByWindowDCSimplify(AListBox: TListBox; ACanvas: TCanvas; AWidth, AHeight: Integer);
var
SourceHDC: HDC;
VRect: TRect;
SourceCanvas: TCanvas;
begin
SourceHDC := GetWindowDC(AListBox.Handle);
try
SourceCanvas := TCanvas.Create;
try
SourceCanvas.Handle := SourceHDC;
VRect := Rect(0, 0, AWidth, AHeight);
ACanvas.CopyRect(VRect, SourceCanvas, VRect);
finally
FreeAndNil(SourceCanvas);
end;
finally
ReleaseDC(AListBox.Handle, SourceHDC);
end;
end;
procedure CaptureByFormAndPaintTo(AListBox: TListBox; ACanvas: TCanvas; AWidth, AHeight: Integer);
var
VForm: TForm;
VParent: TWinControl;
VBoundsRect: TRect;
VRect: TRect;
begin
VForm := TForm.Create(nil);
try
//VForm.Position := poDesigned;
//VForm.Left := Screen.Width;
VForm.AutoSize := True;
VForm.BorderStyle := bsNone;
VParent := AListBox.Parent;
VBoundsRect := AListBox.BoundsRect;
try
AListBox.Parent := VForm;
VForm.Show();
AListBox.Height := AHeight;
VForm.Refresh();
AListBox.PaintTo(ACanvas, 0, 0);
finally
AListBox.Parent := VParent;
AListBox.SetBounds(VBoundsRect.Left, VBoundsRect.Top, VBoundsRect.Width, VBoundsRect.Height);
end;
finally
VForm.Free();
end;
end;
procedure CaptureByFormAndByWindowDCSimplify(AListBox: TListBox; ACanvas: TCanvas; AWidth, AHeight: Integer);
var
VForm: TForm;
VParent: TWinControl;
VBoundsRect: TRect;
VRect: TRect;
begin
VForm := TForm.Create(nil);
try
//VForm.Position := poDesigned;
//VForm.Left := Screen.Width;
VForm.AutoSize := True;
VForm.BorderStyle := bsNone;
VParent := AListBox.Parent;
VBoundsRect := AListBox.BoundsRect;
try
AListBox.Parent := VForm;
VForm.Show();
AListBox.Height := AHeight;
VForm.Refresh();
CaptureByWindowDCSimplify(AListBox, ACanvas, AWidth, AHeight);
finally
AListBox.Parent := VParent;
AListBox.SetBounds(VBoundsRect.Left, VBoundsRect.Top, VBoundsRect.Width, VBoundsRect.Height);
end;
finally
VForm.Free();
end;
end;
var
LWidth, LHeight: Integer;
begin
if ckbCaptureSizeStrict.Checked then
begin
LWidth := ListBox1.Width;
LHeight := ListBox1.Height;
end
else
begin
LWidth := PaintBox1.Width;
LHeight := PaintBox1.Height;
end;
case rgrpCaptureMode.ItemIndex of
0: ListBox1.PaintTo(PaintBox1.Canvas, 0, 0);
1: CaptureByCopyRect(ListBox1, PaintBox1.Canvas, LWidth, LHeight);
2: CaptureByWindowDC(ListBox1, PaintBox1.Canvas, LWidth, LHeight);
3: CaptureByWindowDCSimplify(ListBox1, PaintBox1.Canvas, LWidth, LHeight);
4: CaptureByFormAndPaintTo(ListBox1, PaintBox1.Canvas, LWidth, LHeight);
5: CaptureByFormAndByWindowDCSimplify(ListBox1, PaintBox1.Canvas, LWidth, LHeight);
end;
end;
procedure TForm1.rgrpCaptureModeClick(Sender: TObject);
begin
PaintBox1.Invalidate();
end;
procedure TForm1.ckbCaptureSizeStrictClick(Sender: TObject);
begin
PaintBox1.Invalidate();
end;
end. |
Partager