簡體   English   中英

如何在不使用C中的system()的情況下清除win32 cmd控制台窗口?

[英]How do I clear a win32 cmd console window without using system() in C?

環顧四周尋找最好的方法來執行清除cmd.exe控制台窗口的簡單任務(假設),並發現只使用系統('cls'); 是錯誤的 (在“對於每個復雜問題都有一個明確,簡單和錯誤的答案”的行中 )並且發現簡單地將win32代碼復制並粘貼到函數中會導致編譯錯誤,然后我發現了以下函數 ,但不知道我要做什么來調用它可以使用的變量,而不會噴出錯誤信息:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

/* 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__); }

HWND GetConsoleHwnd(void) {
  #define MY_BUFSIZE 1024  // Buffer size for console window titles.
  HWND hwndFound;  // This is what is returned to the caller.
  char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
  char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.

  // Fetch current window title.
  GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
  // Format a "unique" NewWindowTitle.
  wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
  // Change current window title.
  SetConsoleTitle(pszNewWindowTitle);
  // Ensure window title has been updated.
  Sleep(40);

  // Look for NewWindowTitle.
  hwndFound=FindWindow(NULL, pszNewWindowTitle);
  // Restore original window title.
  SetConsoleTitle(pszOldWindowTitle);

  return(hwndFound);
}

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;
}

int main (void) {
  // why does this fail?
  HWND cons = GetConsoleHwnd();
  cls(cons);

  return 0;
}

我的問題是:如何設置控制台句柄以傳遞給`cls`函數?

編輯 :請注意,我不希望通過system調用來調用/調用clsclear命令, 如此問題的大多數答案中所建議的那樣。

如果未重定向標准輸出,請使用GetStdHandle (STD_OUTPUT_HANDLE)獲取控制台句柄。 如果它可以被重定向但你想要清除真正的控制台,請使用CreateFile函數打開CONOUT$偽文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM