📄 setconsolectrlhandler.cpp
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -