2012-05-16 78 views
-2

我正在编写一个程序,在屏幕上显示一些信息,我希望它们每秒更新一次。如何更新(刷新)控制台屏幕?

我该怎么办?

我在Windows上编写C控制台应用程序。

+0

我不确定清楚的控制台方法,但可以尝试检测控制台的高度并添加适量的换行符。 –

回答

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

两个选项:

  1. 写,这将编程清除屏幕

    /* 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; 
    } 
    
  2. 使用的系统函数的函数(不是很漂亮;需要启动一个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);