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

Agi*_*ief 3 c windows console

四处寻找执行清除 cmd.exe 控制台窗口的(据称)简单任务的最佳方法,并发现只需使用system('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;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何设置控制台句柄以传递给“cls”函数?

编辑:请注意,我不想按照此问题的大多数答案中的建议通过调用来简单地调用cls或clear调用命令。system

Ant*_*nko 7

如果标准输出未重定向,则使用GetStdHandle获取控制台句柄。(STD_OUTPUT_HANDLE)如果可以重定向,但您仍想清除真实控制台,请CONOUT$使用函数打开伪文件CreateFile。

  • 请注意,为了完整性,它应该是“HANDLE cons”而不是“HWND cons”。为了未来读者的利益,还值得一提的是,原始代码不起作用的原因是它将窗口句柄传递给需要控制台句柄的函数,而它们根本不是同一件事。 (2认同)
  • 虽然这个问题特别要求一个 Windows 解决方案(答案满足了这个要求),但为了完整性,我添加了这个: UNIX/POSIX 解决方案是简单地写 `"\033[2J\033[3J\n\0"`到`stdout`(文件句柄#1),如[`clear(1)`的手册页](http://man7.org/linux/man-pages/man1/clear.1.html)中所述 (2认同)