#include <windows.h>
#include <d2d1.h>
//#include <d2d1helper.h>
#pragma comment(lib, "d2d1.lib")
LRESULT PASCAL WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
HRESULT CreateRenderTarget(HWND hWnd);
void ReleaseRenderTarget();
void OnPaint(HWND hWnd);
void OnSize(UINT Width, UINT Height);
ID2D1Factory* Direct2DFactory = nullptr;
ID2D1HwndRenderTarget* RenderTarget = nullptr;
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HRESULT hResult;
hResult = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &Direct2DFactory);
if (SUCCEEDED(hResult))
{
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = &WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = hInstance;
wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr;
wcex.hCursor = LoadCursor(nullptr, IDI_APPLICATION);
wcex.lpszClassName = L"Direct2D Template";
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(L"Direct2D Template", L"Direct2D Template", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, nullptr, nullptr, hInstance, nullptr);
BOOL ShowWindowState = ShowWindow(hWnd,nCmdShow);
if (ShowWindowState = TRUE)
{
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
}
else
{
//Window Create Error
return -1;
}
MSG Message;
while (GetMessage(&Message, nullptr, 0, 0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return static_cast<int>(Message.wParam);
}
else
{
//D2D Initialization Error
return -1;
}
}
HRESULT CreateRenderTarget(HWND hWnd)
{
HRESULT hResult = S_OK;
if (RenderTarget == nullptr)
{
RECT Rect;
GetClientRect(hWnd, &Rect);
D2D1_SIZE_U size = D2D1::SizeU(Rect.right - Rect.left, Rect.bottom - Rect.top);
hResult = Direct2DFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd, size), &RenderTarget);
}
return hResult;
}
void ReleaseRenderTarget()
{
if (RenderTarget == nullptr)
{
RenderTarget->Release();
RenderTarget = nullptr;
}
}
LRESULT PASCAL WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
OnPaint(hWnd);
return 0;
case WM_SIZE:
{
UINT Width = LOWORD(lParam);
UINT Height = HIWORD(lParam);
OnSize(Width, Height);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd, message,wParam,lParam));
}
void OnSize(UINT Width, UINT Height)
{
if(RenderTarget != nullptr)
{
RenderTarget->Resize(D2D1::SizeU(Width, Height));
}
}
void OnPaint(HWND hWnd)
{
HRESULT hResult = CreateRenderTarget(hWnd);
if(SUCCEEDED(hResult))
{
RenderTarget->BeginDraw();
D2D1::ColorF BackGroundColor(255,255,255); //RGB
RenderTarget->Clear(BackGroundColor);
D2D1::ColorF BrushColor(D2D1::ColorF::Black);
ID2D1SolidColorBrush* RectBrush;
RenderTarget->CreateSolidColorBrush(BrushColor, &RectBrush);
D2D1_RECT_F RectangleRect = D2D1::RectF(100.0f, 100.0f, 400.0f, 300.0f);
RenderTarget->DrawRectangle(RectangleRect, RectBrush);
hResult = RenderTarget->EndDraw();
}
if (hResult == D2DERR_RECREATE_TARGET)
{
//이 경우 RenderTarget이 무효화 된 것이며, RenderTarget과 RenderTarget에서 생성된 모든 객체를 해제한 후 다시 생성해야 한다.
ReleaseRenderTarget();
}
}