码迷,mamicode.com
首页 > 其他好文 > 详细

Direct2D 第4篇 渐变画刷

时间:2019-05-07 01:43:27      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:draw   get   ips   temp   ade   lap   lang   unknown   die   

原文:Direct2D 第4篇 渐变画刷

技术图片


#include <windows.h>
#include <d2d1.h>
#include <d2d1helper.h>
#include <dwrite.h>
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "d2d1.lib")

HINSTANCE g_hinst;
HWND g_hwnd;

ID2D1Factory * g_factory;
ID2D1HwndRenderTarget * g_render_target;
ID2D1SolidColorBrush  * g_brush;

IDWriteFactory * g_write_factory;
IDWriteTextFormat * g_text_format;

ID2D1GradientStopCollection * g_gradient_stop_collection;
ID2D1LinearGradientBrush  * g_linear_gradient_brush;
ID2D1RadialGradientBrush * g_radial_gradient_brush;

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);

	g_render_target->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::WhiteSmoke), &g_brush);

	// Init Font
	DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,__uuidof(g_write_factory),reinterpret_cast<IUnknown **>(&g_write_factory));
	g_write_factory->CreateTextFormat(L"Arial Block", NULL, DWRITE_FONT_WEIGHT_NORMAL,
		DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 40, L"",&g_text_format);

 
	// Create Gradient Stops
	D2D1_GRADIENT_STOP gradient_stops[3];
	gradient_stops[0].color = D2D1::ColorF(D2D1::ColorF::YellowGreen);
	gradient_stops[0].position = 0.0f;
	gradient_stops[1].color = D2D1::ColorF(D2D1::ColorF::ForestGreen);
	gradient_stops[1].position = 0.5f;
	gradient_stops[2].color = D2D1::ColorF(D2D1::ColorF::Green);
	gradient_stops[2].position = 1.0f;

	// Create Interface
	g_render_target->CreateGradientStopCollection(gradient_stops, 3, &g_gradient_stop_collection);

	// Create Linear Gradient Brush
	g_render_target->CreateLinearGradientBrush(
	D2D1::LinearGradientBrushProperties(D2D1::Point2F(0,0), D2D1::Point2F(600,600)), 
		g_gradient_stop_collection, &g_linear_gradient_brush);
 
	g_render_target->CreateRadialGradientBrush(
		D2D1::RadialGradientBrushProperties(D2D1::Point2F(500,500),D2D1::Point2F(),1000.0f,1000.0f),
		g_gradient_stop_collection,&g_radial_gradient_brush);

	return true;
}

void OnSize(LPARAM lparam)
{
	if(g_render_target)
		g_render_target->Resize(D2D1::SizeU(LOWORD(lparam),HIWORD(lparam)));
}


void OnPaint()
{
	if(!g_render_target)
		return;

	g_render_target->BeginDraw();

	// Clear Background
	g_render_target->Clear(D2D1::ColorF(0.63, 0.84, 0.00)); 

	// Draw Ellipse  
    D2D1_SIZE_F size = g_render_target->GetSize();  
    D2D1_ELLIPSE ellipse;  
    ellipse.point = D2D1::Point2F(size.width/2.0, size.height/2.0);  
    ellipse.radiusX = 280;  
    ellipse.radiusY = 100;  
	g_render_target->FillEllipse(&ellipse, g_radial_gradient_brush); 
	//g_render_target->FillEllipse(&ellipse, g_linear_gradient_brush); 


	// Draw Text 
	const wchar_t * text = L"Direct2D Gradient Brush";
	g_render_target->DrawText(text, wcslen(text),
		g_text_format,
		D2D1::RectF(100, 190, size.width, size.height),
		g_brush);

	g_render_target->EndDraw();
}

void OnDestroy()
{
	g_linear_gradient_brush->Release();
	g_radial_gradient_brush->Release();
	g_gradient_stop_collection->Release();
	g_text_format->Release();
	g_write_factory->Release();
	g_brush->Release();
	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: 
		OnDestroy();
		PostQuitMessage(0);
		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(GetMessage(&msg, NULL, 0, 0) > 0) 
	{  
		TranslateMessage(&msg);  
		DispatchMessage(&msg); 
	}

	return msg.wParam;
}


Direct2D 第4篇 渐变画刷

标签:draw   get   ips   temp   ade   lap   lang   unknown   die   

原文地址:https://www.cnblogs.com/lonelyxmas/p/10823168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!