Direct2D 第1篇 最简单的D2D程序

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Direct2D 第1篇 最简单的D2D程序相关的知识,希望对你有一定的参考价值。

原文:Direct2D 第1篇 最简单的D2D程序


编译之前,得先安装DirectX SDK


技术图片

#include <windows.h>  
#include <d2d1.h>  
#include <d2d1helper.h>  
#include <dwrite.h>  
#pragma comment(lib, "dwrite.lib")  
#pragma comment(lib, "d2d1.lib")  
  
static HINSTANCE g_hinst;  
static HWND g_hwnd;
static bool g_flag_app_exit;
  
static ID2D1Factory * g_factory;  
static ID2D1HwndRenderTarget * g_render_target;  
  
void OnSize(LPARAM lparam)  
{  
    if(g_render_target)  
        g_render_target->Resize(D2D1::SizeU(LOWORD(lparam),HIWORD(lparam)));  
}  
  
bool AppInit()  
{  
    D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_factory);  
  
    RECT rc;  
    GetClientRect(g_hwnd, &rc);  
   
    g_factory->CreateHwndRenderTarget(  
        D2D1::RenderTargetProperties(),  
        D2D1::HwndRenderTargetProperties(g_hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)    ),  
        &g_render_target);  
  
    return true;  
}  
  
void OnPaint()  
{  
    if(!g_render_target)  
        return;  
  
    g_render_target->BeginDraw();  
    g_render_target->Clear(D2D1::ColorF(0.63, 0.84, 0.00));   
    g_render_target->EndDraw();  
}  
  
void AppExit()  
{  
    g_render_target->Release();  
    g_factory->Release();  
}  
  
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)   
{  
    switch(msg)   
    {    
    case WM_PAINT:  
        OnPaint();  
        break;  
  
    case WM_SIZE:  
        OnSize(lparam);  
        break;  
	
	case WM_DESTROY:   
        g_flag_app_exit = true;
        break;  
  
    default:  
        return DefWindowProc(hwnd, msg, wparam, lparam);  
    }  
    return 0;  
}  
   
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)   
{  
    WNDCLASSEX wc;    
    MSG msg;    
       
    memset(&wc,0,sizeof(wc));  
    wc.cbSize = sizeof(WNDCLASSEX);  
    wc.lpfnWndProc = WndProc;   
    wc.hInstance = hinst;  
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);  
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);  
    wc.lpszClassName = "WindowClass";  
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);    
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);   
  
    if(!RegisterClassEx(&wc))   
    {  
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);  
        return 0;  
    }  
  
    g_hinst = hinst;  
  
    g_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Direct2D Demo",WS_VISIBLE|WS_OVERLAPPEDWINDOW,  
        CW_USEDEFAULT,   
        CW_USEDEFAULT,   
        640,   
        480,   
        NULL, NULL, hinst, NULL);  
  
    if(g_hwnd == NULL)   
    {  
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);  
        return 0;  
    }  
  
    if(!AppInit())   
    {  
        MessageBox(NULL, "Application Initialisation Failed !","Error",MB_ICONEXCLAMATION|MB_OK);  
        return 0;  
    }  
   
    while(!g_flag_app_exit)   
    {
		if(PeekMessage(&msg, g_hwnd, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);    
			DispatchMessage(&msg);   
		}
    }  
  
	AppExit();

    return msg.wParam;  
}  


以上是关于Direct2D 第1篇 最简单的D2D程序的主要内容,如果未能解决你的问题,请参考以下文章

Direct2D 第3篇 绘制文字

Direct2D 第4篇 渐变画刷

Direct2D 第5篇 绘制图像

Direct2D教程简单几何图形

Direct2D教程渲染位图

Direct2D 设备中的 C++ 绘图弧