如何在C语言中不使用system()函数来清除win32 cmd控制台窗口?

3
看了一下如何执行(据说)简单的任务:清除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;
}

我的问题是:如何设置控制台句柄以传递给`cls`函数?
编辑:请注意,我不希望仅通过调用system来调用/调用cls或clear命令,这是this question中大多数答案建议的方法。

1
也许如果您告诉我们您遇到的编译器错误... - user529758
1
例如,AllocConsole。然而,普遍存在一个误解,即编程主要是通过在互联网上找到片段并将它们拼凑在一起来完成的,而不知道自己在做什么。抱歉给你带来坏消息,这样行不通。 - IInspectable
清单编译正常,但我收到以下错误信息,对我来说毫无意义:“cls.c:第59行GetConsoleScreenBufferInfo出错;cls.c:第66行FillConsoleOutputCharacter出错;cls.c:第71行ConsoleScreenBufferInfo出错;cls.c:第77行FillConsoleOutputAttribute出错;cls.c:第82行SetConsoleCursorPosition出错”。我的C编程技能(或缺乏技能)处于初学者水平,所以大部分内容都超出了我的理解范围。我只想要一个简单的程序来清除控制台屏幕。 - Agi Hammerthief
@IInspectable 我不是编程新手,已经有三年的PHP和Drupal经验。然而,我对C语言还很陌生,所以请您不要居高临下(同时承认,在这种情况下,我确实不知道自己在做什么)。我知道编译器说代码有问题。我不知道的是为什么以及如何进行纠正。更进一步,除了知道错误发生在哪些行和文件中,我甚至不知道输出的含义是什么。 - Agi Hammerthief
好的,这个问题让我在今天大部分时间都束手无策,所以我的情绪并不是很好。 - Agi Hammerthief
显示剩余4条评论
1个回答

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

1
谢谢,先生,这样做就可以了!int main(void) { HWND cons = GetStdHandle(STD_OUTPUT_HANDLE); cls(cons); return 0;} - Agi Hammerthief
每次我查看这个答案(频繁地),我都希望能够点赞它。 - Agi Hammerthief
2
注意为了完整性,应该是HANDLE cons而不是HWND cons。同时,对于未来的读者来说,值得一提的是,原始代码无法工作的原因是它将一个窗口句柄传递给了一个期望控制台句柄的函数,而它们并不是同一个东西。 - Harry Johnston
2
虽然问题特别要求Windows解决方案(答案已经提供了),但是出于完整性考虑,我将此添加进来:UNIX/POSIX的解决方案是在stdout(文件句柄#1)中简单地写入"\033[2J\033[3J\n\0",就像clear(1)的手册页面中提到的那样。 - Agi Hammerthief

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接