码迷,mamicode.com
首页 > 编程语言 > 详细

C++ Win32控制台应用程序捕捉关闭事件

时间:2017-05-29 19:53:22      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:logo   style   off   控制台应用程序   error:   logs   could not   bsp   void   

C++ Win32控制台应用程序捕捉关闭事件  

 
#include
#include
bool ctrlhandler( DWORD fdwctrltype )
{
    switch( fdwctrltype )
    {
    // handle the ctrl-c signal.
    case CTRL_C_EVENT:
        printf( "ctrl-c event\n\n" );
        return( true );
    // ctrl-close: confirm that the user wants to exit.
    case CTRL_CLOSE_EVENT:
        printf( "ctrl-close event\n\n" );
        return( true );
    // pass other signals to the next handler.
    case CTRL_BREAK_EVENT:
        printf( "ctrl-break event\n\n" );
        return false;
    case CTRL_LOGOFF_EVENT:
        printf( "ctrl-logoff event\n\n" );
        return false;
    case CTRL_SHUTDOWN_EVENT:
        printf( "ctrl-shutdown event\n\n" );
        return false;
    default:
        return false;
    }
}
void main( void )
{
    if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) ctrlhandler, true ) )
    {
        printf( "\nthe control handler is installed.\n" );
        printf( "\n -- now try pressing ctrl+c or ctrl+break, or" );
        printf( "\n try logging off or closing the console...\n" );
        printf( "\n(...waiting in a loop for events...)\n\n" );
       while( 1 ){ Sleep(100);}
    }
else
    printf( "\nerror: could not set control handler");
}

 

C++ Win32控制台应用程序捕捉关闭事件

标签:logo   style   off   控制台应用程序   error:   logs   could not   bsp   void   

原文地址:http://www.cnblogs.com/aliblogs/p/6918758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!