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
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.Direct2D, WinApi.D2D1, WinApi.Wincodec;
const
BackgroundImage = 'd:\images\monimage.jpg';
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
class var WicFactory :IWICImagingFactory;
D2DCanvas :TDirect2DCanvas;
BkImage :ID2D1Bitmap;
protected
class constructor Create;
procedure CreateWnd; override;
procedure DestroyWnd; override;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
function CreateBitmap(const aFileName: string): ID2D1Bitmap;
end;
var
Form1: TForm1;
implementation
uses Win.ComObj;
{$R *.dfm}
//=====================================================================================================================================================================================================
// Ces quelques méthodes sont immuables
class constructor TForm1.Create;
begin
inherited;
WicFactory := CreateComObject(CLSID_WICImagingFactory) as IWICImagingFactory;
end;
procedure TForm1.CreateWnd;
begin
inherited;
D2DCanvas := TDirect2DCanvas.Create(Handle);
end;
procedure TForm1.DestroyWnd;
begin
D2DCanvas.Free;
inherited;
end;
procedure TForm1.WMPaint(var Message: TWMPaint);
var
PS: TPaintStruct;
begin
BeginPaint(Handle, PS);
try
D2DCanvas.BeginDraw;
try
Paint;
finally
D2DCanvas.EndDraw;
end;
finally
EndPaint(Handle, PS);
end;
end;
procedure TForm1.WMSize(var Message: TWMSize);
begin
if Assigned(D2DCanvas) then
begin
var PixelSize := D2D1SizeU(ClientWidth, ClientHeight);
ID2D1HwndRenderTarget(D2DCanvas.RenderTarget).Resize(PixelSize);
end;
end;
//=====================================================================================================================================================================================================
// Chargement d'une image
function TForm1.CreateBitmap(const aFileName: string): ID2D1Bitmap;
begin
var Image := TWICImage.Create;
try
Image.LoadFromFile(aFileName);
var Converter :IWICFormatConverter;
WicFactory.CreateFormatConverter(Converter);
Converter.Initialize(Image.Handle, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nil, 0, WICBitmapPaletteTypeMedianCut);
D2DCanvas.RenderTarget.CreateBitmapFromWicBitmap(Converter, nil, Result);
finally
Image.Free;
end;
end;
//=====================================================================================================================================================================================================
procedure TForm1.FormCreate(Sender: TObject);
begin
BkImage := CreateBitmap(BackgroundImage);
end;
// -- La routine de dessin habituelle ------
procedure TForm1.FormPaint(Sender: TObject);
var
Pen :ID2D1SolidColorBrush;
Brush :ID2D1LinearGradientBrush;
GradientCollection :ID2D1GradientStopCollection;
begin
// Image de fond
D2DCanvas.RenderTarget.DrawBitmap(BkImage);
// Cercle en dégradé et semi-transparence
var Ellipse := D2D1Ellipse(D2D1PointF(200, 200), 100, 100);
var StartPoint := D2D1PointF(Ellipse.point.x, Ellipse.point.y -Ellipse.radiusY);
var EndPoint := D2D1PointF(Ellipse.point.x, Ellipse.point.y +Ellipse.radiusY);
var GradientStops :TArray<TD2D1GradientStop> := [D2D1GradientStop(0.0, D2D1ColorF(clRed, 1)),
D2D1GradientStop(0.5, D2D1ColorF(clWhite, 0.3)),
D2D1GradientStop(1.0, D2D1ColorF(clGreen, 1))];
D2DCanvas.RenderTarget.CreateGradientStopCollection(@GradientStops[0], Length(GradientStops), D2D1_GAMMA_2_2, D2D1_EXTEND_MODE_CLAMP, GradientCollection);
D2DCanvas.RenderTarget.CreateLinearGradientBrush(D2D1LinearGradientBrushProperties(StartPoint, EndPoint), nil, GradientCollection, Brush);
D2DCanvas.RenderTarget.CreateSolidColorBrush(D2D1ColorF(clRed, 1), nil, Pen);
D2DCanvas.RenderTarget.FillEllipse(Ellipse, Brush);
D2DCanvas.RenderTarget.DrawEllipse(Ellipse, Pen, 3);
end;
end. |
Partager