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



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