ImGui基本方法

目录

创建窗口

Begin函数

设置字体 

checkbox函数

Button函数

ImGui::SliderInt、ImGui::SliderFloat函数

SetCursorPos函数

style设置样式

Login基本要素

计算大小的函数


创建窗口

包含文件(以Opengl3为例):

#include "imgui.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"

ImGui::Begin()创建窗口,窗口展开则返回true:

	if (ImGui::Begin("Test"))
	{
		ImGui::Text("Aa");
	}ImGui::End();

  

设置窗口大小,若调用没有Next的函数,则后面的窗口都会以这个大小创建,ImVec2:

	ImGui::SetNextWindowSize(ImVec2(500, 500));//ImVec2(x, y)
	//ImGui::SetWindowSize(ImVec2(500, 500));
	if (ImGui::Begin("Test"))
	{
		
	}ImGui::End();

Begin函数

bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)

name:设置名称 ;
p_open:用于设置是否在右上角有关闭按钮 ,当按下按钮时,p_open为false;ImGuiWindowFlags:设置窗口属性,如_NoResize禁用改变大小,NoCollapse禁用折叠:

ImGui::Begin("Test"),NULL,ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)

设置字体 

加载字体的函数:

ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)

输入中文需要使用glyph_ranges参数:

		//默认
		io.Fonts->AddFontDefault();
		//加载字体
		ImFont* mainfont = io.Fonts->AddFontFromFileTTF("path.ttf", 18.f,NULL, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
		ImGui::PushFont(mainfont);
		//.....此处内容为mainfont字体
		ImGui::PopFont();
		io.Fonts->AddFontFromFileTTF("path/path0/font.ttf", 18.0f);
		io.Fonts->AddFontFromFileTTF("path/path1/font.ttf", 18.0f);
		io.FontDefault = io.Fonts->Fonts[0];

 checkbox函数

bool ImGui::Checkbox(const char* label, bool* v)

label:标签
v:是否勾选

	bool testbool = false;//在渲染循环外
	ImGui::SetNextWindowSize(ImVec2(500, 500));
	if (ImGui::Begin("Test"),NULL)
	{
		ImGui::Checkbox("check1", &testbool);
	}ImGui::End();

Button函数

bool ImGui::Button(const char* label, const ImVec2& size_arg)

label:标签;
size_args:按钮大小

点击后返回true:

		if (ImGui::Button("Click me!"))
		{
			...//点击后执行的任务
		}

ImGui::SliderInt、ImGui::SliderFloat函数

bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format, ImGuiSliderFlags flags)

v:绑定的变量;
v_min、v_max:最小、最大值;

		if (ImGui::Begin("Test"), NULL)
		{
			ImGui::SliderInt("Number Int", &initI, 1, 20);
			ImGui::SliderFloat("Number Float", &initF, 0, 10);

		}ImGui::End();

 

SetCursorPos函数

void ImGui::SetCursorPos(const ImVec2& local_pos)

用于设置目标位置,起始点为左上角:

		ImGui::Begin("Test");
			ImGui::SetCursorPos(ImVec2(50, 50));
			ImGui::Button("Test1", ImVec2(50, 50));
			ImGui::SetCursorPos(ImVec2(100, 100));
			ImGui::Button("Test2", ImVec2(50, 50));
		ImGui::End();

RadioButton

bool ImGui::RadioButton(const char* label, int* v, int v_button)

const char* label:名字
int* v:赋值的对象
int v_button:选择对应按钮时的赋值

示例:

目录

创建窗口

Begin函数

设置字体 

 checkbox函数

Button函数

ImGui::SliderInt、ImGui::SliderFloat函数

SetCursorPos函数

RadioButton

 style设置样式

Login基本要素

计算大小的函数


    static int model = 1;
        ImGui::RadioButton("MCF-STGCN", &model, 0);
        ImGui::SameLine();
        ImGui::RadioButton("DSSTGCN", &model, 1);

 

style设置样式

		ImGuiStyle& style = ImGui::GetStyle();
		style.WindowRounding = 0.0f;
		style.Colors[ImGuiCol_WindowBg] = ImVec4(0.8, 1.0, 0.2, 1.0);

  

style变量有许多属性,可以设置各种样式的外观,https://github.com/ocornut/imgui/issues/707网站有各种样式的参数可以参考。

Login基本要素

		char Input_username;
		char Input_password;
		std::string username = "AAA";
		std::string password = "1234";		
        //上方为全局变量
        ImGui::Begin("Test");
		ImGui::Text("UserName:");
		ImGui::InputText("##Input_username", &Input_username, MAXCHAR);
		ImGui::Text("PassWord:");
		ImGui::InputText("##Input_password", &Input_password, MAXCHAR, ImGuiInputTextFlags_Password);

		if (ImGui::Button("Login"))
		{
			if (&Input_username == username && &Input_password == password)
			{
				//...
			}
		}
		ImGui::End();

计算大小的函数

ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width)
float ImGui::CalcItemWidth()
### 关于 ImGui 的初学者教程 ImGui 是一个用于创建图形用户界面 (GUI) 的即时模式库,广泛应用于游戏开发和其他实时应用领域。以下是有关如何入门以及一些基础概念的内容。 #### 安装与配置 为了开始学习 `imgui`,需要先设置好开发环境并集成该库到项目中。通常情况下,可以按照官方文档中的说明完成安装过程[^1]。对于 C++ 开发者来说,推荐使用现代编译器支持标准特性以便更好地利用模板和 STL 功能。 ```cpp // Example of initializing imgui within your application context. #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" void setupImgui() { IMGUI_CHECKVERSION(); ImGui::CreateContext(); } ``` 上述代码片段展示了初始化 Imgui 所需的基本操作。需要注意的是,在实际部署之前还需要处理平台特定部分比如窗口管理等。 #### 基础控件介绍 一旦成功集成了 Imgui 库之后就可以尝试构建简单的 UI 组件了。下面列举了一些常用的 GUI 控制方法: - **Text Display**: 显示静态文本信息。 - **Buttons & Sliders**: 创建交互按钮或者滑动条来调整数值范围。 - **Input Fields**: 提供输入框让用户能够录入数据。 这些功能都可以通过调用相应的 API 函数实现,并且大多数都遵循类似的参数结构设计思路。 ```cpp if(ImGui::Button("Click Me")) { /* Do something */ } float value = 0.f; ImGui::SliderFloat("Speed", &value, 0.f, 100.f); ``` 以上例子演示了一个基本的按钮点击事件监听机制以及浮点数类型的调节组件定义方式。 #### 进阶主题探索 随着经验积累还可以深入研究更多高级话题例如自定义样式、多视图布局管理和复杂动画效果制作等等。这不仅有助于提升用户体验还能使程序更加灵活高效运行。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值