uot;, FirstIdx,SecondIdx);
//一个表格示例
static ImGuiTableFlags flags =
ImGuiTableFlags_SizingFixedFit |
ImGuiTableFlags_RowBg |
ImGuiTableFlags_Borders |
ImGuiTableFlags_Resizable |
ImGuiTableFlags_Reorderable |
ImGuiTableFlags_Hideable;
if (ImGui::BeginTable("table0", 3, flags))
{
ImGui::TableSetupColumn("AAA", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("BBB", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("CCC", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableHeadersRow();
for (int row = 0; row < 5; row++)
{
ImGui::TableNextRow();
for (int column = 0; column < 3; column++)
{
ImGui::TableSetColumnIndex(column);
ImGui::Text("%s %d,%d", (column == 2) ? "Stretch" : "Fixed", column, row);
}
}
ImGui::EndTable();
}
}
void ShowPageView1()
{
ImGui::Text("功能%d -> 按钮%d -> 页面1", FirstIdx, SecondIdx);
//选项卡示例
ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None;
if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
{
if (ImGui::BeginTabItem("Avocado"))
{
ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Broccoli"))
{
ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Cucumber"))
{
ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
}
void ShowPageView2() { ImGui::Text("功能%d -> 按钮%d -> 页面2", FirstIdx, SecondIdx); }
void ShowPageView3() { ImGui::Text("功能%d -> 按钮%d -> 页面3", FirstIdx, SecondIdx); }
void ShowPageView4() { ImGui::Text("功能%d -> 按钮%d -> 页面4", FirstIdx, SecondIdx); }
隐藏控制台窗口
隐藏控制台窗口一般都是调用HWND hWnd = GetConsoleWindow()获取控制台窗口,然后使用ShowWindow(hWnd, SW_HIDE)隐藏窗口,这里使用另一种更合理的方法。
右击项目“属性->链接器->系统->子系统”,将控制台改为窗口,如下图所示:
然后将main函数改成WinMain函数,代码如下:
// Main code
//int main(int, char**)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
如果使用的不是win32,还需要引用以下头文件:
#include <windows.h>
顺便把程序标题也改一下,代码如下:
// Create window with graphics context
GLFWwindow* window = glfwCreateWindow(1280, 720, "演示程序", nullptr, nullptr);
打包程序
将生成的exe文件重命名为App.exe单独放到一个文件夹,初次打开需要手动调整布局,程序退出时布局自动保存在ImGui.ini文件中。最后打包的文件如下所示:
可以看到exe文件很小,只有500多KB。
总结
待解决问题
本文记录初次使用ImGui的整个过程,解决了使用过程中的大部分问题,但还有一些细节问题需要解决:
- 字体内存加载方式时软件退出报错
- 字体文件过大时加载耗时导致白屏
- 程序图标自定义,默认窗体标题栏的自定义
- 控件外观自定义,加载图标字体作为控件图标
这些问题后面会每个单独写一篇文章处理,目前的内容已经可以开发一些简单的应用。
开发优势
ImGui界面框架是我使用过开发体验的最好的界面框架,有种在C++中使用Winform开发的感觉,甚至比Winform开发更简单。这里要点名吐槽MFC,如果不是工作所迫我是绝对不会用它的。个人感觉ImGui良好的开发体验主要来自两方面:
- 即时UI模式降低了界面和数据之间的交互难度
- 框架默认的布局、样式降低了开发的心智负担
ImGui目前主要应用在游戏开发中,但我感觉在其它领域也能很好的发挥作用,在C#中也有ImGui.Net可以使用。
附件
源代码:提取码: 59nz