while (!done)
{
// Poll and handle messages (inputs, window resize, etc.)
// See the WndProc() function below for our to dispatch events to the Win32 backend.
MSG msg;
while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if (msg.message == WM_QUIT)
done = true;
}
if (done)
break;
// Handle window being minimized or screen locked
if (g_SwapChainOccluded && g_pSwapChain->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED)
{
::Sleep(10);
continue;
}
g_SwapChainOccluded = false;
// Handle window resize (we don't resize directly in the WM_SIZE handler)
if (g_ResizeWidth != 0 && g_ResizeHeight != 0)
{
CleanupRenderTarget();
g_pSwapChain->ResizeBuffers(0, g_ResizeWidth, g_ResizeHeight, DXGI_FORMAT_UNKNOWN, 0);
g_ResizeWidth = g_ResizeHeight = 0;
CreateRenderTarget();
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
//自己创建imgui窗口1
int flags = ImGuiWindowFlags_HorizontalScrollbar;//用于控制窗口行为的标志 如:不能移动,不能缩小等操作
if (open)
{
ImGui::SetNextWindowPos(ImVec2(0,0), ImGuiCond_FirstUseEver); //设置下一个即将创建的窗口的位置的函数 三个参数,1.窗口位置 2.控制何时应用该位置 (存在很多种标志可选)3.用于窗口的对齐方式(范围 [0,1],可选)
ImGui::SetNextWindowSize(ImVec2(500, 500), ImGuiCond_FirstUseEver);//设置下一个即将创建的窗口的大小的函数
ImGui::Begin("Hello imgui", &open, flags); //开始绘制一个 ImGui 窗口,可以传入多个参数来自定义窗口行为,比如标题、可见性、窗口标志等。
//按钮
if (ImGui::Button(u8"关闭软件")) //按钮
{
//按钮点击触发事件
ExitProcess(0); //关闭软件 结束进程
}
//复选框
static bool v = false; //用静态变量 这样做的目的是在每次 ImGui 渲染帧时,变量 v 的值不会被重置
static bool v1 = false; //第二个复选框标志位
if (ImGui::Checkbox(u8"启动功能", &v))// 参数v=真(选中) 假(未选中)
{
if (v == true)
{
//复选框选中触发事件
MessageBox(NULL, L"选中复选框", L"提示", MB_OK);
}
else
{
MessageBox(NULL, L"未选中复选框", L"提示", MB_OK);
}
}
//第二个复选框
if (ImGui::Checkbox(u8"启动功能1",&v1))
{
if (v1==true)
{
MessageBox(NULL, L"选中复选框", L"提示", MB_OK);
}
else
{
MessageBox(NULL, L"未选中复选框", L"提示", MB_OK);
}
}
//单选框
static int a{};//几个单选框为一组 一组只能选中一个单选框 返回单选框当前选择值 当前选择值与单选框第三个参数一致
static int b{};//第二组单选框
//第一组单选框
ImGui::RadioButton(u8"单选框1",&a ,1 );
ImGui::RadioButton(u8"单选框2", &a, 2);
ImGui::RadioButton(u8"单选框3", &a, 3);
//第二组单选框
ImGui::RadioButton(u8"单选框4", &b, 4);
ImGui::RadioButton(u8"单选框5", &b, 5);
ImGui::RadioButton(u8"单选框6", &b, 6);
//利用switch循环 第一个选中单选框返回值为1以此类推 来进行触发事件
switch (a)
{
case 1:
//触发单选框1的事件
ImGui::Text(u8"选中:%d", a);
break;
case 2:
//触发单选框2的事件
ImGui::Text(u8"选中:%d", a);
break;
}
switch (b)
{
case 4:
//触发单选框4的事件
ImGui::Text(u8"选中:%d", b);
break;
case 5:
//触发单选框5的事件
ImGui::Text(u8"选中:%d", b);
break;
}
//标签
ImGui::TextWrapped(u8"TextWrapped----------------------------------------------------------------"); //标签内容超过窗口大小自动换行
ImGui::TextColored(ImVec4(0.45f, 0.55f, 0.60f, 1.00f), u8"TextColored"); //可以修改标签内容颜色
ImGui::BulletText("BulletText");//标签内容前会有一个圆点符号
HelpMarker("HelpMarker"); //鼠标放置上去显示内容
//编辑框
static char str[MAXCHAR] = ""; //创建一个数组把编辑框内容存入进去
static char str1[MAXCHAR] = "";
ImGui::InputText(u8"账号", str, MAXCHAR);
ImGui::InputText(u8"密码", str1, MAXCHAR);//第四个参数放入ImGuiInputTextFlags_Password 让密码显示*号
//编辑框框中的账号密码保存到本地文本文档中
if (ImGui::Button(u8"保存账号密码")) //创建一个按钮 点击保存账号密码信息
{
ofstream ofs; //创建文件流对象
ofs.open("tset.txt", ios::out); //打开文件
ofs << "账号:" << str << endl;
ofs << "密码:" << str1 << endl;
ofs.close();//关闭文件
}
//读取本地账号密码放入编辑框中
if (ImGui::Button(u8"读取账号密码"))
{
ifstream ifs;//创建文件流对象
ifs.open("tset.txt", ios::in);
if (!ifs.is_open()) //判断文件是否打开成功
{
return 0;
}
else
{
//读取数据
std::string line;
// 读取第一行(账号)
if (getline(ifs, line))
{
if (line.find("账号:") == 0)
{
// 去掉“账号:”前缀,拷贝到 str
strncpy_s(str, MAXCHAR, line.c_str() + 5, MAXCHAR - 1);
str[MAXCHAR - 1] = '\0'; // 确保字符串结尾
}
}
// 读取第二行(密码)
if (getline(ifs, line))
{
if (line.find("密码:") == 0)
{
// 去掉“密码:”前缀,拷贝到 str1
strncpy_s(str1, MAXCHAR, line.c_str() + 5, MAXCHAR - 1);
str1[MAXCHAR - 1] = '\0';
}
}
ifs.close();
}
}
//模拟登录状态 进度条会自动增加
static int SliderInt_buf = 0; // 进度条值,0~100
static bool is_logging_in = false; // 是否正在登录
static bool login_success = false; // 是否登录成功
if (ImGui::Button(u8"登录")) //按钮
{
//按钮点击触发事件
is_logging_in = true;
SliderInt_buf = 0; // 重置进度条为0
}
//进度条
ImGui::SliderInt(u8"进度条", &SliderInt_buf,0,10,u8"当前进度%d" );
//登录进度条增加
if (is_logging_in && SliderInt_buf < 10) {
SliderInt_buf += 1;
Sleep(30); // 控制进度速度(30ms/步,总共约3秒)
}
else if (SliderInt_buf >= 10) {
is_logging_in = false;
login_success = true;
}
// 登录状态提示
if (login_success) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), u8"登录成功!");
}
else if (is_logging_in) {
ImGui::Text(u8"正在登录,请稍候...");
}
else {
ImGui::Text(u8"点击登录按钮开始登录");
}
//下拉框
static int current_buf;//缓冲区 存储下拉框索引
static const char* name[3] = { u8"地图1",u8"地图2" ,u8"地图3" }; //下拉框中内容
//IM_ARRAYSIZE(name):宏,用于自动计算数组长度;
ImGui::Combo(u8"选择地图", ¤t_buf, name, IM_ARRAYSIZE(name));
switch (current_buf)
{
case 0:
//选择下拉框第一个事件
MessageBox(NULL,L"确认",L"提示", MB_OK);
break;
case 1:
//选择下拉框第二个事件
MessageBox(NULL, L"确认1", L"提示", MB_OK);
break;
case 2:
//选择下拉框第san个事件
MessageBox(NULL, L"确认2", L"提示", MB_OK);
break;
}
//ImGui::SetCursorPos(ImVec2(100, 200)); //自定义控件位置
ImGui::Text("hello"); //在窗口中写入一个标签
//ImGui::SameLine(); //将上下两个标签内容设置同行显示
ImGui::Text(u8"你好,ImGui"); //ImGui 的文本显示是基于 UTF-8 编码
ImGui::End(); //结束当前窗口的绘制。
}下拉框弹窗死循环
最新发布