Direct2D 第3篇 绘制文字

原文:Direct2D 第3篇 绘制文字

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

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::ForestGreen), &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, 44, L"",&g_text_format);

	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 Text 

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

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

	g_render_target->EndDraw();
}

void OnDestroy()
{
	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;
}

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

时间: 2024-08-30 08:30:29

Direct2D 第3篇 绘制文字的相关文章

Direct2D 第2篇 绘制椭圆

原文:Direct2D 第2篇 绘制椭圆 #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; ID2D1Fa

Direct2D 第5篇 绘制图像

原文:Direct2D 第5篇 绘制图像 我加载的图像是一张透明底PNG图像,背景使用渐变的绿色画刷 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> #include <dwrite.h> #pragma comment(lib, "dwrite.lib") #pragma comment(lib, "d2d1.lib") #include

Direct2D 第6篇 绘制多种风格的线条

原文:Direct2D 第6篇 绘制多种风格的线条 上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的"慢",不过Direct2D的绘制线条代码,要比GDI/GDI+要繁锁一些. 1.首先,初始化Direct2D(可以参考?http://blog.csdn.net/ubuntu_ai/article/details/50365536 ) 2.创建线条的风格实例 ?ID2D1StrokeStyle,以下函数Create

C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字

C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用PointSprite绘制文字,这可以只用1个点绘制文字,并确保文字始终面相窗口.用PointSprite绘制的文字,其大小范围有限,本篇提供的Demo中,Max Row Width最大只有256.现在能够绘制少量的文字,为其指定的位置的过程与为一个点指定位置的过程是相同的,所以此方式的应用范围还是比较广

使用XCB编写X Window程序(04):在窗口中绘制文字

在前面的几节中,我展示了使用XCB创建窗口.在窗口中画图以及捕获并处理事件.在这一篇中,我将展示在窗口中绘制文字.绘制文字当然离不开字体,所以我还会简单地探讨一下X Server的核心字体系统.老规矩,先上代码和运行效果图,代码如下: 1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <inttypes.h> 5 #include <xcb/xcb.h

Qt 2D绘图之三:绘制文字、路径、图像、复合模式

一.绘制文字 除了绘制图形以外,还可以使用QPainter::darwText()函数来绘制文字,也可以使用QPainter::setFont()设置文字所使用的字体,使用QPainter::fontInfo()函数可以获取字体的信息,它返回QFontInfo类对象.在绘制文字时会默认使用抗锯齿. 1.1 基本绘制 下面仍然在上一节的程序中进行代码演示,更改paintEvent()的内容如下: void Widget::paintEvent(QPaintEvent *) { QPainter p

用TextPaint来绘制文字

TextPaint是paint的子类,用它可以很方便的进行文字的绘制,一般情况下遇到绘制文字的需求时,我们一般用TextPaint所提供的方法.开始学习如何绘制文字之前,我们必须要先了解下android中文字是怎么绘制到屏幕上的,文字的格式又是怎么样的. 一.FontMetrics 1.1 理论知识 它是一个Paint的内部类,作用是“字体测量”.它里面呢就定义了top,ascent,descent,bottom,leading五个成员变量其他什么也没有,和rect很相似.如果你不信,我们可以去

IOS开发 图形绘制,绘制线条,矩形,和垂直和居中绘制文字

概述 吐槽下IOS下 的图形绘图,代码冗长,不得不自己重新封装方法.整理形成本文. 绘制线 // 绘制直线 + (void)toDrawLineFromX:(CGFloat)x1 Y:(CGFloat)y1 toX:(CGFloat)x2 toY:(CGFloat)y2 context:(CGContextRef)con{ CGContextMoveToPoint(con, x1, y1); CGContextAddLineToPoint(con, x2, y2); CGContextSetLi

CreateJs系列教程之 EaselJs_1_绘制文字(Text)

核心Js代码: var canvas,     stage,     w = window.innerWidth,     h = window.innerHeight; function init() {     //设置canvas属性     canvas = document.getElementById('game');     canvas.width = w;     canvas.height = h;     //创建舞台     stage = new createjs.St