原始的 Direct2D 只提供了幾種簡單圖形(直線、矩形、圓角矩形、橢圓)的繪制與填充,更多集合圖形或路徑的描繪要使用 DrawGeometry() 和 FillGeometry()。
既然能繪制更復雜的圖形,當然也能繪制基本圖形,先試下使用 DrawGeometry()、FillGeometry() 實現矩形、圓角矩形和橢圓。
方法的參數是 ID2D1Geometry 接口,ID2D1RectangleGeometry、ID2D1RoundedRectangleGeometry、ID2D1EllipseGeometry 都是它的子接口。
ID2D1Factory 提供了實現這三個接口的方法,從 D2DFactory() 可獲取 ID2D1Factory 接口。
測試代碼:
uses Direct2D, D2D1;
procedure TForm1.FormPaint(Sender: TObject);
var
cvs: TDirect2DCanvas;
fPt: TD2DPoint2f;
fLeft,fTop: Single;
IEllipse: ID2D1EllipseGeometry;
iRectangle: ID2D1RectangleGeometry;
iRoundedRectangle: ID2D1RoundedRectangleGeometry;
iFactory: ID2D1Factory;
begin
fPt := D2D1PointF(ClientWidth / 2, ClIEntHeight / 2);
fLeft := ClIEntWidth / 4;
fTop := ClIEntHeight / 4;
iFactory := D2DFactory();
iFactory.CreateRectangleGeometry(D2D1RectF(fLeft, fTop, fLeft*3, fTop*3), iRectangle);
iFactory.CreateRoundedRectangleGeometry(D2D1RoundedRect(D2D1RectF(fLeft, fTop, fLeft*3, fTop*3), 32, 32), iRoundedRectangle);
iFactory.CreateEllipseGeometry(D2D1Ellipse(fPt, fLeft, fTop), IEllipse);
cvs := TDirect2DCanvas.Create(Canvas, ClIEntRect);
cvs.BeginDraw;
cvs.Pen.Color := clRed;
cvs.Brush.Color := clBlack;
cvs.FillGeometry(iRectangle);
cvs.Brush.Color := clGreen;
cvs.FillGeometry(iRoundedRectangle);
cvs.Brush.Color := clBlue;
cvs.FillGeometry(IEllipse);
cvs.DrawGeometry(iRectangle);
cvs.DrawGeometry(iRoundedRectangle);
cvs.DrawGeometry(IEllipse);
cvs.EndDraw;
cvs.Free;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Repaint;
end;