原文: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;
}

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

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

    原文:Direct2D 第6篇 绘制多种风格的线条 上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的"慢", ...

  2. Direct2D 第5篇 绘制图像

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

  3. Direct2D 第2篇 绘制椭圆

    原文:Direct2D 第2篇 绘制椭圆 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> ...

  4. 【朝花夕拾】Android自定义View篇之(三)Canvas绘制文字

    前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/10968358.html],谢谢! 前面的文章中在介绍Canvas的时候,提到过后续单独讲Can ...

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

    C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用Poi ...

  6. C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字

    C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...

  7. Canvas入门(3):图像处理和绘制文字

    来源:http://www.ido321.com/997.html 一.图像处理(非特别说明,所有结果均来自最新版Google) 在HTML 5中,不仅可以使用Canvas API绘制图形,也可以用于 ...

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

    一.绘制文字 除了绘制图形以外,还可以使用QPainter::darwText()函数来绘制文字,也可以使用QPainter::setFont()设置文字所使用的字体,使用QPainter::font ...

  9. [Qt2D绘图]-04绘制文字&&绘制路径

    注:学习自<Qt Creator 快速入门>第三版.   文档中的示例参考 Qt Example推荐:Painter Paths Example和Vector Deformation   ...

随机推荐

  1. Mkdir- Linux必学的60个命令

    1.作用 mkdir命令的作用是建立名称为dirname的子目录,与MS DOS下的md命令类似,它的使用权限是所有用户. 2.格式 mkdir [options] 目录名 3.[options]主要 ...

  2. Nmap扫描原理(上)

    转自:https://blog.csdn.net/qq_34398519/article/details/89055991 Nmap是一款开源免费的网络发现(Network Discovery)和安全 ...

  3. [转]用DateTime.ToString(string format)输出不同格式的日期

    DateTime.ToString()函数有四个重载.一般用得多的就是不带参数的那个了.殊不知,DateTime.ToString(string format)功能更强大,能输出不同格式的日期.以下把 ...

  4. hbuilder scss自动编译

    hbuilder 命令参数:  --no-cache %FileName% %FileBaseName%.css --style compact

  5. sql server 获取指定格式的当前日期

    使用sqlserver日期函数中的getdate()可以获取当现的日期,下面就将为您介绍这种使用sqlserver日期函数获取当前日期的方法. 但是如果我们只需要得到当前的日期,不需要时间部分,或者不 ...

  6. x86架构:x86架构

    ylbtech-x86架构:x86架构 X86架构(The X86 architecture)是微处理器执行的计算机语言指令集,指一个intel通用计算机系列的标准编号缩写,也标识一套通用的计算机指令 ...

  7. 60行JavaScript代码俄罗斯方块

    教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏   早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下( ...

  8. Python学习笔记(五)函数和代码复用

    函数能提高应用的模块性,和代码的重复利用率.在很多高级语言中,都可以使用函数实现多种功能.在之前的学习中,相信你已经知道Python提供了许多内建函数,比如print().同样,你也可以自己创建函数, ...

  9. Python学习之循环--绕圈圈(蛇形盘)

    效果图: 注意哦,右边多出来的一点不是程序有问题,是打印的时候我用的\t,但100,三个字符顶格的时候给顶出去的,我太懒了,不想再调输出格式了,就这么凑合看吧 实现代码: sum = int(inpu ...

  10. Consul3-使用consul作为配置中心

    在前面的文章中学习了consul在windows下的安装配置,然后consul作为spring boot的服务发现和注册中心,详细的参考: https://blog.csdn.net/j9038291 ...