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篇 绘制文字的更多相关文章
- Direct2D 第6篇 绘制多种风格的线条
原文:Direct2D 第6篇 绘制多种风格的线条 上图是使用Direct2D绘制的线条,Direct2D在效率上比GDI/GDI+要快几倍,GDI/GDI+绘图是出了名的"慢", ...
- Direct2D 第5篇 绘制图像
原文:Direct2D 第5篇 绘制图像 我加载的图像是一张透明底PNG图像,背景使用渐变的绿色画刷 #include <windows.h> #include <d2d1.h> ...
- Direct2D 第2篇 绘制椭圆
原文:Direct2D 第2篇 绘制椭圆 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> ...
- 【朝花夕拾】Android自定义View篇之(三)Canvas绘制文字
前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/10968358.html],谢谢! 前面的文章中在介绍Canvas的时候,提到过后续单独讲Can ...
- C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字
C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用Poi ...
- C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字
C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...
- Canvas入门(3):图像处理和绘制文字
来源:http://www.ido321.com/997.html 一.图像处理(非特别说明,所有结果均来自最新版Google) 在HTML 5中,不仅可以使用Canvas API绘制图形,也可以用于 ...
- Qt 2D绘图之三:绘制文字、路径、图像、复合模式
一.绘制文字 除了绘制图形以外,还可以使用QPainter::darwText()函数来绘制文字,也可以使用QPainter::setFont()设置文字所使用的字体,使用QPainter::font ...
- [Qt2D绘图]-04绘制文字&&绘制路径
注:学习自<Qt Creator 快速入门>第三版. 文档中的示例参考 Qt Example推荐:Painter Paths Example和Vector Deformation ...
随机推荐
- Windows 系统文件夹目录挂载到 Linux服务器中
在Windows系统文件上传到Linux服务器时有时候很麻烦,因为Linux无界面的系统不像Windows系统一样,可以直接复制粘贴,下面方法可以解决Windows系统文件拷贝到Linux服务器. 1 ...
- Mysql 增加rownum
Mysql 增加rownum 实现(极度推荐) mysql本身不像oracle那样使用 rownum 来显示行号的功能.但是可以使用设置变量的方式来实现这一功能. mysql使用 @ 定义用户变量, ...
- Leetcode319. Bulb Switcher灯泡开关
初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡你关闭一次. 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 i 个灯泡切换一次 ...
- Windows API 第二篇 SHGetSpecialFolderPath
BOOL SHGetSpecialFolderPath( HWND hwndOwner, LPTSTR lpszPath, ...
- List -- 列表的一些操作
1,基本操作 2,内建函数 3,方法Method
- HZOI20190828模拟32题解
题面:https://www.cnblogs.com/Juve/articles/11428730.html chinese: 考虑$\sum\limits_{i=0}^{n*m}i*f_i$的意义: ...
- vue中使用vue-echarts
一.先说在原生里怎么使用echarts <!-- 1.引入echarts文件 --> <script src="echarts.min.js"></s ...
- inode学习笔记
在学习文件描述符时会看到有个inode概念,今天学习了一下. 在操作系统里,一个文件对应一个inode,inode存储了该文件相关信息,作用有一点点像内存的指针,通过他可以找到对应位置上的数据,但是i ...
- struts1 总结吧
以前都是使用struts2,但是新公司要使用struts1,所有只有硬着头皮上了. 一.Dynamic Method Invoc : 自定义的 Action 必须继承 DispatchAction 而 ...
- 我是如何在实际项目中解决MySQL性能问题
可能是本性不愿随众的原因,我对于程序员面试中动辄就是考察并发上千万级别的QPS向来嗤之以鼻,好像国内的应用都是那么多用户量一样,其实并发达到千万,百万以上的应用能有几个? 绝大多数的程序员面临的只是解 ...