我正在编写一个在屏幕上显示一些信息的程序,我希望它们每秒更新一次。
我应该怎么办?
我正在 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
}
因此,如果您想更改一小段文本,则无需清除和刷新所有控制台。
附言。不要忘记释放句柄:
CloseHandle(hConsole);
Since you are on Windows, you can do this:
system('cls')
Here is an example:
#include <stdio.h>
int main()
{
printf("Press enter to clear the screen.");
getchar();
system("cls");
return 0;
}
If you are developing using Microsoft Visual Studoo and you want to programmatically clear the screen by outputting spaces, see Example 2 at http://msdn.microsoft.com/en-us/library/windows/desktop/ms682022(v=vs.85).aspx.
If you had searched google before asking, you would have found this link right in the first results.
To quote one of the methods:
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 ); }
有一篇关于该主题的知识库文章。
两种选择:
编写一个以编程方式清除屏幕的函数
/* 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");