DirectX技术----D2D基础篇(一)

发表于2017-09-02
评论0 3.9k浏览

这一篇主要谈谈如何创建简单的D2D窗口并绘制一个进行sin移动矩形出来。

首先,我们需要有个比较清楚的思路来进行D2D的开发。

开发Direct2D程序的一般步骤

l  包含 Direct2D 头文件

l  创建 D2D1Factory

l  创建 RenderTarget

l  创建画刷

l  渲染

l  释放资源

以上就是做Direct2D开发的一般步骤,具体的我们可以暂时不管它,到时候可发的时候可以慢慢领会。

不过我们需要记住的是,微软给我们提供了很多库供我们使用开发,那么,我们就需要根据他们的思路来进行开发,很多时候没有那么多为什么的。

好,现在我们就来开始实现出一个简单的D2D窗口并画出一个矩形出来。

步骤过程如下:

第一步:

包含头文件

  1. #include  

第二步:

创建D2D1Factory

  1. ID2D1Factory *pFactory = NULL;  
  2. HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);  

第三步:

创建RenderTarget

  1. ID2D1HwndRenderTarget *pRenderTarget = NULL;  
  2. RECT rtClient = { 0 };//定义一个矩形  
  3. GetClientRect(hwnd, &rtClient);//获得窗口大小  
  4. HRESULT hr = pFactory->CreateHwndRenderTarget(  
  5.             D2D1::RenderTargetProperties(),  
  6.             D2D1::HwndRenderTargetProperties(  
  7.             hwnd,  
  8.             D2D1::SizeU(  
  9.             rtClient.right - rtClient.left,  
  10.             rtClient.bottom - rtClient.top)),  
  11.             &pRenderTarget);  

第四步:

创建画刷

  1. ID2D1SolidColorBrush *pBrush = NULL;  
  2. HRESULT hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red), &pBrush);  

第五步:

 渲染

  1. pRenderTarget->BeginDraw();  
  2. //需要渲染的内容都应该写在BeginDraw()和EndDraw()之间  
  3. pRenderTarget->EndDraw();  


第六步:

释放资源

  1. (释放的资源指针)->Release();  

在具体的就不多讲,以上就是实现的整个步骤。下面直接贴出本实例的代码(创建简单的D2D窗口并绘制一个进行sin移动矩形出来)

  1. /*************************************************************** 
  2. 功能:简单的D2D窗口创建、矩形的sin曲线移动 
  3. 作者:*** 
  4. 版本:1.0 
  5. 日期:2016.4 
  6. ****************************************************************/  
  7. #include   
  8. #include  
  9. #include  
  10.   
  11. //函数声明  
  12. void CreateResource();  
  13.   
  14. //变量声明  
  15. ID2D1Factory *pFactory = NULL;  
  16. ID2D1HwndRenderTarget *pRenderTarget = NULL;  
  17. ID2D1SolidColorBrush *pBrush = NULL;  
  18.   
  19. HWND hwnd = 0;  
  20. RECT rt = { 0 }, rtClient = { 0 };  
  21. int num = 1;  
  22.   
  23. void init()  
  24. {  
  25.     if (!hwnd)  
  26.     {  
  27.         return;  
  28.     }  
  29.     GetClientRect(hwnd, &rtClient);  
  30.     CreateResource();  
  31. }  
  32.   
  33. void CreateResource()  
  34. {  
  35.     HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);  
  36.     if (SUCCEEDED(hr))  
  37.     {  
  38.         hr = pFactory->CreateHwndRenderTarget(  
  39.             D2D1::RenderTargetProperties(),  
  40.             D2D1::HwndRenderTargetProperties(  
  41.             hwnd,  
  42.             D2D1::SizeU(  
  43.             rtClient.right - rtClient.left,  
  44.             rtClient.bottom - rtClient.top)),  
  45.             &pRenderTarget);  
  46.         if (SUCCEEDED(hr))  
  47.         {  
  48.             hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red), &pBrush);  
  49.             if (FAILED(hr))  
  50.             {  
  51.                 MessageBox(hwnd, TEXT("Create Resource Fail!"), TEXT("Error"), MB_OK);  
  52.                 return;  
  53.             }  
  54.         }  
  55.     }  
  56. }  
  57. void DrawRectangle()  
  58. {  
  59.     if (pRenderTarget){  
  60.         pRenderTarget->BeginDraw();  
  61.         pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::Black));  
  62.         pRenderTarget->DrawRectangle(D2D1::RectF(rt.left   0.0f, rt.top   200.0f, rt.right   50.0f, rt.bottom   250.0f), pBrush);  
  63.         pRenderTarget->EndDraw();  
  64.     }  
  65. }  
  66. void Render()  
  67. {  
  68.     DrawRectangle();  
  69.     num  = 5;  
  70.     rt.right  = 2;  
  71.     rt.left  = 2;  
  72.     rt.top = (100 * sin(num * 3.14 / 180));  
  73.     rt.bottom = (100 * sin(num * 3.14 / 180));  
  74.     if (rt.right == 1000)  
  75.     {  
  76.         rt = { 0 };  
  77.         num = 0;  
  78.     }  
  79. }  
  80.   
  81. template<class Interface>  
  82. inline void SafeRelease(Interface **ppinterfaceToRelease)  
  83. {  
  84.     if (NULL != *ppinterfaceToRelease)  
  85.     {  
  86.         (*ppinterfaceToRelease)->Release();  
  87.         (*ppinterfaceToRelease) = NULL;  
  88.     }  
  89. }  
  90. void CleanUp()  
  91. {  
  92.     SafeRelease(&pBrush);  
  93.     SafeRelease(&pRenderTarget);  
  94.     SafeRelease(&pFactory);  
  95. }  
  96.   
  97. LRESULT CALLBACK WndProc(HWND Hwnd, UINT message, WPARAM wParam, LPARAM iParam);  
  98. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)  
  99. {  
  100.     MSG Msg;  
  101.     WNDCLASS wndclass;  
  102.     TCHAR lpszTitle[] = TEXT("MyDemo001");  
  103.   
  104.     wndclass.style = CS_HREDRAW|CS_VREDRAW;  
  105.     wndclass.lpfnWndProc = WndProc;  
  106.     wndclass.cbClsExtra = 0;  
  107.     wndclass.cbWndExtra = 0;  
  108.     wndclass.hInstance = hInstance;  
  109.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
  110.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);  
  111.     wndclass.lpszMenuName = NULL;  
  112.     wndclass.lpszClassName = TEXT("MyClass");  
  113.     wndclass.hbrBackground = 0;  
  114.     if (!RegisterClass(&wndclass))  
  115.     {  
  116.         MessageBox(NULL, TEXT("RegisterClass fail!"), TEXT("error"), MB_ICONERROR);  
  117.         return 0;  
  118.     }  
  119.   
  120.     hwnd = CreateWindow(  
  121.         TEXT("MyClass"),  
  122.         lpszTitle,  
  123.         WS_OVERLAPPEDWINDOW,  
  124.         CW_USEDEFAULT,  
  125.         CW_USEDEFAULT,  
  126.         CW_USEDEFAULT,  
  127.         CW_USEDEFAULT,  
  128.         NULL,  
  129.         NULL,  
  130.         hInstance,  
  131.         NULL);  
  132.     ShowWindow(hwnd, nCmdShow);  
  133.     UpdateWindow(hwnd);  
  134.     init();  
  135.   
  136.     BOOL bRet;  
  137.     PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE);  
  138.     while (Msg.message != WM_QUIT)  
  139.     {  
  140.         bRet = PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);  
  141.         if (bRet)  
  142.         {  
  143.             TranslateMessage(&Msg);  
  144.             DispatchMessage(&Msg);  
  145.         }  
  146.         else  
  147.         {  
  148.             Render();  
  149.         }  
  150.     }  
  151.     CleanUp();  
  152.     return Msg.wParam;  
  153. }  
  154.   
  155. LRESULT CALLBACK WndProc(HWND Hwnd, UINT message, WPARAM wParam, LPARAM iParam)  
  156. {  
  157.     switch (message)  
  158.     {  
  159.     case WM_DESTROY:  
  160.         PostQuitMessage(0);  
  161.         return 0;  
  162.     }  
  163.     return DefWindowProc(Hwnd, message, wParam, iParam);  
  164. }  

需要注意的是,SUCCEEDED或FAILD宏是用来检测创建的对象是否成功的。

最后的效果如下:

http://blog.csdn.net/qq_30501909/article/details/51461592

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引