2011/10/02

[Direct2D] MFC에서 간단한 사용 예

Direct2D 에 대해서는 오래전에 들어 보았지만 오늘에서야 관련 정보를 찾아 보았다.

그리고 간단한게 사용해 봤다.



MSDN 에서 찾은 것으로 간단하게 사각형을 그리는 코드 이다. 이 코드는 Windows 7 x64 에서 Visual Studio 2010 SP1 버전에서 작성되고 테스트 하였다.


Direct2D 기능을 사용하려면 Windows 7 또는 Vista SP2 이상이어야 해서 범용 프로그램 제작에 이용하는 것은 이른것 같지만 알아두면 앞으로 사용할 일이 있을 것 같다. 상세한 것들은 차차 익혀가야 겠다.



////////////////////////////////////////////////////////////////////////////////
// stdafx.h
// Direct2D
#include <d2d1.h>
#pragma comment(lib, "D2D1.lib")

template<class Interface>
inline void SafeRelease(
Interface **ppInterfaceToRelease
)
{
if (*ppInterfaceToRelease != NULL)
{
(*ppInterfaceToRelease)->Release();

(*ppInterfaceToRelease) = NULL;
}
}

#ifndef Assert
#if defined( DEBUG ) || defined( _DEBUG )
#define Assert(b) do {if (!(b)) {OutputDebugStringA("Assert: " #b "\n");}} while(0)
#else
#define Assert(b)
#endif //DEBUG || _DEBUG
#endif

#ifndef HINST_THISCOMPONENT
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
#endif

////////////////////////////////////////////////////////////////////////////////
// ChildView.h
ID2D1Factory* m_pD2DFactory;

////////////////////////////////////////////////////////////////////////////////
// ChildView.cpp
CChildView::CChildView()
{
// Initialize Direct2D
m_pD2DFactory = NULL;
HRESULT hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&m_pD2DFactory
);
}

CChildView::~CChildView()
{
// Release Direct2D
SafeRelease( &m_pD2DFactory );
}

void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting

Render();

// Do not call CWnd::OnPaint() for painting messages
}

int CChildView::Render()
{
if ( m_pD2DFactory == NULL )
{
return -1;
}

// Obtain the size of the drawing area.
CRect rcClient;
GetClientRect(&rcClient);

// Create a Direct2D render target
ID2D1HwndRenderTarget* pRT = NULL;
HRESULT hr = m_pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
GetSafeHwnd(),
D2D1::SizeU( rcClient.Width(), rcClient.Height() )),
&pRT
);

ID2D1SolidColorBrush* pBlackBrush = NULL;
pRT->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Blue),
&pBlackBrush
);

// Begin Drawing
pRT->BeginDraw();

pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));

pRT->DrawRectangle(
D2D1::RectF(
rcClient.left + 100.0f,
rcClient.top + 100.0f,
rcClient.right - 100.0f,
rcClient.bottom - 100.0f),
pBlackBrush
);

// End Drawing
HRESULT hrDraw = pRT->EndDraw();

SafeRelease( &pBlackBrush );
SafeRelease( &pRT );

return NO_ERROR;
}



Direct2D on MSDN

&

Original Post : http://neodreamer-dev.tistory.com/582

No comments :

Post a Comment