본문 바로가기
::public/윈도우즈 응용 프로그래밍

Direct2D 사용하기

by 해맑은욱 2019. 9. 25.
// 
 
#include "stdafx.h"
#include "MyWindowsProject.h"
 
// Direct2D를 사용하기 위한 파일 포함.
#include <d2d1.h>
#pragma comment(lib, "D2D1.lib")
using namespace D2D1;
 
// Direct2D를 구성하는 각종 객체를 생성하는 객체
ID2D1Factory* gp_factory;
// Direct2D에서 윈도우의 클라이언트 영역에 그림을 그릴 객체
ID2D1HwndRenderTarget* gp_render_target;
 
void OnCreateRenderTarget(HWND hWnd)
{
    RECT r;
    GetClientRect(hWnd, &r);
    
    // 지정한 윈도우의 클라이언트 영역에 그림을 그리기 위한 render target 생성.
    gp_factory->CreateHwndRenderTarget(
                    RenderTargetProperties(), 
                    HwndRenderTargetProperties(hWnd, SizeU(r.right, r.bottom)), 
                    &gp_render_target);
}
 
void OnPaintRenderTarget()
{
    // Direct2D의 Render Target을 사용해서 그림 그리기 시작.
    gp_render_target->BeginDraw();
    // Clear 함수를 사용하여 하늘색으로 채움.
    gp_render_target->Clear(ColorF(0.0f, 0.8f, 1.0f));
 
    D2D1_ELLIPSE my_region;
    my_region.point.x = 100;
    my_region.point.y = 100;
    my_region.radiusX = 50.0f;
    my_region.radiusY = 50.0f;
 
    ID2D1SolidColorBrush* p_yellow_brush = NULL;
    // 노란색 brush 객체 생성.
    gp_render_target->CreateSolidColorBrush(ColorF(1.0f, 1.0f, 0.0f), &p_yellow_brush);
    // 지정한 영역에 brush로 타원을 그림.
    gp_render_target->FillEllipse(my_region, p_yellow_brush);
    // brush 객체 제거.
    p_yellow_brush->Release();
    p_yellow_brush = NULL;
 
    gp_render_target->EndDraw();
}
 
void OnDestoryRenderTarget()
{
    if (gp_render_target != NULL)
    {
        gp_render_target->Release();
        gp_render_target = NULL;
    }
}
 
// 사용자가 메시지를 처리하는 함수.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//     HDC h_screen_dc = ::GetDC(NULL);    // 모니터 전체 화면용 DC
     HDC h_dc = ::GetDC(hWnd);    // 현재 윈도우용 DC
    switch (message)
    {
    case WM_CREATE:
        OnCreateRenderTarget(hWnd);
        break;
    case WM_PAINT:
        OnPaintRenderTarget();
        break;
    case WM_LBUTTONDOWN:
        OnLButtonDown(hWnd, LOWORD(lParam), HIWORD(lParam));
        break;
    case WM_DESTROY:
        OnDestoryRenderTarget();
        // 프로그램 종료
        PostQuitMessage(0);
        break;
    default:
        // 자신이 처리하지 않는 메시지들의 기본 작업을 대신 처리해주는 함수.
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
 
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,            // 프로그램의 instance 핸들 값.
                     _In_opt_ HINSTANCE hPrevInstance,    // 현재 사용안함. 항상 NULL.
                     _In_ LPWSTR    lpCmdLine,            // 하나의 문자열로 실행인자가 전달됨.
                     _In_ int       nCmdShow)            // 초기 기작 형식이 전달됨.
{
    // 컴포넌트를 사용할 수 있도록 프로그램을 초기화.
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    // Direct2D를 위한 factory 객체를 생성.
    if(S_OK != D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &gp_factory))
        return 0;
 
    // 윈도우 클래스 등록
    WNDCLASSEXW wcex;
 
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(24417677));    // 바둑판 컬러 설정
    wcex.lpszMenuName = IDI_APPLICATION;
    wcex.lpszClassName = L"MyWindow";
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
    RegisterClassExW(&wcex);
 
    // 윈도우 생성
    HWND hWnd = CreateWindowW(L"MyWindow", L"scriptplay.tistory.com", WS_OVERLAPPEDWINDOW,
        5050600600, nullptr, nullptr, hInstance, nullptr);
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
 
    // 프로그램에 전달된 메시지를 번역하고 실행하는 작업.
    MSG msg;
    while (GetMessage(&msg, nullptr, 00))    // 메시지를 큐에서 읽는 함수.
    {
        TranslateMessage(&msg);                // 가상 키 메시지이면 ASCII 형태의 메시지를 추가로 생성.
        DispatchMessage(&msg);                // 변환된 메시지를 처리하는 함수.
    }
 
    // 사용하던 Factory 객체를 제거.
    gp_factory->Release();
    // 컴포넌트 사용을 해제.
    CoUninitialize();
 
    return (int) msg.wParam;
}
 
cs

'::public > 윈도우즈 응용 프로그래밍' 카테고리의 다른 글

Direct2D - 그림파일 읽고 출력  (0) 2019.09.25
Direct2D - 오목 만들기  (0) 2019.09.25
Direct2D  (0) 2019.09.25
Timer  (0) 2019.09.24
오목 만들기(결과로직 없음)  (0) 2019.09.24