2012-05-16 87 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);