当前位置:网站首页>D2dengine edible tutorial (4) -- draw text
D2dengine edible tutorial (4) -- draw text
2022-07-28 03:33:00 【dgaf】
Catalog
D2DEngine Library links :Direct2D Of C++ Self made auxiliary Library ———D2DEngine ( Continuous updating )
The goal is : Draw text

Because it didn't come true GIF Dynamic map export can only use other software …
Not much to say , Code up
—————————————————————————————————————————————————————
| D2DEngine Member functions | effect |
|---|---|
| bool ChangeTextFormat(FontSize,TextColor,FontFamilyName); | Change text style , Set monochrome text , Parameters :FontSize font size ,TextColor The font color ,FontFamilyName Font cluster , Such as " Microsoft YaHei "“ Song style " etc. , Default " NSimSun ”. If the font cluster cannot be found, the system default font will be used ( Not going to happen COM error ) |
| void DrawText(text,x,y,width,height); | ( Rendering ) Draw monochrome text , Parameters :text Text ,x,y Relative to the render target Start coordinates of ,width Text drawing area Width of ,height The height of the text drawing area , If the text exceeds the drawing area , Will open up space to draw down or right |
D2DDrawText.CPP
#include<windows.h>
#include"D2D_DRAW.h"
using namespace D2D1;
LRESULT CALLBACK CallBackFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hin, HINSTANCE, LPSTR, int CmdShow)
{
MSG msg = {
};
WNDCLASSEX wcex = {
};
memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = CallBackFunc;
wcex.lpszClassName = L"D2D";
wcex.hInstance = hin;
wcex.style = CS_GLOBALCLASS;
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
RegisterClassEx(&wcex);
HWND hwnd = CreateWindowW(wcex.lpszClassName, L"D2D",
WS_OVERLAPPED | WS_SYSMENU,
0, 0, 900, 554,
nullptr,
nullptr,
hin,
nullptr
);
ShowWindow(hwnd, CmdShow);
D2DEngine* Engine = new D2DEngine; // establish D2DEngine object
Picture Pic(L" " .jpeg"); // Create a picture object
Engine->Initialize(hwnd); // Initialize with handle D2DEngine
Engine->TransformPictureIntoBitmap(Pic); // Convert pictures
Engine->BeginDraw(); // Start rendering
Engine->DrawPicture(Pic, 0, 0, 900, 554); // Draw the picture first
Engine->ChangeTextFormat(40, ColorF::White,L" Ye Genyou brush running calligraphy 2.0 edition ");
// Change text style , Font size: 40, The color is white , Font cluster ( family ) by " Ye Genyou brush running calligraphy 2.0 edition "
Engine->Drawtext(L" " , Not Wang Chen ", 400, 100, 400, 100);
// stay (400,100,800,200) Draw text in the area of , Make the text not covered by the picture
Engine->EndDraw(); // Stop rendering
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
D2DDrawText2.CPP
#include<windows.h>
#include<string>
#include<thread> // Thread library
#include"D2D_DRAW.h"
using namespace std;
using namespace D2D1;
D2DEngine* Engine; // overall situation D2DEngine, So that sub threads can use
Picture Pic(L" The Supreme Master disordered the Han .jpeg"); // Create a picture object
void D2DThread(); // Provide functions run by sub threads
LRESULT CALLBACK CallBackFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hin, HINSTANCE, LPSTR, int CmdShow)
{
MSG msg = {
};
WNDCLASSEX wcex = {
};
memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = CallBackFunc;
wcex.lpszClassName = L"D2D";
wcex.hInstance = hin;
wcex.style = CS_GLOBALCLASS;
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
RegisterClassEx(&wcex);
HWND hwnd = CreateWindowW(wcex.lpszClassName, L"D2D",
WS_OVERLAPPED | WS_SYSMENU,
0, 0, 1280, 642,
nullptr,
nullptr,
hin,
nullptr
);
ShowWindow(hwnd, CmdShow);
Engine = new D2DEngine; // establish D2DEngine object
Engine->Initialize(hwnd); // Initialize with handle D2DEngine
Engine->TransformPictureIntoBitmap(Pic); // Convert pictures
thread thread1(D2DThread); // Create child threads
thread1.detach(); // Do not block main thread , Let the sub thread run alone
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void D2DThread()
{
wstring text(L" DongZhuo Wudao , The emperor's room fell , needless , Failure, , Wait for the heroes of life to help them ");
float color[3] = {
1.0f,0.0f,0.0f }; // The font color
for(size_t i = 0 ; i < text.size(); i++)
{
color[0] -= 0.03f;
color[1] += 0.03f;
color[2] += 0.03f;
Engine->BeginDraw(); // Start rendering
Engine->DrawPicture(Pic, 0, 0, 1280, 642); // Draw the picture first
Engine->ChangeTextFormat(40, color, L" Ye Genyou brush running calligraphy 2.0 edition ");
// Change text style , Font size: 40, The color is white , Font cluster ( family ) by " Ye Genyou brush running calligraphy 2.0 edition "
Engine->Drawtext(&text.substr(0,i+1)[0],
75, 400, 1200, 100);
// stay (75,400,1200,100) Draw text in the area of , Make the text not covered by the picture
Engine->EndDraw(); // Stop rendering
Sleep(200); // stop it 200 millisecond
}
}

Be careful : Because we used Multithreading , When we click on the window while the program is running , The window will not get stuck and unresponsive , This is because the drawing process is placed in another thread that runs separately , Make the application still have space to process window messages . But you will find , Compiling this code many times will report an error , This is because C++ Standard thread library and Direct2D Are not compatible , Lead to resource conflict in critical areas , Then we will use Microsoft's thread library for D2DEngine Add features that support multithreading , Make its rendering process perfectly compatible .
Like it
边栏推荐
- When a dialog box pops up, the following form is not available
- 20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration
- "Introduction to engineering electromagnetic field" after class exercises with answers
- 一键重装win7系统详细教程
- Uniapp - make phone calls and send text messages
- 4天Excel实战训练营,0.01元特惠仅三天,赠200套学习资料包
- Acid characteristics of MySQL transactions and example analysis of concurrency problems
- 如何一键进行重装Win11系统
- Practice of online problem feedback module (16): realize the function of checking details
- 695. Maximum area of the island
猜你喜欢

Contour detection based on OpenCV (3)

What if the word selection box of win11 input method is missing?

verticle-align行内元素垂直居中对齐
![[5g NR] RRC reject analysis](/img/51/fc39804b39a9014be3130c09e5444c.png)
[5g NR] RRC reject analysis

如何让外网访问内网IP(esp8266网页使用)

Engineering Geology Practice - engineering geology problem set

Softek Barcode Reader 9.1.5

C -- switch case statement

How to reinstall win11 system with one click

The open source of "avoiding disease and avoiding medicine" will not go far
随机推荐
颜色的识别方法和探索 基于matlab
203.移除链表元素
Shell编写规范和变量
Shell:资源监控脚本和高负载报警
在线问题反馈模块实战(十六):实现查详情功能
Shell:一键部署pxe
沃尔沃:深入人心的“安全感” 究竟靠的是什么?
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
20条心灵鸡汤唯美句子,句句温情暖心!
过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
什么是虚函数?
Digital economy has become the biggest attraction
ThreadLocal usage scenario
The object array is converted to string and then separated by strings, including the competition to select the children of a field, or the sum,
MySQL的碎片有哪些
VMware virtual machine network settings
Color recognition method and exploration based on MATLAB
如何使用JDBC操作数据库
Raspberry pie development relay control lamp