http://www.cnblogs.com/graphics/archive/2011/05/23/1964273.html
http://msdn.microsoft.com/zh-cn/library/dd371902(v=VS.85).aspx
http://technet.microsoft.com/zh-cn/library/dd756653(v=vs.85).aspx
http://msdn.microsoft.com/zh-cn/library/windows/desktop/ff684172(v=vs.85).aspx
http://msdn.microsoft.com/zh-cn/library/windows/desktop/ee329947
http://msdn.microsoft.com/zh-cn/library/dd742732(v=vs.85).aspx
操作系统 : Windows 8 Pro
开发环境 :Microsoft Visual Studio Ultimate 2012
记得要右键工程->属性->配置属性->链接器->命令行
“其它选项” 添加 d2d1.lib dwrite.lib
D2D画矩形
VOID DrawRectangle(HWND hwnd)
{
HRESULT hr;
RECT rc={0,0,500,500};//Render area
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory) ;
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Create D2D factory failed!",L"Error",0);
}
hr = pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd,
D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top)),
&pRenderTarget) ;
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Create render target failed!",L"Error",0);
}
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red),&pBlackBrush);
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Create brush failed!",L"Error",0);
}
D2D1_GRADIENT_STOP gradientStop[2];
gradientStop[0].color=D2D1::ColorF(D2D1::ColorF::Yellow);
gradientStop[0].position=0.f;
gradientStop[1].color=D2D1::ColorF(D2D1::ColorF::Red);
gradientStop[1].position=1.f;
ID2D1GradientStopCollection* pGradientStopCollection;
pRenderTarget->CreateGradientStopCollection(
gradientStop,
2,
D2D1_GAMMA_2_2,D2D1_EXTEND_MODE_CLAMP,
&pGradientStopCollection);
D2D1_ELLIPSE g_ellipse=D2D1::Ellipse(D2D1::Point2F(250,250),250.f,250.f);
pRenderTarget->CreateRadialGradientBrush(
D2D1::RadialGradientBrushProperties(
g_ellipse.point,
D2D1::Point2F(0,0),
g_ellipse.radiusX,
g_ellipse.radiusY),
pGradientStopCollection,
&pRadialGradientBrush);
pRenderTarget->BeginDraw() ;
pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White,0));
D2D1_RECT_F rect2={125,125,375,375};
D2D1_ROUNDED_RECT rounded_rect={rect2,30.f,30.f};
pRenderTarget->DrawRoundedRectangle(rounded_rect,pBlackBrush);
pRenderTarget->FillRoundedRectangle(rounded_rect,pRadialGradientBrush);
//pRenderTarget->DrawRectangle(
// D2D1::RectF(0,0,500.f,500.f),
// pBlackBrush);
hr = pRenderTarget->EndDraw() ;
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Draw failed!",L"Error",0);
}
SAFE_RELEASE(pBlackBrush) ;
SAFE_RELEASE(pRadialGradientBrush);
}
d2d写文本
VOID DrawText2(HWND hwnd)
{
HRESULT hr;
IDWriteFactory* pDWriteFactory=NULL;
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory));
IDWriteTextFormat* pDWriteTextFormat=NULL;
hr=pDWriteFactory->CreateTextFormat(
L"Microsoft Yahei",
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
50.f,
L"zh-cn",
&pDWriteTextFormat);
RECT clientRect;
GetClientRect(hwnd,&clientRect);
D2D1_RECT_F textLayoutRect=D2D1::RectF(
static_cast<FLOAT>(clientRect.left),
static_cast<FLOAT>(clientRect.top),
static_cast<FLOAT>(clientRect.right-clientRect.left),
static_cast<FLOAT>(clientRect.bottom-clientRect.top));
WCHAR* text=L"Hello,Direct2D";
ID2D1SolidColorBrush* pSolidColorBrush;
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black,1.f),&pSolidColorBrush);
pRenderTarget->BeginDraw();
pRenderTarget->DrawTextW(text,
wcslen(text),
pDWriteTextFormat,
textLayoutRect,
pSolidColorBrush,
D2D1_DRAW_TEXT_OPTIONS_NONE,
DWRITE_MEASURING_MODE_NATURAL
);
pRenderTarget->EndDraw();
pSolidColorBrush->Release();
pDWriteTextFormat->Release();
pDWriteFactory->Release();
}
所有代码:
#include <tchar.h>
#include <Windows.h>
#include <d2d1.h>
#include <dwrite.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
#define MAX_LOADSTRING 100
#define SAFE_RELEASE(P) if(P){P->Release() ; P = NULL ;}
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
VOID DrawRectangle(HWND hwnd);
VOID DrawText2(HWND hwnd);
VOID Cleanup();
ID2D1Factory* pD2DFactory = NULL;//Direct2D factory d2d工厂接口,这个接口是所有d2d程序的起始点,几乎所有的d2d资源都是由这个接口创建的
ID2D1HwndRenderTarget* pRenderTarget=NULL;//Render target 用来在窗口中进行渲染
ID2D1SolidColorBrush* pBlackBrush=NULL;//定义画刷,用来绘制图形,ID2D1SolidColorBrush是固定颜色的画刷
ID2D1RadialGradientBrush* pRadialGradientBrush=NULL;
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine,_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
// 初始化全局字符串
_stprintf_s(szTitle, MAX_LOADSTRING, _T("D2D1_Text"));
_stprintf_s(szWindowClass, MAX_LOADSTRING, _T("D2D1_Text_Wnd"));
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
VOID DrawRectangle(HWND hwnd)
{
HRESULT hr;
RECT rc={0,0,500,500};//Render area
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory) ;
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Create D2D factory failed!",L"Error",0);
}
hr = pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd,
D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top)),
&pRenderTarget) ;
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Create render target failed!",L"Error",0);
}
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Red),&pBlackBrush);
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Create brush failed!",L"Error",0);
}
D2D1_GRADIENT_STOP gradientStop[2];
gradientStop[0].color=D2D1::ColorF(D2D1::ColorF::Yellow);
gradientStop[0].position=0.f;
gradientStop[1].color=D2D1::ColorF(D2D1::ColorF::Red);
gradientStop[1].position=1.f;
ID2D1GradientStopCollection* pGradientStopCollection;
pRenderTarget->CreateGradientStopCollection(
gradientStop,
2,
D2D1_GAMMA_2_2,D2D1_EXTEND_MODE_CLAMP,
&pGradientStopCollection);
D2D1_ELLIPSE g_ellipse=D2D1::Ellipse(D2D1::Point2F(250,250),250.f,250.f);
pRenderTarget->CreateRadialGradientBrush(
D2D1::RadialGradientBrushProperties(
g_ellipse.point,
D2D1::Point2F(0,0),
g_ellipse.radiusX,
g_ellipse.radiusY),
pGradientStopCollection,
&pRadialGradientBrush);
pRenderTarget->BeginDraw() ;
pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White,0));
D2D1_RECT_F rect2={125,125,375,375};
D2D1_ROUNDED_RECT rounded_rect={rect2,30.f,30.f};
pRenderTarget->DrawRoundedRectangle(rounded_rect,pBlackBrush);
pRenderTarget->FillRoundedRectangle(rounded_rect,pRadialGradientBrush);
//pRenderTarget->DrawRectangle(
// D2D1::RectF(0,0,500.f,500.f),
// pBlackBrush);
hr = pRenderTarget->EndDraw() ;
if(!SUCCEEDED(hr))
{
MessageBox(hwnd,L"Draw failed!",L"Error",0);
}
SAFE_RELEASE(pBlackBrush) ;
SAFE_RELEASE(pRadialGradientBrush);
}
VOID DrawText2(HWND hwnd)
{
HRESULT hr;
IDWriteFactory* pDWriteFactory=NULL;
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory));
IDWriteTextFormat* pDWriteTextFormat=NULL;
hr=pDWriteFactory->CreateTextFormat(
L"Microsoft Yahei",
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
50.f,
L"zh-cn",
&pDWriteTextFormat);
RECT clientRect;
GetClientRect(hwnd,&clientRect);
D2D1_RECT_F textLayoutRect=D2D1::RectF(
static_cast<FLOAT>(clientRect.left),
static_cast<FLOAT>(clientRect.top),
static_cast<FLOAT>(clientRect.right-clientRect.left),
static_cast<FLOAT>(clientRect.bottom-clientRect.top));
WCHAR* text=L"Hello,Direct2D";
ID2D1SolidColorBrush* pSolidColorBrush;
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black,1.f),&pSolidColorBrush);
pRenderTarget->BeginDraw();
pRenderTarget->DrawTextW(text,
wcslen(text),
pDWriteTextFormat,
textLayoutRect,
pSolidColorBrush,
D2D1_DRAW_TEXT_OPTIONS_NONE,
DWRITE_MEASURING_MODE_NATURAL
);
pRenderTarget->EndDraw();
pSolidColorBrush->Release();
pDWriteTextFormat->Release();
pDWriteFactory->Release();
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX 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(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
//创建窗口
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX,CW_USEDEFAULT, CW_USEDEFAULT,500 , 500, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
DrawRectangle(hWnd); //画矩形
DrawText2(hWnd);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
示例代码: