下面是通过自定义函数绘制菱形的测试代码。

uses Direct2D, D2D1;

{自定义的绘制菱形的函数}
function GetDiamondPath(ptLeft, ptTop: TD2DPoint2f): ID2D1PathGeometry; //返回路径接口
var
  sink: ID2D1GeometrySink; //给路径添加图形的接口
  ptRight,ptBottom: TD2DPoint2f;
begin
  {算出另外两个点}
  ptRight := D2D1PointF((ptTop.x-ptLeft.x)*2+ptLeft.x, ptLeft.y);
  ptBottom := D2D1PointF(ptTop.x, (ptLeft.y-ptTop.y)*2+ptTop.y);

  {构建 ID2D1PathGeometry}
  D2DFactory.CreatePathGeometry(Result);
  {建立并启用 ID2D1GeometrySink}
  Result.Open(sink);
  {开始添加图形,}
  sink.BeginFigure(ptLeft, D2D1_FIGURE_BEGIN_FILLED); //选项 D2D1_FIGURE_BEGIN_FILLED 标识图形可以填充
  sink.AddLine(ptTop);
  sink.AddLine(ptRight);
  sink.AddLine(ptBottom);
  {结束图形并关闭 ID2D1GeometrySink}
  sink.EndFigure(D2D1_FIGURE_END_CLOSED); //选项 D2D1_FIGURE_END_CLOSED 标识图形自动封闭
  sink.Close;
end;

{绘制}
procedure TForm1.FormPaint(Sender: TObject);
var
  path: ID2D1PathGeometry;
begin
  path := GetDiamondPath(D2D1PointF(ClientWidth/5, ClientHeight/2), D2D1PointF(ClientWidth/2, ClientHeight/5));
  with TDirect2DCanvas.Create(Canvas, ClientRect) do
  begin
    Pen.Color := clRed;
    Brush.Color := clYellow;
    BeginDraw;
    FillGeometry(path);
    DrawGeometry(path);
    EndDraw;
    Free;
  end;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Repaint;
end;


效果图:



posted on 2011-03-31 21:09  万一  阅读(2316)  评论(0编辑  收藏  举报