# 概述

Direct2D支持以下几种类型的几何图形。
Simple Geometry（简单几何图形）

•     矩形
•     圆角矩形
•     椭圆

Path Geometry（路径图形）
Composite Geometry（复合图形）

•     Geometry Group（图形组）
•     Transformed Geometry（变换的图形）

# 图形组

virtual HRESULT CreateGeometryGroup(
D2D1_FILL_MODE fillMode,
[
in] ID2D1Geometry **geometries,
UINT geometriesCount,
[
out] ID2D1GeometryGroup **geometryGroup
)
0;

## 创建图形组中的所有图形

const D2D1_ELLIPSE ellipse1 = D2D1::Ellipse(
D2D1::Point2F(
105.0f, 105.0f),
25.0f,
25.0f
);

hr
= pD2DFactory->CreateEllipseGeometry(
ellipse1,
&pEllipseGeometry1
);

## 创建图形组

ID2D1Geometry *ppGeometries[] =
{
pEllipseGeometry1,
pEllipseGeometry2,
pEllipseGeometry3,
pEllipseGeometry4
};

hr = pD2DFactory->CreateGeometryGroup(
D2D1_FILL_MODE_ALTERNATE,
ppGeometries,
ARRAYSIZE(ppGeometries),
&pGeoGroup_AlternateFill
);

## 使用图形组进行绘制

pRenderTarget->FillGeometry(pGeoGroup_AlternateFill, pFillBrush);
pRenderTarget
->DrawGeometry(pGeoGroup_AlternateFill, pStrokeBrush, 1.0f);

D2D1_FILL_MODE_ALTERNATE和D2D1_FILL_MODE_WINDING这两个模式的区别见下图，两者线条的颜色都是一样的，只是填充色一个是交替模式，一个是统一模式。

# 变换图形

virtual HRESULT CreateTransformedGeometry(
[
in] ID2D1Geometry *sourceGeometry,
[
in, optional] const D2D1_MATRIX_3X2_F *transform,
[
out] ID2D1TransformedGeometry **transformedGeometry
)
0;

hr = pD2DFactory->CreateTransformedGeometry(
pPathGeometry,
D2D1::Matrix3x2F::Translation(
100, 100),
&pTransformedGeometry
);

## 变换render target

// Transform the render target, then draw the rectangle geometry again.
m_pRenderTarget->SetTransform(
D2D1::Matrix3x2F::Scale(
D2D1::SizeF(
3.f, 3.f),
D2D1::Point2F(
175.f, 175.f))
);

m_pRenderTarget
->DrawGeometry(m_pRectangleGeometry, m_pBlackBrush, 1);

## 变换图形本身

hr = m_pD2DFactory->CreateTransformedGeometry(
m_pRectangleGeometry,
D2D1::Matrix3x2F::Scale(
D2D1::SizeF(
3.f, 3.f),
D2D1::Point2F(
175.f, 175.f)),
&m_pTransformedGeometry
);

// Replace the previous render target transform.
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());

// Draw the transformed geometry.
m_pRenderTarget->DrawGeometry(m_pTransformedGeometry, m_pBlackBrush, 1);

Happy Coding!!!

== THE END ==

posted on 2011-05-31 10:05  翰墨小生  阅读(8036)  评论(7编辑  收藏  举报