原始的 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;

posted on 2011-03-31 17:42  万一  阅读(3393)  评论(0编辑  收藏  举报