我有一个在NCurses界面中从左到右滚动文本的功能,但它每500毫秒左右依赖一个计时器来正确写出前面的新字符并删除后面的前一个字符。这是代码:
void GekkoFyre::TuiHangouts::gui_scrollText(WINDOW *display, const char *msg,
const int &msgPosY)
{
size_t maxX = getmaxx(display);
size_t msgLen = strlen(msg);
short origPosY = 0;
short origPosX = 0;
getyx(display, origPosY, origPosX); // Obtain the original cursor position
if (msgLen > maxX) {
// size_t diff = (msgLen - maxX);
size_t i = 0;
for (i = maxX; i < msgLen; ++i) {
// Scroll the message from left to right, then vice versa, so that it fits within the
// designated window.
move(msgPosY, 0);
clrtoeol(); // Delete just the given line, <http://stackoverflow.com/questions/5072881/how-to-clear-a-specific-line-with-ncurses>
move(origPosY, origPosX); // Go back to the original cursor position
std::string tmp((msgLen - i), msg[(msgLen - i)]);
wattron(display, A_REVERSE); // Highlight selection
mvwaddstr(display, msgPosY, 0, tmp.c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
}
老实说,我不知道这是否有效,因为我无法让它发挥作用。但是有一个像std::this_thread::sleep_for(std::chrono::milliseconds(500));
一样的计时器,你可以在一个函数中使用但是是异步的吗?那会很漂亮!我做了一些研究,遇到了std :: async和std :: thread,但它们依赖于函数的外部。
非常感谢任何帮助,谢谢。
答案 0 :(得分:1)
但是有一个类似于
std::this_thread::sleep_for(std::chrono::milliseconds(500));
的定时器,你可以在一个函数中使用但是是异步的吗?
不,你不能。您从术语异步行为中实际掌握了什么。您需要在代码中的其他位置加入一个连接点来捕获计时器已用事件。