目前博客园中成系列的Direct2D的教程有
1、万一的 Direct2D 系列,用的是Delphi 2009
2、zdd的 Direct2D 系列,用的是VS中的C++
3、本文所在的 Direct2D教程 系列,用的是VS2010的Visual Basic语言(可以很方便的转为C#),基于Windows API Code Pack 1.1。
还有官方的说明文档 Direct2D ,用的是C++。
本系列的前几篇文章:
Direct2D教程I——简介及首个例子
Direct2D教程II——绘制基本图形和线型(StrokeStyle)的设置详解
Direct2D教程III——几何(Geometry)对象
Direct2D教程IV——笔刷(Brush)对象
Direct2D教程V——位图(Bitmap)和位图笔刷(BitmapBrush)
Direct2D教程VI——转换(Transform)
变换几何(TransformedGeometry)的运用
在一些精度要求不高的场景下,为了提高效率,重视元件重用(即一个元件,多次使用)。变换几何(TransformedGeometry)就应运而生,它是把一个源元件通过转换(Transform)得到一个新元件,而不需要重新用大量的代码增加新元件(可以省去很多的关于点的计算)。
和其他的几何对象一样,变换几何(TransformedGeometry)也得通过D2DFactory对象的CreateTransformedGeometry方法来创建,来看看该方法的原型定义
Public
Function CreateTransformedGeometry(sourceGeometry
As Direct2D1.
Geometry, transform
As Direct2D1.
Matrix3x2F)
As Direct2D1.
TransformedGeometry
从该函数的原型定义来看,只有两个参数:sourceGeometry,源几何对象,类型是Geometry类;transform,对源几何对象进行的转换,类型是结构Matrix3x2F。
通过该方法获得一个新的几何对象
接下来,我们用一个例子来说明变换几何(TransformedGeometry)的用法
上图是一个简单的图形,分为左山、右山、小河、太阳、太阳光芒这几部分。
我们先用路径几何(PathGeometry)对象创建右山,然后利用CreateTransformedGeometry方法创建了左山,并对它进行了缩放转换(SacleTransform)。
太阳光芒的部分,先用路径几何(PathGeometry)对象创建基本的光芒,然后利用CreateTransformedGeometry方法创建光芒数组,并依次对数组中的光芒进行了旋转转换(RotateTransform)。
下面是示例代码
Public
Class
clsDirect2DSample16
Inherits
clsDirect2DSample
Public
Shadows
Sub Render()
If
Not _renderTarget
Is
Nothing
Then
Dim ColorBackground
As
New Direct2D1.
ColorF(
Color.Chocolate.ToArgb)
Dim ColorBorder
As
New Direct2D1.
ColorF(0, 0, 0)
Dim ColorLeftMountain
As
New Direct2D1.
ColorF(
Color.OliveDrab.ToArgb)
Dim ColorRightMountain
As
New Direct2D1.
ColorF(
Color.YellowGreen.ToArgb)
Dim ColorRiver
As
New Direct2D1.
ColorF(
Color.LightSkyBlue.ToArgb)
'Create Sun Brush
Dim gradientStops(2)
As Direct2D1.
GradientStop
gradientStops(0).Color =
New Direct2D1.
ColorF(
Color.Gold.ToArgb)
gradientStops(0).Position = 0
Dim TC
As
New Direct2D1.
ColorF(
Color.Orange.ToArgb)
TC.Alpha = 0.8
gradientStops(1).Color = TC
gradientStops(1).Position = 0.85
TC =
New Direct2D1.
ColorF(
Color.OrangeRed.ToArgb)
TC.Alpha = 0.7
gradientStops(2).Color = TC
gradientStops(2).Position = 1
Dim GS
As Direct2D1.
GradientStopCollection = _renderTarget.CreateGradientStopCollection(gradientStops, Direct2D1.
Gamma.Linear, Direct2D1.
ExtendMode.Clamp)
Dim RBS
As Direct2D1.
RadialGradientBrushProperties
RBS.Center =
New Direct2D1.
Point2F(185, 250)
RBS.GradientOriginOffset =
New Direct2D1.
Point2F(80, 80)
RBS.RadiusX = 85
RBS.RadiusY = 85
Dim RB
As Direct2D1.
RadialGradientBrush = _renderTarget.CreateRadialGradientBrush(RBS, GS)
Dim BorderBrush
As Direct2D1.
SolidColorBrush = _renderTarget.CreateSolidColorBrush(ColorBorder)
Dim FillBrush
As Direct2D1.
SolidColorBrush = _renderTarget.CreateSolidColorBrush(ColorBorder)
'Create Right Mountain
Dim rightMountainGeometry
As Direct2D1.
PathGeometry = _d2DFactory.CreatePathGeometry()
Dim sink
As Direct2D1.
GeometrySink = rightMountainGeometry.Open
sink.SetFillMode(Direct2D1.
FillMode.Winding)
sink.BeginFigure(
New Direct2D1.
Point2F(395, 263), Direct2D1.
FigureBegin.Filled)
Dim points()
As Direct2D1.
Point2F = { _
New Direct2D1.
Point2F(301, 146), _
New Direct2D1.
Point2F(269, 181), _
New Direct2D1.
Point2F(253, 159), _
New Direct2D1.
Point2F(221, 214), _
New Direct2D1.
Point2F(201, 199), _
New Direct2D1.
Point2F(143, 263), _
New Direct2D1.
Point2F(395, 263)
}
sink.AddLines(points)
sink.EndFigure(Direct2D1.
FigureEnd.Closed)
sink.Close()
'Create Left Mountain
Dim leftMountainGeometry
As Direct2D1.
TransformedGeometry = _d2DFactory.CreateTransformedGeometry(rightMountainGeometry, Direct2D1.
Matrix3x2F.Scale(-0.5, 0.6,
New Direct2D1.
Point2F(160, 250)))
'Create Sun
Dim sunGeometry
As Direct2D1.
PathGeometry = _d2DFactory.CreatePathGeometry
sink = sunGeometry.Open
sink.SetFillMode(Direct2D1.
FillMode.Winding)
sink.BeginFigure(
New Direct2D1.
Point2F(100, 250), Direct2D1.
FigureBegin.Filled)
sink.AddArc(
New Direct2D1.
ArcSegment(
New Direct2D1.
Point2F(270, 250), _
New Direct2D1.
SizeF(85, 85), _
0, Direct2D1.
SweepDirection.Clockwise, Direct2D1.
ArcSize.Small))
sink.EndFigure(Direct2D1.
FigureEnd.Closed)
sink.Close()
'Create Base Sun Light
Dim sunLightGeometry
As Direct2D1.
PathGeometry = _d2DFactory.CreatePathGeometry
sink = sunLightGeometry.Open
sink.BeginFigure(
New Direct2D1.
Point2F(185, 140), Direct2D1.
FigureBegin.Hollow)
sink.AddBezier(
New Direct2D1.
BezierSegment(
New Direct2D1.
Point2F(170, 125), _
New Direct2D1.
Point2F(200, 125), _
New Direct2D1.
Point2F(185, 110)))
sink.EndFigure(Direct2D1.
FigureEnd.Open)
sink.Close()
'Create some Sun Lights
Dim sunLights(4)
As Direct2D1.
TransformedGeometry
Dim I
As
Integer
For I = 0
To 4
sunLights(I) = _d2DFactory.CreateTransformedGeometry(sunLightGeometry, Direct2D1.
Matrix3x2F.Rotation(I * 20 - 40,
New Direct2D1.
Point2F(185, 250)))
Next
'Create River
Dim riverGeometry
As Direct2D1.
PathGeometry = _d2DFactory.CreatePathGeometry
sink = riverGeometry.Open
sink.SetFillMode(Direct2D1.
FillMode.Winding)
sink.BeginFigure(
New Direct2D1.
Point2F(23, 392), Direct2D1.
FigureBegin.Filled)
sink.AddBezier(
New Direct2D1.
BezierSegment(
New Direct2D1.
Point2F(58, 284), _
New Direct2D1.
Point2F(312, 345), _
New Direct2D1.
Point2F(196, 303)))
sink.AddBezier(
New Direct2D1.
BezierSegment(
New Direct2D1.
Point2F(77, 261), _
New Direct2D1.
Point2F(163, 256), _
New Direct2D1.
Point2F(163, 256)))
sink.AddBezier(
New Direct2D1.
BezierSegment(
New Direct2D1.
Point2F(165, 257), _
New Direct2D1.
Point2F(81, 261), _
New Direct2D1.
Point2F(251, 306)))
sink.AddBezier(
New Direct2D1.
BezierSegment(
New Direct2D1.
Point2F(414, 350), _
New Direct2D1.
Point2F(128, 324), _
New Direct2D1.
Point2F(136, 392)))
sink.EndFigure(Direct2D1.
FigureEnd.Open)
sink.Close()
With _renderTarget
.BeginDraw()
.Clear(ColorBackground)
.DrawGeometry(sunGeometry, BorderBrush, 2)
.FillGeometry(sunGeometry, RB)
For I = 0
To 4
.DrawGeometry(sunLights(I), BorderBrush, 1)
Next
.DrawGeometry(riverGeometry, BorderBrush, 2)
FillBrush.Color = ColorRiver
.FillGeometry(riverGeometry, FillBrush)
.DrawGeometry(rightMountainGeometry, BorderBrush, 2)
FillBrush.Color = ColorRightMountain
.FillGeometry(rightMountainGeometry, FillBrush)
.DrawGeometry(leftMountainGeometry, BorderBrush, 2)
FillBrush.Color = ColorLeftMountain
.FillGeometry(leftMountainGeometry, FillBrush)
.EndDraw()
End
With
End
If
End
Sub
End
Class
通过上面的示例代码说明了变换几何(TransformedGeometry)对象的用法。它实际上是一种元件复用的思路,利用已知的元件,通过转换(Transform)得到一个新的元件,简化绘图的难度。当然,变换几何(TransformedGeometry)对象要想用的好,得和转换(Transform)配合起来才行。