C lambda/回调弹出窗口
C++ lambda/callback popup?
我有一个这样做的弹出系统(GUI):
// creates popup with two possible answer-buttons
if (timeToEat())
callPopup(ID_4, "What to eat?", "Cake", "Cookies!");
//elsewhere in code i check for key presses
if (popupAnswered(ID_4,0)) // clicked first button in popup
eatCake();
if (popupAnswered(ID_4,1)) // clicked second button in popup
eatCookiesDamnit();
我可以使用某种lambda/回调来安排像以下内容一样。因此,将函数"保留",并在按下按钮时可以激活(返回值)。
谢谢!
if (timeToEat())
callPopup("What to eat?", "Cake", "Cookies!"){
<return was 0 :> eatCake(); break;
<return was 1 :> eatCookies(); break;
}
您可以将持续参数添加到callPopup
:
void callPopup(int id, std::function<void(int)> f)
{
if (something)
f(0);
else
f(1);
}
//...
callPopup(ID_4, [](int x) { if (x == 0) eatCake(); });
,也可以添加另一个功能层并使用返回值:
std::function<void(std::function<void(int)>)>
callPopup(int id)
{
return [](std::function<void(int)> f) { f(something ? 0 : 1); }
}
// ...
callPopup(ID_4)([](int x) { if (x == 0) ... ;});
// or
void popupHandler(int);
auto popupResult = callPopup(ID_4);
// ...
popupResult(popupHandler);
您可以将选择与操作相关联,然后执行与单击
关联的操作using PopupActions = std::map<std::string, std::function<void()>>;
void callPopup(int id, std::string prompt, PopupActions actions)
{
for (auto & pair : actions)
// create button with text from pair.first and on-click from pair.second
// show buttons
}
if (timeToEat())
callPopup(ID_4, "What to eat?", {
{ "Cake!", [this]{ eatCake(); } }
{ "Cookies!", [this]{ eatCookies(); } }
});
}
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 鼠标离开 GLFW窗口后触发 GLFW 鼠标回调?
- 全局挂钩 DLL 仅在 C# 主窗口处于活动状态/前台时调用 C# 回调方法
- 在窗口执行目标 TLS 回调之前注入 dll
- C lambda/回调弹出窗口
- SDL视频显示窗口:如何连接窗口关闭事件回调
- 在QtWebkit中,如何安装从C++到Javaobject窗口的回调
- 窗口进程/回调函数可以是类的成员函数吗?
- 如何在执行回调时隐藏顶级GTK窗口
- WNDPROC窗口回调过程中接收到的句柄与CreateWindow返回的句柄之间的差异
- C++,Qt,QtWebKit:如何创建一个html呈现窗口,以便您的应用程序从JS调用中获得回调
- C++,win32api:如何创建一个html呈现窗口,以便您的应用程序从JS调用中获得回调
- Windows:当我从本机代码回调到托管代码时,将显示“仅消息”窗口
- opencv窗口在鼠标回调时未刷新
- 关于窗口中的 TLS 回调
- 窗口事件回调函数C++的问题