合并通过 ID2D1Geometry.CombineWithGeometry() 完成,但返回的是 ID2D1SimplifiedGeometrySink(ID2D1GeometrySink 的父对象);
需要把 ID2D1SimplifiedGeometrySink 添加到 ID2D1PathGeometry 才能使用。

uses Direct2D, D2D1;

procedure TForm1.FormPaint(Sender: TObject);
var
  cvs: TDirect2DCanvas;
  iEllipseGeometry1, iEllipseGeometry2: ID2D1EllipseGeometry;
  iGeometrySink: ID2D1GeometrySink;
  iPathGeometry1,iPathGeometry2,iPathGeometry3,iPathGeometry4: ID2D1PathGeometry;
  ptCenter: TD2DPoint2f;
  r: Single;
  matrix: TD2DMatrix3x2F;
begin
  ptCenter := D2D1PointF(100, 100);
  r := 50;
  matrix := TD2DMatrix3x2F.Identity;

  {两个源对象}
  D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry1);
  ptCenter.x := ptCenter.x + r;
  D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry2);

  {UNION 运算}
  D2DFactory.CreatePathGeometry(iPathGeometry1);
  iPathGeometry1.Open(iGeometrySink);
    iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_UNION, matrix, 0, iGeometrySink);
  iGeometrySink.Close;

  {INTERSECT 运算}
  D2DFactory.CreatePathGeometry(iPathGeometry2);
  iPathGeometry2.Open(iGeometrySink);
    iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_INTERSECT, matrix, 0, iGeometrySink);
  iGeometrySink.Close;

  {XOR 运算}
  D2DFactory.CreatePathGeometry(iPathGeometry3);
  iPathGeometry3.Open(iGeometrySink);
    iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_XOR, matrix, 0, iGeometrySink);
  iGeometrySink.Close;

  {EXCLUDE 运算}
  D2DFactory.CreatePathGeometry(iPathGeometry4);
  iPathGeometry4.Open(iGeometrySink);
    iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, D2D1_COMBINE_MODE_EXCLUDE, matrix, 0, iGeometrySink);
  iGeometrySink.Close;

  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
  cvs.BeginDraw;
  cvs.Pen.Color := clWebCornFlowerBlue;
  cvs.Brush.Color := clWebCornFlowerBlue;
  cvs.Brush.Handle.SetOpacity(0.75);

  cvs.FillGeometry(iPathGeometry1);
  cvs.DrawGeometry(iEllipseGeometry1);
  cvs.DrawGeometry(iEllipseGeometry2);

  cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180, 0));
  cvs.FillGeometry(iPathGeometry2);
  cvs.DrawGeometry(iEllipseGeometry1);
  cvs.DrawGeometry(iEllipseGeometry2);

  cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180*2, 0));
  cvs.FillGeometry(iPathGeometry3);
  cvs.DrawGeometry(iEllipseGeometry1);
  cvs.DrawGeometry(iEllipseGeometry2);

  cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180*3, 0));
  cvs.FillGeometry(iPathGeometry4);
  cvs.DrawGeometry(iEllipseGeometry1);
  cvs.DrawGeometry(iEllipseGeometry2);

  cvs.EndDraw;
  cvs.Free;
end;


效果图:



简化后的代码:

uses Direct2D, D2D1;

procedure TForm1.FormPaint(Sender: TObject);
var
  cvs: TDirect2DCanvas;
  iEllipseGeometry1, iEllipseGeometry2: ID2D1EllipseGeometry;
  iGeometrySink: ID2D1GeometrySink;
  iPathGeometrys: array[0..3] of ID2D1PathGeometry;
  ptCenter: TD2DPoint2f;
  r: Single;
  i: Integer;
begin
  ptCenter := D2D1PointF(100, 100);
  r := 50;

  D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry1);
  ptCenter.x := ptCenter.x + r;
  D2DFactory.CreateEllipseGeometry(D2D1Ellipse(ptCenter, r, r), iEllipseGeometry2);

  for i := 0 to 3 do
  begin
    D2DFactory.CreatePathGeometry(iPathGeometrys[i]);
    iPathGeometrys[i].Open(iGeometrySink);
      iEllipseGeometry1.CombineWithGeometry(iEllipseGeometry2, i, TD2DMatrix3x2F.Identity, 0, iGeometrySink);
    iGeometrySink.Close;
  end;

  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
  cvs.BeginDraw;
  cvs.Pen.Color := clWebCornFlowerBlue;
  cvs.Brush.Color := clWebCornFlowerBlue;
  cvs.Brush.Handle.SetOpacity(0.75);

  for i := 0 to 3 do
  begin
    cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(180*i, 0));
    cvs.FillGeometry(iPathGeometrys[i]);
    cvs.DrawGeometry(iEllipseGeometry1);
    cvs.DrawGeometry(iEllipseGeometry2);
  end;

  cvs.EndDraw;
  cvs.Free;
end;


posted on 2011-04-06 20:39  万一  阅读(1843)  评论(0编辑  收藏  举报