【发布时间】:2012-05-16 08:50:44
【问题描述】:
我正在编写一个在屏幕上显示一些信息的程序,我希望它们每秒更新一次。
我该怎么办?
我正在 Windows 上编写一个 C 控制台应用程序。
【问题讨论】:
-
我不确定明确的控制台方法,但您可以尝试检测控制台的高度并添加适当数量的换行符。
我正在编写一个在屏幕上显示一些信息的程序,我希望它们每秒更新一次。
我该怎么办?
我正在 Windows 上编写一个 C 控制台应用程序。
【问题讨论】:
钢琴家,您可以使用 WinAPI 函数在控制台上工作。
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!hConsole)
return;
然后确定光标位置:
CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
GetConsoleScreenBufferInfo(hConsole, &csbi);
COORD coordCur = csbi.dwCursorPosition;
还有……
while (TRUE) { // your cycle goes here
// ...
// now you can change position of the cursor
coordCur.X = newX;
coordCur.Y = newY;
SetConsoleCursorPosition(hConsole, coordCur);
// and print any information from the new position
printf("..."); // old text will be replaced
}
因此,如果您想更改一小段文本,则无需清除和刷新所有控制台。
ps。不要忘记释放句柄:
CloseHandle(hConsole);
【讨论】:
有一个关于该主题的KB article。
两种选择:
编写一个以编程方式清除屏幕的函数
/* Standard error macro for reporting API errors */ #define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \ on line %d\n", __FILE__, GetLastError(), api, __LINE__);} void cls( HANDLE hConsole ) { COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */ BOOL bSuccess; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ DWORD dwConSize; /* number of character cells in the current buffer */ /* get the number of character cells in the current buffer */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); PERR( bSuccess, "GetConsoleScreenBufferInfo" ); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; /* fill the entire screen with blanks */ bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ); PERR( bSuccess, "FillConsoleOutputCharacter" ); /* get the current text attribute */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); PERR( bSuccess, "ConsoleScreenBufferInfo" ); /* now set the buffer's attributes accordingly */ bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ); PERR( bSuccess, "FillConsoleOutputAttribute" ); /* put the cursor at (0, 0) */ bSuccess = SetConsoleCursorPosition( hConsole, coordScreen ); PERR( bSuccess, "SetConsoleCursorPosition" ); return; }使用系统函数(不是很漂亮;需要启动 shell 和命令)
system("cls");
【讨论】:
由于您使用的是 Windows,因此您可以这样做:
system('cls')
这是一个例子:
#include <stdio.h>
int main()
{
printf("Press enter to clear the screen.");
getchar();
system("cls");
return 0;
}
如果您正在使用 Microsoft Visual Studoo 进行开发,并且希望通过输出空格以编程方式清除屏幕,请参阅示例 2,http://msdn.microsoft.com/en-us/library/windows/desktop/ms682022(v=vs.85).aspx。
【讨论】:
如果您在询问之前搜索过 google,您会在第一个结果中找到 this link。
引用其中一种方法:
Windows:
#include <windows.h> void ClearScreen() { HANDLE hStdOut; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD count; DWORD cellCount; COORD homeCoords = { 0, 0 }; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); if (hStdOut == INVALID_HANDLE_VALUE) return; /* Get the number of cells in the current buffer */ if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return; cellCount = csbi.dwSize.X *csbi.dwSize.Y; /* Fill the entire buffer with spaces */ if (!FillConsoleOutputCharacter( hStdOut, (TCHAR) ' ', cellCount, homeCoords, &count )) return; /* Fill the entire buffer with the current colors and attributes */ if (!FillConsoleOutputAttribute( hStdOut, csbi.wAttributes, cellCount, homeCoords, &count )) return; /* Move the cursor home */ SetConsoleCursorPosition( hStdOut, homeCoords ); }
【讨论】: