-2
A
回答
0
由于您使用的是Windows,你可以这样做:
system('cls')
下面是一个例子:
#include <stdio.h>
int main()
{
printf("Press enter to clear the screen.");
getchar();
system("cls");
return 0;
}
如果您使用Microsoft Visual Studoo发展,要以编程方式清除屏幕通过输出空格,请参见http://msdn.microsoft.com/en-us/library/windows/desktop/ms682022(v=vs.85).aspx处的示例2。
0
如果您在询问之前曾经搜索过谷歌,您会在第一个结果中找到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); }
0
有一个关于该主题的一个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");
2
钢琴家,您可以使用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);
相关问题
- 1. XNA更新和屏幕刷新
- 2. 如何在屏幕分辨率更改后刷新屏幕?
- 3. Firebug的控制台刷新
- 4. Graphics.h刷新屏幕
- 5. Java刷新屏幕
- 6. 如何在刷新刷新后保持屏幕上的任何更改
- 7. AngularJS - 如何刷新控制器更改/刷新JSON调用
- 8. 如何在BlackBerry中刷新屏幕?
- 9. 如何刷新android主屏幕图标?
- 10. 如何从当前屏幕刷新UITableView
- 11. 如何刷新lightswitch中的父屏幕?
- 12. 如何获得屏幕刷新率?
- 13. 如何控制锁定屏幕上的通知更新频率
- 14. UIApplicationExitsOnSuspend和屏幕刷新
- 15. 覆盖屏幕刷新率
- 16. GNU屏幕刷新问题
- 17. reloadData不刷新屏幕
- 18. 无效()不刷新屏幕
- 19. 刷新屏幕从BillingReceiver内
- 20. 更新NUnit控制台
- 21. 刷新控制台日志数据,无需刷新页面
- 22. 刷新控制台中的值
- 23. 刷新助手从Rails的控制台
- 24. Rails 3控制台沙箱刷新?
- 25. AndEngine更新屏幕
- 26. 屏幕未更新
- 27. 重新定位控制台窗口相对于屏幕
- 28. Android:强制屏幕刷新方向更改
- 29. 控制台架构更新和新表
- 30. 如何在刷新时进行全屏幕背景更改?
我不确定清楚的控制台方法,但可以尝试检测控制台的高度并添加适量的换行符。 –