C ++防止单击红色X时关闭控制台窗口 [英] C++ Prevent the console window from closing when red X is clicked

查看:198
本文介绍了C ++防止单击红色X时关闭控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个不带类&的简单C ++控制台应用程序对象。

是否有任何方法或功能可以防止单击红色X按钮时关闭控制台?

我正在使用Visual Studio C ++ Express 2010:一个简单的控制台应用程序,仅包含main.cpp文件。
感谢您回答我的问题:) !!

I'm developing a simple C++ console application without classes & objects.
Is there any method or function to prevent the console from closing when red X button is clicked ?
I'm using Visual Studio C++ Express 2010 : A simple console application which containes only main.cpp file. Thank you for answering my question :) !!

推荐答案

这对我有用:

#include "conio.h"
void main()
{
    HWND hwnd = GetConsoleWindow();
    HMENU hmenu = GetSystemMenu(hwnd, FALSE);
    EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED);
}

在我们需要的时候,重新启用按钮:

While we're at it, to re-enable the button:

EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED);

...并设置窗口标题:

... and to set the window's title:

char consoleTitle[256];
wsprintf(consoleTitle, _T("My little window"));
SetConsoleTitle((LPCTSTR)consoleTitle);






我看到大多数引用使用 DeleteMenu 而不是 EnableMenuItem 。我倾向于稍后,因为您获得更多控制权(启用/禁用/变灰等)。有关完整选项,请查看MSDN 控制台功能菜单功能


I saw that most references used DeleteMenu and not EnableMenuItem. I preffer the later, as you get more control (enable/disable/greyed-out etc.). For full options, take a look at MSDN Console Functions and Menu Functions

这篇关于C ++防止单击红色X时关闭控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆