当前位置:网站首页>D2dengine edible tutorial (1) -- the simplest program
D2dengine edible tutorial (1) -- the simplest program
2022-07-23 11:19:00 【dgaf】
The goal is : Create a window with a blue background
1. First the D2DEngine Four files of are added to the project
Right click on the item -> add to -> Existing items
2. Set the compliance mode to no (/permissive)
project -> attribute ->C/C+±> Language -> The compliance mode is set to no
If the mode is not set, an error will be reported . My understanding is that , Because Microsoft is VS2019 After starting, the requirements for value transfer become stricter , Image is not allowed by default char turn const char Such narrowing conversion , May cause memory leak . Here we don't need , Just turn it off .
3. Code
Let's start with Templates , This template is for all D2DEngine The programs are Universal
#include<windows.h>
#include"D2D_DRAW.h" // Directly reference the header file
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; // Get rid of ReDraw Redraw
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
// Set to NULL_BRUSH, No need to come from GDI Any drawing of
RegisterClassEx(&wcex);
HWND hwnd = CreateWindowW(wcex.lpszClassName, L"D2D",
WS_OVERLAPPED | WS_SYSMENU,
// WS_OVERLAPPEDWINDOW Will trigger OnSize news , Don't deal with this message for the time being
0, 0, 640, 480,
nullptr,
nullptr,
hin,
nullptr
);
ShowWindow(hwnd, CmdShow);
// because CS_GLOBALCLASS, It must be used. ShowWindow Display window
//————————————————————————————————————————————————————————————————————
//TODO: Do it here Direct2D Rendering
//————————————————————————————————————————————————————————————————————
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
This program only needs to render in TODO part ( See note ) Just write it .
| Member functions | effect |
|---|---|
| bool Initialize( hwnd, BrushColor) | D2DEngine Initialization of the device , Anything to do with D2DEngine All operations must go through this step first . Otherwise rendering fails . The parameters are :hwnd Handle ( Required ),BrushColor Brush color ( optional , Default white ) |
| void BeginDraw() | The signal that the program begins to draw ,D2DEngine The draw ( Rendering ) Operation can only be carried out after this function , Otherwise it will not work |
| void Clear(ClearColor) | ( Rendering ) Clear the screen and render the target background color , Parameters :ClearColor Background color ( optional , Default white ) |
| void EndDraw() | The signal that the program stops drawing , Every time BeginDraw One time must end with this function . |
Be careful : belt ( Rendering ) The function of must be in BeginDraw() And EndDraw() It can only take effect between , Like the following code :
Engine->BeginDraw();Engine->Clear(); // Rendering functionEngine->EndDraw();
D2D.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, 640, 480,
nullptr,
nullptr,
hin,
nullptr
);
ShowWindow(hwnd, CmdShow);
//————————————————————————————————————————————————————————————————————
D2DEngine* Engine = new D2DEngine; // establish D2DEngine object
Engine->Initialize(hwnd); // Initialize with handle D2DEngine
Engine->BeginDraw();
Engine->Clear(ColorF::Blue); // Empty the window and render it blue
Engine->EndDraw();
//————————————————————————————————————————————————————————————————————
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
D2DEngine Resource link :https://blog.csdn.net/DGAF2198588973/article/details/125909836
If you like, please like it
边栏推荐
- 通用视图,序列化器
- py程序可以运行,但打包出的exe运行提示错误:加载“cv2”二进制扩展时检测到递归。请检查OpenCV安装。
- Mixed view base class
- [flink]flink on yarn之flink-conf最简单配置
- Understanding of closures of JS
- uni-app小程序中v-show与display:flex一起使用时v-show不生效!
- [Doris]配置和基本使用contens系统(有时间继续补充内容)
- DWI图像 从DICOM Tag识别 b value 的方法
- 用getchar清理缓冲区(强烈推荐,C语言易错典型)
- Rice mall registration
猜你喜欢
随机推荐
【无标题】
The super simple face recognition API can realize face recognition in just a few lines of code
[untitled]
解决手动查询Oracle数据库时间格式不正确的问题(DATE类型)
大米商城注册
MySQL statement queries all child nodes of a level node
项目流程总结
Application of higher-order functions: handwritten promise source code (III)
高阶函数的应用:手写Promise源码(四)
请求数据获取与响应
When using cache in sprintboot, the data cannot be loaded
【uiautomation】键指令大全(以及三种调用方式)+常用鼠标动作+SendKeys+Inspect学习
gbk编解码器无法解码的问题,有可能出题出在文件名上
[部署]presto-server-0.261.tar.gz的集群部署和启动
高阶函数的应用:手写Promise源码(三)
Inheritance mode of JS
用getchar清理缓冲区(强烈推荐,C语言易错典型)
js的事件执行机制(Event loop)
2. Analysis of the return value of the startup function
View set and route


![[监控部署实操]基于granfana展示Prometheus的图表和loki+promtail的图表](/img/34/b7a05bff05e1d3a1daef4fb2b98a92.png)





