如何在Windows本机C ++控制台应用程序中覆盖控制台的同一部分,而不使用第三方库? [英] How can I overwrite the same portion of the console in a Windows native C++ console app, without using a 3rd Party library?

查看:127
本文介绍了如何在Windows本机C ++控制台应用程序中覆盖控制台的同一部分,而不使用第三方库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序需要显示项目的状态,而不是像疯了我的文字滚动我宁愿看到当前状态保持显示在同一行。为了示例:

I have a console app that needs to display the state of items, but rather than having text scroll by like mad I'd rather see the current status keep showing up on the same lines. For the sake of example:


正在运行... nn%complete

缓冲区大小:bbbb字节

输出,其中'nn'是当前百分比完成,'bbbb'是缓冲区大小,在控制台的相同行上定期更新。

should be the output, where 'nn' is the current percentage complete, and 'bbbb' is a buffer size, updated periodically on the same lines of the console.

第一种方法在打印新状态之前,简单地打印了正确数量的后空间到控制台,但这有一个令人讨厌的闪烁,我想摆脱。我还想坚持标准库或MS提供的功能(VC 8),以免为这一个简单的需求引入另一个依赖。

The first approach I took simply printed the correct number of backspaces to the console before printing the new state, but this has an obnoxious flicker that I want to get rid of. I also want to stick to either standard library or MS-provided functionality (VC 8) so as not to introduce another dependency for this one simple need.

推荐答案

您可以使用 SetConsoleCursorPosition 。您需要调用 GetStdHandle 来获取输出缓冲区的句柄。

You can use SetConsoleCursorPosition. You'll need to call GetStdHandle to get a handle to the output buffer.

这篇关于如何在Windows本机C ++控制台应用程序中覆盖控制台的同一部分,而不使用第三方库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆