From 75eb1fc143950d9269da7434ecd935f7f09a03e6 Mon Sep 17 00:00:00 2001 From: LuChiChick <1084116302@qq.com> Date: Mon, 10 Nov 2025 20:29:06 +0800 Subject: [PATCH] add font msyh.ttc && Basic main panel for UI widgets --- Inc/Config.hpp | 2 + Inc/UI_Layout.hpp | 9 ++++ Src/Main.cpp | 113 ++++++++++++++++++++-------------------------- Src/UI_Layout.cpp | 35 ++++++++++++++ 4 files changed, 94 insertions(+), 65 deletions(-) create mode 100644 Inc/UI_Layout.hpp create mode 100644 Src/UI_Layout.cpp diff --git a/Inc/Config.hpp b/Inc/Config.hpp index 7da8de1..fac0ce2 100644 --- a/Inc/Config.hpp +++ b/Inc/Config.hpp @@ -1,5 +1,7 @@ #ifndef __CONFIG_HPP__ #define __CONFIG_HPP__ +// 主窗体标题 +#define MAIN_FRAME_TITTLE L"DLT Splitter —— Dev by : LuChiChick" #endif \ No newline at end of file diff --git a/Inc/UI_Layout.hpp b/Inc/UI_Layout.hpp new file mode 100644 index 0000000..e6738f1 --- /dev/null +++ b/Inc/UI_Layout.hpp @@ -0,0 +1,9 @@ +#ifndef __UI_LAYOUT__ +#define __UI_LAYOUT__ + +#include "imgui.h" + +// UI布局 +void UI_Layout(); + +#endif \ No newline at end of file diff --git a/Src/Main.cpp b/Src/Main.cpp index f776b1a..fa04576 100644 --- a/Src/Main.cpp +++ b/Src/Main.cpp @@ -9,10 +9,12 @@ extern "C" #include "d3d11.h" #include "tchar.h" #include "wchar.h" +} // 自定义头文件 -#include "Global_Variables.hpp" -} +#include "Config.hpp" // 配置文件 +#include "Global_Variables.hpp" // 全局变量相关 +#include "UI_Layout.hpp" // UI布局相关 // 辅助函数声明 bool CreateDeviceD3D(HWND hWnd); // 创建 Direct 3D 设备 @@ -52,17 +54,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine RegisterClassExW(&wc); // 创建窗体 - HWND hwnd = CreateWindowW(wc.lpszClassName, // 创建的窗体类名 - L"DLT Splitter —— Dev by: LuChiChick", // 窗体标题 - WS_OVERLAPPEDWINDOW, // 窗体风格为标准可重叠的顶层窗口 - 100, // 起始坐标x - 100, // 起始坐标y - (int)(1280 * main_scale), // 窗体长 - (int)(720 * main_scale), // 窗体宽 - nullptr, // 父窗体句柄 - nullptr, // 菜单句柄 - wc.hInstance, // 创建实例句柄 - nullptr); // 额外的事件参数指针 + HWND hwnd = CreateWindowW(wc.lpszClassName, // 创建的窗体类名 + MAIN_FRAME_TITTLE, // 窗体标题 + WS_OVERLAPPEDWINDOW, // 窗体风格为标准可重叠的顶层窗口 + 100, // 起始坐标x + 100, // 起始坐标y + (int)(1280 * main_scale), // 窗体长 + (int)(720 * main_scale), // 窗体宽 + nullptr, // 父窗体句柄 + nullptr, // 菜单句柄 + wc.hInstance, // 创建实例句柄 + nullptr); // 额外的事件参数指针 // 在窗体内创建 Direct 3D 设备 if (!CreateDeviceD3D(hwnd)) @@ -138,12 +140,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine // io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"); // ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf"); // IM_ASSERT(font != nullptr); - } - // Our state - bool show_demo_window = true; - bool show_another_window = false; - ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + // 加载字体为微软雅黑 + io.Fonts->AddFontFromFileTTF("c:/windows/Fonts/msyh.ttc", 15.0f * main_scale, NULL, io.Fonts->GetGlyphRangesChineseFull()); + } // 主事件循环 bool done = false; @@ -176,61 +176,44 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine ImGui_ImplWin32_NewFrame(); ImGui::NewFrame(); - // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). - if (show_demo_window) - ImGui::ShowDemoWindow(&show_demo_window); + // 此处进行UI布局 + UI_Layout(); - // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. + // 渲染部分 { - static float f = 0.0f; - static int counter = 0; + // 调用 ImGui 渲染器进行预渲染 + ImGui::Render(); - ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. + // 设置D3D渲染目标 + g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr); - ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) - ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state - ImGui::Checkbox("Another Window", &show_another_window); + // 创建一个带透明度通道的RGB色 + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + const float clear_color_with_alpha[4] = {clear_color.x * clear_color.w, + clear_color.y * clear_color.w, + clear_color.z * clear_color.w, + clear_color.w}; + // 使用创建的RGB色擦除D3D渲染画布 + g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha); - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f - ImGui::ColorEdit3("clear color", (float *)&clear_color); // Edit 3 floats representing a color + // 渲染 ImGui 原始画面 + ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); - if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) - counter++; - ImGui::SameLine(); - ImGui::Text("counter = %d", counter); + // 启用脱离窗口渲染时更新主窗体外的部分 + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + } - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); - ImGui::End(); + // 呈现当前帧到画布 + // HRESULT hr = g_pSwapChain->Present(0, 0); // 非垂直同步 + HRESULT hr = g_pSwapChain->Present(1, 0); // 垂直同步 + // HRESULT hr = g_pSwapChain->Present(2, 0); // 等待双倍的垂直同步时间 + + // 更新交换链阻塞状态 + g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED); } - - // 3. Show another simple window. - if (show_another_window) - { - ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) - ImGui::Text("Hello from another window!"); - if (ImGui::Button("Close Me")) - show_another_window = false; - ImGui::End(); - } - - // Rendering - ImGui::Render(); - const float clear_color_with_alpha[4] = {clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w}; - g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr); - g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha); - ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); - - // Update and Render additional Platform Windows - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - ImGui::UpdatePlatformWindows(); - ImGui::RenderPlatformWindowsDefault(); - } - - // Present - HRESULT hr = g_pSwapChain->Present(1, 0); // Present with vsync - // HRESULT hr = g_pSwapChain->Present(0, 0); // Present without vsync - g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED); } // 收尾清理工作 diff --git a/Src/UI_Layout.cpp b/Src/UI_Layout.cpp new file mode 100644 index 0000000..245aef3 --- /dev/null +++ b/Src/UI_Layout.cpp @@ -0,0 +1,35 @@ +#include "UI_Layout.hpp" + +// UI布局 +void UI_Layout() +{ + // 配置窗口绘制位置为Dear ImGUI生成的主窗口 + const ImGuiViewport *viewport = ImGui::GetMainViewport(); + // 下一个窗体绘制到主窗体位置 + ImGui::SetNextWindowPos(viewport->WorkPos); + // 下一个窗体绘制为主窗体大小 + ImGui::SetNextWindowSize(viewport->WorkSize); + // 设置下一个焦点窗体为主窗体 + ImGui::SetNextWindowViewport(viewport->ID); + + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); // 绘制风格栈压入窗口圆角为0 + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); // 绘制风格栈压入窗体边框宽度为0 + + // 窗体选项配置 + ImGuiWindowFlags window_flags = ImGuiWindowFlags_None; + // 设置窗体相关属性,无标题栏,无折叠标志,不可重塑大小,不可移动 + window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; + // 设置窗体相关属性,收到焦点时不移动到前台,禁用焦点事件 + window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; + // 设置窗体相关属性,不允许停靠 + window_flags |= ImGuiWindowFlags_NoDocking; + + ImGui::Begin("Main Panel", NULL, window_flags); + { + // 退出绘制风格栈中的设置项 + ImGui::PopStyleVar(2); + } + ImGui::End(); + + ImGui::ShowDemoWindow(nullptr); +} \ No newline at end of file