-
Notifications
You must be signed in to change notification settings - Fork 9
/
DrawEng.cpp
219 lines (196 loc) · 7.07 KB
/
DrawEng.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <dwmapi.h>
#include "DrawEng.h"
#include "proc_help.h"
#include "DrawCallBack.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_dx11.h"
#include "imgui/imgui_impl_win32.h"
#include <d3d11.h>
#include <dinput.h>
#include <tchar.h>
#pragma comment(lib, "Dwmapi.lib")
#pragma comment(lib, "d3d11.lib")
ID3D11Device* g_pd3dDevice = NULL;
ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
IDXGISwapChain* g_pSwapChain = NULL;
ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
ImFont* font;
HWND myHWND;
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();
// Main code
void startDraw() {
int clsLen = getRandomInt(100, 200);
int wndLen = getRandomInt(100, 200);
char* clsName = (char*)malloc(clsLen);
char* wndName = (char*)malloc(wndLen);
memset(clsName, 0, clsLen);
memset(wndName, 0, wndLen);
rand_str(clsName, clsLen - 1);
rand_str(wndName, wndLen - 1);
//printf("clsName: %s\nwndName: %s\n", clsName, wndName);
WNDCLASSEXA wc = { sizeof(WNDCLASSEX), CS_HREDRAW, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL,clsName, NULL };
RegisterClassExA(&wc);
myHWND = CreateWindowExA(WS_EX_TOPMOST, wc.lpszClassName, wndName, WS_POPUP, 0, 0, 1920, 1080, NULL, NULL, GetModuleHandle(NULL), NULL);//创建窗口
ShowWindow(myHWND, SW_SHOWDEFAULT);
UpdateWindow(myHWND);
SetWindowLongA(myHWND, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST);
MARGINS marg = { -1 };
DwmExtendFrameIntoClientArea(myHWND, &marg);
if (!CreateDeviceD3D(myHWND)) {
CleanupDeviceD3D();
UnregisterClassA(wc.lpszClassName, wc.hInstance);
return;
}
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplWin32_Init(myHWND);//imgui链接上这个窗口
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);//初始化d3d设备
io.Fonts->AddFontDefault();
font = io.Fonts->AddFontFromFileTTF(R"(c:\Windows\Fonts\msyhbd.ttc)", 25.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());//msyhbd
IM_ASSERT(font != NULL);
// Our state
MSG msg;
ZeroMemory(&msg, sizeof(msg));
const float _0f = 0.f;
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
ImGui_ImplDX11_NewFrame();
while (msg.message != WM_QUIT) {//窗口没有关闭
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
continue;
}
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();//新帧
draw();
ImGui::Render();//渲染
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, &_0f);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0);//呈现
}
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
CleanupDeviceD3D();
DestroyWindow(myHWND);
UnregisterClassA(wc.lpszClassName, wc.hInstance);
return;
}
// Helper functions
bool CreateDeviceD3D(HWND hWnd) {
// Setup swap chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
DEVMODE devMode;
devMode.dmSize = sizeof(devMode);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = devMode.dmDisplayFrequency;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
UINT createDeviceFlags = 0;
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2,
D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel,
&g_pd3dDeviceContext) != S_OK)
return false;
CreateRenderTarget();
return true;
}
void CleanupDeviceD3D() {
CleanupRenderTarget();
if (g_pSwapChain) {
g_pSwapChain->Release();
g_pSwapChain = NULL;
}
if (g_pd3dDeviceContext) {
g_pd3dDeviceContext->Release();
g_pd3dDeviceContext = NULL;
}
if (g_pd3dDevice) {
g_pd3dDevice->Release();
g_pd3dDevice = NULL;
}
}
void CreateRenderTarget() {
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
pBackBuffer->Release();
}
void CleanupRenderTarget() {
if (g_mainRenderTargetView) {
g_mainRenderTargetView->Release();
g_mainRenderTargetView = NULL;
}
}
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return ::DefWindowProc(hWnd, msg, wParam, lParam);
}
RECT getGameWindowInfo(HWND hWnd, RECT recinfo) {
POINT local = { 0, 0 };
ClientToScreen(hWnd, &local);
GetClientRect(hWnd, &recinfo);
recinfo.left = local.x;
recinfo.top = local.y;
return recinfo;
}
void drawStrockText(ImDrawList* drawList, ImFont* font, float size, ImVec2 pos, ImColor col, const char* text) {
ImColor strockcol = IM_COL32(0.0f, 0.0f, 0.0f, 255.0f);
ImVec2 xL = pos;
ImVec2 xA = pos;
ImVec2 yL = pos;
ImVec2 yA = pos;
xL.x = xL.x - 1;
xA.x = xA.x + 1;
yL.y = yL.y - 1;
yA.y = yA.y + 1;
drawList->AddText(font, size, xL, strockcol, text, text + strlen(text), 0.f);
drawList->AddText(font, size, xA, strockcol, text, text + strlen(text), 0.f);
drawList->AddText(font, size, yL, strockcol, text, text + strlen(text), 0.f);
drawList->AddText(font, size, yA, strockcol, text, text + strlen(text), 0.f);
drawList->AddText(font, size, pos, col, text, text + strlen(text), 0.f);
}
void drawRectFilled(ImDrawList* drawList, ImVec4 rect, ImColor col) {
ImVec2 xy = { 0.f, 0.f };
xy.x = rect.x;
xy.y = rect.y;
ImVec2 zw = { 0.f, 0.f };
zw.x = xy.x + rect.z;
zw.y = xy.y + rect.w;
drawList->AddRectFilled(xy, zw, col);
drawList->AddRect(xy, zw, col);
}
/*绘制*/
void drawFrame(ImDrawList* drawList, ImVec4 rect, float px, ImColor col) {
drawLine(drawList, { rect.x, rect.y, rect.x + rect.z / 4, rect.y }, px, col);
drawLine(drawList, { rect.x, rect.y, rect.x, rect.y + rect.w / 4 }, px, col);
drawLine(drawList, { rect.x + rect.z, rect.y, rect.x + rect.z - rect.z / 4, rect.y }, px, col);
drawLine(drawList, { rect.x + rect.z, rect.y, rect.x + rect.z, rect.y + rect.w / 4 }, px, col);
drawLine(drawList, { rect.x, rect.y + rect.w, rect.x + rect.z / 4, rect.y + rect.w }, px, col);
drawLine(drawList, { rect.x, rect.y + rect.w, rect.x, rect.y + rect.w - rect.w / 4 }, px, col);
drawLine(drawList, { rect.x + rect.z, rect.y + rect.w, rect.x + rect.z - rect.z / 4, rect.y + rect.w }, px, col);
drawLine(drawList, { rect.x + rect.z, rect.y + rect.w, rect.x + rect.z, rect.y + rect.w - rect.w / 4 }, px, col);
}
void drawLine(ImDrawList* drawList, ImVec4 rect, float px, ImColor col) {
float sx = rect.x + gameRect.left;
float sy = rect.y + gameRect.top;
float ex = rect.z + gameRect.left;
float ey = rect.w + gameRect.top;
drawList->AddLine({ sx, sy }, { ex, ey }, col, px);
}