Direct2D 第6篇 绘制多种风格的线条


上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的“慢”,不过Direct2D的绘制线条代码,要比GDI/GDI+要繁锁一些。


1.首先,初始化Direct2D(可以参考 http://blog.csdn.net/ubuntu_ai/article/details/50365536 )


2.创建线条的风格实例  ID2D1StrokeStyle,以下函数CreateStrokeStyle是创建ID2D1StrokeStyle的接口,它的声明大致如下:

virtual HRESULT CreateStrokeStyle(
  const D2D1_STROKE_STYLE_PROPERTIES *strokeStyleProperties,
  const FLOAT *dashes,
  UINT dashesCount,
  ID2D1StrokeStyle **strokeStyle
);

你可以这样创建一个ID2D1StrokeStyle实例

ID2D1StrokeStyle * g_stroke_style; // 声明线条风格接口
g_pD2DFactory->CreateStrokeStyle(D2D1::StrokeStyleProperties(
				D2D1_CAP_STYLE_ROUND, 
				D2D1_CAP_STYLE_ROUND,
				D2D1_CAP_STYLE_ROUND,
				D2D1_LINE_JOIN_MITER,
				1.0f,
				D2D1_DASH_STYLE_SOLID, // 有多种风格可以设置(dash,dot....)
				10.0f),
			NULL,
			0,
			&g_stroke_style);





3. 绘制线条


在你的绘制函数中写下以下代码,此处brush我没有创建,不懂如何创建的可以参考 <a target=_blank href="http://blog.csdn.net/ubuntu_ai/article/details/50365818">点击打开链接</a>  http://blog.csdn.net/ubuntu_ai/article/details/50365818
D2D1_POINT_2F p1={20.0f, 20.0f};
D2D1_POINT_2F p2={600.0f, 20.0f};
g_pD2DHwndRenderTarget->DrawLine(p1, p2, brush, 3.0f, g_stroke_style);



4.释放你的资源


前面你创建了g_stroke_style,在Direct2D中,几乎所有创建的资源都需要释放,否则后果会导致内存泄漏(后果会怎样我也不知道)


这样释放你的资源


#define SafeRelease(p)    if(p){ p->Release(); p = NULL; }


SafeRelease(g_stroke_style)



 作者已经用VS编译一个实例,存放在我的【资源】当中,有必须的可以前往下载

点击打开链接     http://download.csdn.net/detail/ubuntu_ai/9386783



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章