C++:如何检查我的窗口是否即将关闭?

发布于 2025-01-07 15:34:39 字数 272 浏览 0 评论 0原文

我正在尝试制作一个基于 Win32/*nix 控制台的 ASCII 游戏。我不想使用任何非标准 C++ 或 *nix/windows(.h) 上的库。

我希望它的结构像游戏循环一样。又名:

while (!WIN_CLOSE_FUNCTION()) {
  //Do crap
}
//Do other shutdown crap
return 0;

谁能告诉我这是什么功能?如果它与平台相关,请给我一个 Windows 和 *nix 上的示例。

I'm trying to make a Win32/*nix console-based ASCII game. I want to use no libraries whatsoever that aren't standard C++ or on *nix/windows(.h).

I want it to be structured like a game loop. Aka:

while (!WIN_CLOSE_FUNCTION()) {
  //Do crap
}
//Do other shutdown crap
return 0;

Can anyone point me to what function this would be? If it is platform dependent, give me one example on Windows and *nix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下慵懒的猫 2025-01-14 15:34:39

对于Unix/Linux控制台,没有这样的功能。您能做的最接近的事情是捕获丢失终端时发送的信号SIGHUP。但是请注意,您可以在信号处理程序中执行的操作非常有限。可能最接近您的循环的是(注意:未经测试的代码):

#include <signal.h>

volatile sig_atomic_t hupflag = 0;

extern "C" void hangup(int)
{
  hupflag = 1;
}

int main()
{
  sigaction act;
  act.sa_handler = hangup;
  act.sa_mask = 0;
  act.sa_flags = 0;
  if (sigaction(SIGHUP, &act, 0) < 0)
  {
    std::cerr << "could not install signal handler\n";
    return 1;
  }

  while (!hupflag)
  {
    // ...
  }
  // shutdown
  return 0;
}

For the Unix/Linux console, there is no such function. The closest you can do is to catch the signal SIGHUP which is sent when losing the terminal. However be aware that the things you can do in a signal handler are quite limited. Probably the closest to your loop would be (note: untested code):

#include <signal.h>

volatile sig_atomic_t hupflag = 0;

extern "C" void hangup(int)
{
  hupflag = 1;
}

int main()
{
  sigaction act;
  act.sa_handler = hangup;
  act.sa_mask = 0;
  act.sa_flags = 0;
  if (sigaction(SIGHUP, &act, 0) < 0)
  {
    std::cerr << "could not install signal handler\n";
    return 1;
  }

  while (!hupflag)
  {
    // ...
  }
  // shutdown
  return 0;
}
你的心境我的脸 2025-01-14 15:34:39

类似的问题可能会帮助您 关闭 c++ 控制台应用程序时会发生什么< /a>

接受的答案是:
使用顶角的“x”关闭 C++ 控制台应用程序会引发 CTRL_CLOSE_EVENT,如果使用 SetConsoleCtrlHandler 函数设置控制处理程序,则可以捕获并处理该事件。

有用的链接:

在 *nix 上:

在 Linux 和其他 Unix 系统上,控制台作为单独的进程运行。当您关闭 shell 时,它会向当前活动的进程或不在后台执行的进程发送 SIGHUP 信号。如果程序员不处理它,进程就会终止。如果您使用终端和活动进程关闭 SSH 会话,也会发送相同的信号。

@Zyx 在上面链接的问题中提供的答案

Similar question that might help you What happens when you close a c++ console application

The accepted answer is:
Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function.

Useful links:

On *nix:

On Linux and other Unix systems, the console runs as a separate process. As you close the shell, it sends the SIGHUP signal to the currently active process or processes that are not executed in the background. If the programmer does not handle it, the process simply terminates. The same signal is sent if you close the SSH session with a terminal and an active process.

answer provided by @Zyx in the question linked above

瑾兮 2025-01-14 15:34:39

本身没有这样的功能,但 Unix 和 Windows 都会发送
向所有信号发送信号(Unix 下为 SIGHUP,Windows 下为 SIGBREAK
进程组中的进程,当该进程所在的窗口
组取决于已关闭。所以你所要做的就是捕捉信号并
设置一个标志,您在循环中测试它:

#ifdef _WIN32
int const sigClosed = SIGBREAK;
#else
int const sigClosed = SIGHUP;
#endif

volatile sig_atomic_t windowClosed = 0;

void signalHandler( int )
{
    windowClosed = 1;
}

//  ...
signal( sigClosed, signalHandler );
while ( windowClosed == 0 ) {
    //  ...
}

如果您在循环中从控制台进行任何输入,您将获得
做好输入失败的准备(无论如何你都应该这样做)。

There isn't such a function per se, but both Unix and Windows will send
a signal (SIGHUP under Unix, SIGBREAK under Windows) to all
processes in the process group when the window on which the process
group depends is closed. So all you have to do is catch the signal and
set a flag, which you test in the loop:

#ifdef _WIN32
int const sigClosed = SIGBREAK;
#else
int const sigClosed = SIGHUP;
#endif

volatile sig_atomic_t windowClosed = 0;

void signalHandler( int )
{
    windowClosed = 1;
}

//  ...
signal( sigClosed, signalHandler );
while ( windowClosed == 0 ) {
    //  ...
}

If you're doing any input from the console in the loop, you'll have the
be prepared for the input to fail (which you should be anyway).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文