setconsolectrlhandler.cpp

来自「验证进程同步的信号量方法的C程序」· C++ 代码 · 共 53 行

CPP
53
字号
#include <stdio.h> 
#include <windows.h> 
#include <iostream.h>
#include <winbase.h> 

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: 
			cout << "CTRL_CLOSE_EVENT" << endl;
 
            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
	cout << "Process starts." << endl;
	Sleep(1000);
	GenerateConsoleCtrlEvent(CTRL_C_EVENT, NULL);
	Sleep(1000);
    if (! fSuccess) 
        cout << "Could not set control handler" << endl;
	cout << "Process ends." << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?