如何在Imgui中使用图标字体
1、从github上搜索imgui,找到dear-imgui的下载处,下载源码并解压到硬盘任意目录中。如何在imgui中使用iconfont的说明就在源码目录中的misc/fonts/readme文件中。
2、有若干icon font可供选择,如FontAwesome,Openfonticons等。
这一步需要下载juliettef的cionfontcppheaders,此头文件包含utf-8字体的头文件。
3、添加头文件iconfsfontawesome4.h。
在源文件中添加以下代码。
io.Fonts->AddFontDefault();
ImFontConfig config;
config.MergeMode = true;
config.GlyphMinAdvanceX = 16.0f;
static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
io.Fonts->AddFontFromFileTTF("fontawesome-webfont.ttf",16.0f, &config, icon_ranges);
这一步的目的是程序在启动时加载必须的字体文件。
4、下载fontawesome-webfont.ttf 字体文件,与编译后的exe文件放到同一目录下。添加以下代码
ImGui::Begin("mytoolbar", 0, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus);
ImGui::Button(ICON_FA_SEARCH );
ImGui::SameLine();
ImGui::Button(ICON_FA_HOME);
ImGui::SameLine();
ImGui::Button(ICON_FA_PLAY_CIRCLE_O); ImGui::SameLine();
ImGui::Button(ICON_FA_LOCK); ImGui::SameLine();
ImGui::Button(ICON_FA_PLAY); ImGui::SameLine();
ImGui::Button(ICON_FA_PAUSE); ImGui::SameLine();
ImGui::Button(ICON_FA_STOP); ImGui::SameLine();
ImGui::Button(ICON_FA_TIMES_CIRCLE_O); ImGui::SameLine();
ImGui::Button(ICON_FA_KEY); ImGui::SameLine();
ImGui::Button(ICON_FA_USERS); ImGui::SameLine();
ImGui::Button(ICON_FA_FLOPPY_O); ImGui::SameLine();
ImGui::Button(ICON_FA_REFRESH); ImGui::SameLine();
ImGui::Button(ICON_FA_KEY); ImGui::SameLine();
ImGui::Button(ICON_FA_KEY); ImGui::SameLine();
ImGui::End();
即可创建一个带图形按钮的工具栏。