如何对console程序的关闭进行控制

zh237 2003-09-16 11:08:31
一个console程序,当按下窗口的关闭按键时,不想退出而是进行自己的处理,应如何进行。敬请指教。
...全文
276 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zh237 2003-09-17
  • 打赏
  • 举报
回复
使用这种方法的话,对于事件CTRL_CLOSE_EVENT,系统只给你5秒的时间进行处理,如果没有在5秒内自己调用exitprocess()退出,系统会弹出是否要强制关闭的对话框,以上还是在处理完后返回true的情况。如果返回false的话,系统会进入默认的处理自动调用exitprocess退出的。
我想要的是能够不弹出是否要强制关闭的对话框的处理方法。
xhzhang6 2003-09-17
  • 打赏
  • 举报
回复

同意masterz()

另外可参考
http://www.programfan.net/showarticle.asp?id=2287
控制台程序的事件处理
masterz 2003-09-16
  • 打赏
  • 举报
回复
Registering a Control Handler Function

This is an example of the SetConsoleCtrlHandler function that is used to install a control handler.


When a CTRL+C signal is received, the control handler returns TRUE, indicating that it has handled the signal. Doing this prevents other control handlers from being called.

When a CTRL_CLOSE_EVENT signal is received, the control handler returns TRUE, causing the system to display a dialog box that gives the user the choice of terminating the process and closing the console or allowing the process to continue execution. If the user chooses not to terminate the process, the system closes the console when the process finally terminates.

When a CTRL+BREAK, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT signal is received, the control handler returns FALSE. Doing this causes the signal to be passed to the next control handler function. If no other control handlers have been registered or none of the registered handlers returns TRUE, the default handler will be used, resulting in the process being terminated.

Note that MyErrorExit is a placeholder for an application-defined function to display and handle error conditions.

BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL+C signal.

case CTRL_C_EVENT:

Beep(1000, 1000);
return TRUE;

// CTRL+CLOSE: confirm that the user wants to exit.

case CTRL_CLOSE_EVENT:

return TRUE;

// Pass other signals to the next handler.

case CTRL_BREAK_EVENT:

case CTRL_LOGOFF_EVENT:

case CTRL_SHUTDOWN_EVENT:

default:

return FALSE;
}
}

void main(void)
{
BOOL fSuccess;

fSuccess = SetConsoleCtrlHandler(
(PHANDLER_ROUTINE) CtrlHandler, // handler function
TRUE); // add to list
if (! fSuccess)
MyErrorExit("Could not set control handler");
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧