当前位置: 首页 > backend >正文

ImGui 基础用法

ImGui 基础用法

简介

ImGui(Dear ImGui)是一个用于在C++应用程序中创建图形用户界面(GUI)的轻量级库。它设计用于快速、简便地在应用程序中嵌入调试和工具界面。

适合于游戏引擎(用于工具)、实时3D应用程序、全屏应用程序、嵌入式应用程序或在操作系统功能非标准的控制台平台上的任何应用程序。

特性

  • 最小化状态同步
  • 最小化用户端与ui相关的状态存储
  • 尽量减少设置和维护
  • 易于用于创建动态UI,以反映动态数据集
  • 易于用于创建代码驱动和数据驱动的工具
  • 易于用于创建临时的短期工具和长寿的、更复杂的工具
  • 易于分析和改进
  • 便携式设备,最小化依赖关系,并在目标设备上运行(控制台、手机等)
  • 高效的运行时和内存消耗
  • 实际测试,被游戏行业的许多主要厂商使用

前置条件

下载安装ImGui

注:运行代码时,需要提前安装必要的依赖库、正确初始化ImGui库

代码示例

官方示例 ImGui examples

具体以example_sdl2_sdlrenderer2为例,主体框架不用动,仅需修改 while循环里 界面绘制部分即可

// Dear ImGui: standalone example application for SDL2 + SDL_Renderer
// (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)// Learn about Dear ImGui:
// - FAQ                  https://dearimgui.com/faq
// - Getting Started      https://dearimgui.com/getting-started
// - Documentation        https://dearimgui.com/docs (same as your local docs/ folder).
// - Introduction, links and more at the top of imgui.cpp// Important to understand: SDL_Renderer is an _optional_ component of SDL2.
// For a multi-platform app consider using e.g. SDL+DirectX on Windows and SDL+OpenGL on Linux/OSX.#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include <stdio.h>
#include <SDL.h>#if !SDL_VERSION_ATLEAST(2,0,17)
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
#endif// Main code
int main(int, char**)
{// 初始化 图形渲染上下文// Setup SDLif (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0){printf("Error: %s\n", SDL_GetError());return -1;}// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UISDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif// Create window with SDL_Renderer graphics contextSDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+SDL_Renderer example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);if (window == nullptr){printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());return -1;}SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);if (renderer == nullptr){SDL_Log("Error creating SDL_Renderer!");return -1;}//SDL_RendererInfo info;//SDL_GetRendererInfo(renderer, &info);//SDL_Log("Current SDL_Renderer: %s", info.name);// Setup Dear ImGui contextIMGUI_CHECKVERSION();ImGui::CreateContext();ImGuiIO& io = ImGui::GetIO(); (void)io;io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controlsio.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls// Setup Dear ImGui styleImGui::StyleColorsDark();//ImGui::StyleColorsLight();// Setup Platform/Renderer backendsImGui_ImplSDL2_InitForSDLRenderer(window, renderer);ImGui_ImplSDLRenderer2_Init(renderer);// Load Fonts// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.// - Read 'docs/FONTS.md' for more instructions and details.// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !//io.Fonts->AddFontDefault();//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());//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);// Main loopbool done = false;while (!done){// Poll and handle events (inputs, window resize, etc.)// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.SDL_Event event;while (SDL_PollEvent(&event)){ImGui_ImplSDL2_ProcessEvent(&event);if (event.type == SDL_QUIT)done = true;if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))done = true;}if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED){SDL_Delay(10);continue;}// Start the Dear ImGui frameImGui_ImplSDLRenderer2_NewFrame();ImGui_ImplSDL2_NewFrame();ImGui::NewFrame();// 下面 1-3 为界面内容,可替换自己的内容// 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);// 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::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.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 stateImGui::Checkbox("Another Window", &show_another_window);ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0fImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a colorif (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)counter++;ImGui::SameLine();ImGui::Text("counter = %d", counter);ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);ImGui::End();}// 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();}// RenderingImGui::Render();SDL_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));SDL_RenderClear(renderer);ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), renderer);SDL_RenderPresent(renderer);}// CleanupImGui_ImplSDLRenderer2_Shutdown();ImGui_ImplSDL2_Shutdown();ImGui::DestroyContext();SDL_DestroyRenderer(renderer);SDL_DestroyWindow(window);SDL_Quit();return 0;
}

基础用法

1.ImGui::Begin() / ImGui::End():

  • 作用: 定义一个可嵌套的区域,开始和结束ImGui的绘图。

  • 示例:

    ImGui::Begin("My Window");
    // GUI 元素
    ImGui::End();
    

修改窗口位置、大小

ImGui::Begin("window1");
{// 设置窗口位置 大小//ImVec2 pos = { 100, 100 };//ImGui::SetWindowPos(pos); //ImVec2 nSize = { 300, 200 };//ImGui::SetWindowSize(nSize);
}
ImGui::End();// 方式1:就近设置
ImGui::Begin("window2");
{//设置窗口位置、大小ImVec2 pos = { 100, 100+200+20 };ImGui::SetWindowPos(pos);ImVec2 nSize = { 300, 200 };ImGui::SetWindowSize(nSize);
}        
ImGui::End();//方式2:通过窗口名 设置窗口位置
ImVec2 pos = { 100, 100 };
ImGui::SetWindowPos("window1", pos);
ImVec2 nSize = { 300, 200 };
ImGui::SetWindowSize("window1", nSize);

2.ImGui::Text():

  • 作用: 在GUI中显示文本。

  • 示例:

    ImGui::Text("Hello, World!");
    

进阶 - ImGui::SetWindowFontScale 设置局部字体大小

ImGui::Text(u8"这是默认大小的文本");// 在局部使用较大的字体
{// 设置字体缩放比例为1.5ImGui::SetWindowFontScale(1.5f); ImGui::Text(u8"这是较大的文本");// 恢复默认字体大小ImGui::SetWindowFontScale(1.0f);
}// 这里的文本将以默认大小显示
ImGui::Text(u8"这是另一个默认大小的文本");

3.ImGui::Button():

  • 作用: 创建一个按钮。

  • 示例:

    // ImGui::Button("btnName", ImVec2(80, 20));
    // 只能响应 鼠标左键点击
    if (ImGui::Button("left Click btn")) 
    {// 按钮被点击时执行的代码
    }
    

扩展版本 EX版本 (需要头文件"imgui_internal.h")

ImGui::ButtonEx("btnName", ImVec2(80, 20), 具体的参数)

示例:

bool IsRightClick = ImGui::ButtonEx("right click btn", ImVec2(160, 20), ImGuiButtonFlags_::ImGuiButtonFlags_MouseButtonRight);
if (IsRightClick)
{MessageBoxA(NULL, "鼠标右键被按下了", 0, 0);
}

4.ImGui::InputText():

  • 作用: 创建一个可输入文本的文本框。

  • 示例:

    // 单行文本框
    ImGui::InputText();// 简单带占位提示的文本框
    char buffer[256] = "Input something";
    ImGui::InputText("Enter Text", buffer, sizeof(buffer));// 高级用法:带过滤功能的文本框char bufferAdv[256] = "Input something";
    ImGui::InputText("Enter Text", bufferAdv, sizeof(bufferAdv),ImGuiInputTextFlags_CallbackCharFilter, testEditCallback);// 过滤函数
    static int testEditCallback(ImGuiInputTextCallbackData* data)
    {printf("data->EventChar=%d buf=%s EventKey=%d EventFlag=%d\r\n",data->EventChar,data->Buf,data->EventKey,data->EventFlag);if (data->EventChar >= '0' && data->EventChar <= '9'){return 1; //过滤0~9 屏蔽掉输入}else{return 0;}
    }

5.ImGui::InputTextMultiline():

  • 作用: 创建一个多行文本框。

  • 示例:

    char buffer[256] = "Input something";
    ImGui::InputTextMultiline("Enter Text", buffer, sizeof(buffer), ImVec2{ 200, 100 });
    

6.ImGui::SliderXXX():

  • 作用: 创建一个对应数值类型的滑动条。

  • 示例:

    // 整数滑动条
    // ImGui::SliderInt("名称", &返回值, 最小值,  最大值, 显示格式);
    static int OutValue1 = 30;
    static int OutValue2 = 30;ImGui::SliderInt("slider int", &OutValue1, 1, 60);
    ImGui::Text(u8"测试滑块 返回值=%d", OutValue1);ImGui::SliderInt("slider int", &OutValue2, 1, 60, "~%X~");
    ImGui::Text(u8"测试滑块 返回值=%d", OutValue2);// 浮点数滑动条
    float value = 0.5f;
    ImGui::SliderFloat("Slider", &value, 0.0f, 1.0f);
    

7.ImGui::Checkbox():

  • 作用: 创建一个复选框。

  • 示例:

    bool checked = false;
    // 参数:名称、状态
    ImGui::Checkbox("Enable Feature", &checked);
    

8.ImGui::RadioButton():

  • 作用: 创建单选框。

  • 示例:

    // ImGui::RadioButton(标题, 条件)
    // 方式1:简单
    enum mymode
    {Mode1,Mode2,Mode3,
    };
    static mymode iValue;
    bool IsDown1 = ImGui::RadioButton(u8"单选框1", iValue == Mode1);
    ImGui::SameLine();       //与下一行显示在同一行
    bool IsDown2 = ImGui::RadioButton(u8"单选框2", iValue == Mode2);bool IsDown3 = ImGui::RadioButton(u8"单选框3", iValue == Mode3);if (IsDown1)
    {iValue = Mode1;
    }
    if (IsDown2)
    {iValue = Mode2;
    }
    if (IsDown3)
    {iValue = Mode3;
    }
    

9.ImGui::ColorEdit3() / ImGui::ColorEdit4():

  • 作用: 创建一个颜色选择器。

  • 示例:

    ImVec4 color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
    ImGui::ColorEdit4("Color", (float*)&color);
    

10.ImGui::Image():

  • 作用: 在GUI中显示图像。

  • 示例:

    ImTextureID textureID = ...; // 图像的纹理ID
    ImVec2 imageSize(100, 100);
    ImGui::Image(textureID, imageSize);
    

11.ImGui::BeginChild() / ImGui::EndChild():

  • 作用: 创建一个子区域,允许在其中放置其他GUI元素。

  • 示例:

    ImGui::BeginChild("Child Window");
    // 子窗口内的 GUI 元素
    ImGui::EndChild();
    

12.ImGui::SameLine():

  • 作用: 将下一个元素放在同一行上。

  • 示例:

    ImGui::Text("Left");
    ImGui::SameLine();
    ImGui::Text("Right");
    

13.ImGui::BeginMenuBar() / ImGui::EndMenuBar():

  • 作用: 创建和结束菜单栏。

  • 示例:

    // BeginMenu
    if (ImGui::BeginMenu(u8"菜单名字")){if (ImGui::MenuItem(u8"1")){}ImGui::MenuItem(u8"2");ImGui::MenuItem(u8"3");ImGui::MenuItem(u8"4", NULL, &Menu2, true);ImGui::EndMenu();};ImGui::End();// BeginMenuBar
    if (ImGui::BeginMenuBar()) 
    {if (ImGui::BeginMenu("File")) {// 菜单项ImGui::EndMenu();}ImGui::EndMenuBar();
    }
    

14.ImGui::BeginListBox() / ImGui::EndListBox():

  • 作用: 创建和结束列表控件。

  • 示例:

// 方式1:ListBox创建列表控件
ImGui::Begin(u8"List框测试2");
{ImVec2 pos = { 100 , 100 + 200 };ImGui::SetWindowPos(pos);ImVec2 nSize = { 300, 200 };ImGui::SetWindowSize(nSize);static int Number2 = 4;const char* items[] = { "l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9" };ImGui::Text(u8"选中%d\r\n", Number2);ImGui::ListBox("listboxtext", &Number2, items, sizeof(items) / sizeof(char*) , -1);
}
ImGui::End();// 方式2:BeginListBox创建列表控件
ImGui::Begin(u8"List框测试");
{ImVec2 pos = { 100 , 100};ImGui::SetWindowPos(pos);ImVec2 nSize = { 300, 200 };ImGui::SetWindowSize(nSize);static int Number = 4;const char* items[] = { "l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9" };ImGui::Text(u8"选中%d\r\n", Number);if (ImGui::BeginListBox("ListBox")){for (int n = 0; n < IM_ARRAYSIZE(items); n++){const bool IsDown = (Number == n);if (ImGui::Selectable(items[n], Number)){Number = n;}if (IsDown){ImGui::SetItemDefaultFocus();}}ImGui::EndListBox();}};
ImGui::End();

15.ImGui::TreeNode() / ImGui::TreePop():

  • 作用: 创建树形结构。

  • 示例:

    if (ImGui::TreeNode("Node_1")) {// 节点展开时的内容ImGui::Text("Child content 1");ImGui::TreePop(); // 必须与 TreeNode 配对
    }if (ImGui::TreeNode("Node_2")) {ImGui::Text("Child content 2");ImGui::TreePop();
    }
    

16.ImGui::BeginTabBar() / ImGui::EndTabBar():

  • 作用: 创建和结束页面控制。

  • 示例:

    ImGui::Begin(u8"页测试");
    {ImVec2 pos = { 100 , 100 + 200 };ImGui::SetWindowPos(pos);ImVec2 nSize = { 300, 200 };ImGui::SetWindowSize(nSize);if (ImGui::BeginTabBar(u8"页面组",  0)){if (ImGui::BeginTabItem(u8"页1")){ImGui::EndTabItem();}if (ImGui::BeginTabItem(u8"页2")){ImGui::EndTabItem();}if (ImGui::BeginTabItem(u8"页3")){ImGui::EndTabItem();}if (ImGui::BeginTabItem(u8"页4")){ImGui::EndTabItem();}ImGui::EndTabBar();}}
    ImGui::End();
    

17.**ImGui::ProgressBar():

  • 作用: 创建进度条。

  • 示例:

    // ImGui::ProgressBar(数值, 控件大小);static float Value = 0.1f;
    Value = Value + 0.001f;
    ImGui::ProgressBar(Value, ImVec2(-1.0f, 0.0f));
    

17.**ImGui::BeginTable()\ ImGui::EndTable():

  • 作用: 创建单元格。

  • 示例:

    // 方式1:静态创建
    ImGui::Begin(u8"表格测试");
    {if (ImGui::BeginTable("table1", 8, ImGuiTableFlags_Borders)){for (int row = 0; row < 4; row++){//转到下一行ImGui::TableNextRow();for (int column = 0; column < 8; column++){//指定列ImGui::TableSetColumnIndex(column);//显示单元格 内容ImGui::Text(u8"单元 (%d,%d)", row, column); //单元格}}ImGui::EndTable();}
    }
    ImGui::End();// 方式2:动态创建
    ImGui::Begin(u8"表格测试");
    {if (ImGui::BeginTable("table1", 3, ImGuiTableFlags_BordersInner)){for (int row = 0; row < 4; row++){ImGui::TableNextRow();for (int column = 0; column < 3; column++){ImGui::TableSetColumnIndex(column);char textid[265];sprintf_s(textid, u8"##单元格(%d,%d)", row, column);//添加ImGui::InputText(textid, Buf[row][column], 256); //单元格}}ImGui::EndTable();//标志位置0initonce = false;}
    }
    ImGui::End();
    

18.**ImGui::SetCursorPos() :

  • 作用: 用于设置 下一个控件 的绘制起始位置(相对于当前窗口/父组件的左上角)。

  • 示例:

    // 方式1:绝对定位
    // 设置位置 X=100px, Y=50px
    ImGui::SetCursorPos(ImVec2(100, 50)); 
    ImGui::Button("Fixed Position Button");// 仅设置X轴
    ImGui::SetCursorPosX(200); 
    ImGui::Text("This text starts at X=200");
    // 仅设置Y轴
    ImGui::SetCursorPosY(80); 
    ImGui::Checkbox("Checkbox at Y=80", &bValue);// 方式2:动态计算位置
    // 获取窗口尺寸
    ImVec2 window_size = ImGui::GetWindowSize();
    // 居中按钮
    ImGui::SetCursorPos(ImVec2((window_size.x - 100) * 0.5f, // 按钮宽度100px(window_size.y - 40) * 0.5f    // 按钮高度40px
    ));
    ImGui::Button("Centered Button");// 混合布局:自动布局 + 手动布局
    // 自动布局开始
    ImGui::Text("Auto-layout Item 1");// 手动插入定位
    ImGui::SetCursorPosX(ImGui::GetWindowWidth() - 120); // 右对齐
    if (ImGui::Button("Right-aligned")) { /* ... */ }// 继续自动布局
    ImGui::Text("Auto-layout Item 2"); 

19.ImGui::SetNextWindowPos() / ImGui::SetNextWindowSize():

  • 作用: 在调用 Begin() 前预设窗口位置,支持 绝对定位相对定位

  • 示例:

    // 设置窗口位置
    ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_condition, pivot = ImVec2(0,0));
    // 设置窗口大小
    ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
    ImGui::Begin("Window Title");
    // ... 窗口内容 ...
    ImGui::End();

20.ImGui::PushStyleColor() / ImGui::PopStyleColor():

  • 作用: 设置UI控件颜色。

  • 示例:

    // ImGui::PushStyleColor(ImGuiCol_Button, 0xFFFF0000);
    // your ui here
    // ImGui::PopStyleColor();// 按钮颜色
    ImGui::PushStyleColor(ImGuiCol_Button, 0xFFFF0000);
    ImGui::Button("test");
    ImGui::PopStyleColor();// 文字颜色
    ImGui::PushStyleColor(ImGuiCol_Text, 0xFF000FFF);
    ImGui::Text(u8"hahahahahahahahahah");
    ImGui::PopStyleColor();

21.**ImGui::AddFontFromFileTTF() **:

  • 作用: 加载字体。

  • 示例:

    //默认
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    io.Fonts->AddFontDefault();
    io.Fonts->AddFontFromFileTTF("font.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesChineseFull());

22.ImGui::CalcTextSize() / ImGui::CalcItemWidth():

  • 作用: 计算文本大小。

  • 示例:

    // 用法
    // ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width)ImVec2 textSize = ImGui::CalcTextSize("文字大小");

23.ImGui::BeginPopup() / ImGui::BeginPopupModal():

  • 作用: 普通弹出窗口、模态弹出窗口。

  • 示例:

    // 普通弹出窗口
    if (ImGui::BeginPopup("Popup")) 
    {// 用户可在此窗口打开时与其他窗口交互ImGui::EndPopup();
    }// 模态弹出窗口
    if (ImGui::BeginPopupModal("Modal")) {// 此处代码块内,其他窗口无法响应输入ImGui::EndPopup();
    }
    
窗口介绍
普通弹出窗口
  • 非阻塞性:用户可以与其他窗口交互
  • 自动关闭:点击窗口外区域或按 ESC 键自动关闭
  • 多实例支持:可同时打开多个普通弹出窗口
模态弹出窗口
  • 阻塞性:阻止所有其他窗口的交互
  • 强制关闭:必须通过按钮或代码显式关闭
  • 聚焦控制:自动获取焦点并保持在最前
窗口对比
选择依据普通弹出窗口模态弹出窗口
是否需要强制用户响应
是否允许后台操作
交互复杂度简单复杂
适用场景

普通弹出窗口适用场景

  • 上下文菜单(右键菜单)
  • 工具提示扩展(Hover 提示的详细内容)
  • 非关键性信息展示

模态弹出窗口适用场景

  • 关键操作确认(删除、覆盖保存)
  • 必须处理的错误提示
  • 用户输入依赖(需要先完成当前操作)

24.ImGui::SetClipboardText() / ImGui::EndMenuBar():

  • 作用: 复制到剪切板。

  • 示例:

    ImGui::SetClipboardText("your str");
    

todo 11.ImGui::BeginMenuBar() / ImGui::EndMenuBar():

  • 作用: 创建和结束菜单栏。

  • 示例:

    // BeginMenu
    if (ImGui::BeginMenu(u8"菜单名字")){if (ImGui::MenuItem(u8"1")){}ImGui::MenuItem(u8"2");ImGui::MenuItem(u8"3");ImGui::MenuItem(u8"4", NULL, &Menu2, true);ImGui::EndMenu();};ImGui::End();// BeginMenuBar
    if (ImGui::BeginMenuBar()) 
    {if (ImGui::BeginMenu("File")) {// 菜单项ImGui::EndMenu();}ImGui::EndMenuBar();
    }
    

参考资料

ImGui入门教程01:框架介绍

ImGui基础概念和简单函数介绍

J-0-ImGui的语法学习

http://www.xdnf.cn/news/4844.html

相关文章:

  • 第5章 深度学习和卷积神经网络
  • 飞算 用到妙处 AI辅助编程 - 双击方法名,自动识别到上下文中很方便
  • macOS Python 环境配置指南
  • Java死锁问题全解析:从原理到实战解决方案
  • 6:点云处理—QT三维窗口显示
  • Screeps Arena基础入门
  • 碰一碰发视频一键成片功能开发实践与技术解析
  • 字符宽度介绍
  • 仿LISP运算 - 华为OD机试真题(A卷、JavaScript题解)
  • 特征工程概述
  • QT 文件选择对话框 QFileDialog
  • DL/T645-2007电表协议简介以及请求应答帧格式
  • RSAC 2025观察:零信任+AI=网络安全新范式
  • 【HCIP】----OSPF综合实验
  • Day 14 训练
  • 雷赛伺服电机
  • 山东安全员A证的考试科目有哪些?
  • MySQL中隔离级别那点事
  • 主备Smart Link + Monitor Link组网技术详细配置
  • 【LeetCode】删除排序数组中的重复项 II
  • 2018机械行业ERP软件发展趋势
  • 从 ImageNet 到产业革命:AlexNet 作为破局者的三大核心创新及其时代穿透力
  • SKNet、空间注意力介绍
  • 1.MySQL数据库初体验
  • Matlab 基于Hough变换的人眼虹膜定位方法
  • Prometheus实战教程:k8s平台-node-exporter监控物理机
  • OPCUA,OPCDA与MODBUS学习笔记
  • RabbitMQ学习(第二天)
  • ConcurrentHashMap解析
  • 3中AI领域的主流方向:预测模型、强化学习和世界模型