win下控制台程序,怎么在原位置实时更新文本?

狗郎君 2014-01-31 02:22:17
比如说在win 环境下,我要在控制台第一行最右边显示 实时的系统时间,精确到秒,如何才能在原来位置刷新这个时间文本,而不是另起一行呢?

要用到什么API?
...全文
387 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vrace 2014-02-19
  • 打赏
  • 举报
回复
楼主可以试试 curse 库,很有意思的,改颜色,设位置,啥都行
MewX 2014-02-18
  • 打赏
  • 举报
回复
本来C程序控制台是只能行内移动的,也就是'\r'这个字符,然后可以刷新本行。 但是包含了Windows.h之后就不是纯C程序,是Win32 C程序了。
ForestDB 2014-02-04
  • 打赏
  • 举报
回复
在MSDN中搜高级终端IO
Pump天天学习 2014-01-31
  • 打赏
  • 举报
回复
哦,lz控制台中应该还有其他内容吧?那就不能清屏了 可以控制光标位置,在你指定的地方输出 网上复制的例子,顺便贴个链接


#include   <windows.h>   
  #include   <winbase.h>   
  #include   <iostream>   
    
  using   namespace   std;   
    
  int   main()   
  //   光标定位   
  {   
        HANDLE   hStdout;   
        //   光标位置   
        COORD     cursorPos;   
          
        //   标准输出句柄   
        hStdout   =   GetStdHandle(STD_OUTPUT_HANDLE);   
    
        cout   <<   "No1.   lines....\n\n";   
        cout   <<   "No3.   lines....\n\n";   
        cout   <<   "No5.   lines....\n"   <<   endl;   
    
        //   等待2秒   
        Sleep(2000);   
        //   设置光标   
        //   第2行输出   
        cursorPos.X   =   0;   
        cursorPos.Y   =   1;   
        SetConsoleCursorPosition(   hStdout,   cursorPos   );   
        cout   <<   "No2.   lines...."   <<endl;   
    
          
        //   第4行输出   
        cursorPos.X   =   0;   
        cursorPos.Y   =   3;   
        SetConsoleCursorPosition(   hStdout,   cursorPos   );   
        cout   <<   "No4.   lines...."   <<endl;   
    
        //   第6行输出   
        cursorPos.X   =   0;   
        cursorPos.Y   =   5;   
        SetConsoleCursorPosition(   hStdout,   cursorPos   );   
    
        //   释放句柄   
        CloseHandle(hStdout);   
        return   0;   
  }

狗郎君 2014-01-31
  • 打赏
  • 举报
回复
谢了楼上 ,我去调试看看。
Pump天天学习 2014-01-31
  • 打赏
  • 举报
回复

#include <iostream>
#include <iomanip>
#include <Windows.h>
using namespace std;

int main(void )
{
	int minute=0;
	int second =0;
	if(getchar()=='\n')
		{
			while(1)
			{
				
				if(second==60)
				{
					minute+=1;
					second=0;
				}
				cout<<setfill('0')<<setw(2)<<minute<<":"<<setw(2)<<second;
				Sleep(1000);
				system("cls");
				second++;
			}
		}
	return 0;
}

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧