下面是通過自定義函數繪制菱形的測試代碼。
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;
效果圖: