D2D绘制文本

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

void CMFCApplication2Dlg::OnBnClickedButton3()
{
	ID2D1Factory * g_factory;
	ID2D1HwndRenderTarget * g_render_target;
	ID2D1SolidColorBrush  * g_brush;

	IDWriteFactory * g_write_factory;
	IDWriteTextFormat * g_text_format;
	D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_factory);

	RECT rc;
	::GetClientRect(m_hWnd, &rc);

	g_factory->CreateHwndRenderTarget(
		D2D1::RenderTargetProperties(),
		D2D1::HwndRenderTargetProperties(m_hWnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)),
		&g_render_target);

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

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


	if (!g_render_target)
		return;

	g_render_target->BeginDraw();

	// Clear Background
	g_render_target->Clear(D2D1::ColorF(0x535353));

	// Draw Text 

	const wchar_t * text = L"背景 GIJQR Direct2D Text Sample";
	D2D1_SIZE_F size = g_render_target->GetSize();

	g_render_target->DrawText(text, wcslen(text),
		g_text_format,
		D2D1::RectF(0, 0, size.width, size.height),
		g_brush);

	g_render_target->EndDraw();


}

猜你喜欢

转载自blog.csdn.net/greless/article/details/110389439