10楼已经解释得很清楚了,我就不画蛇添足了
雁无留踪之意,水无取影之心
#include <windows.h> #include <stdio.h> BOOL WINAPI myConsoleHandler(DWORD CEvent); int main() { while(1) if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)myConsoleHandler,true)==false) { printf("unable to install handler!\n"); return -1; } return 0; } BOOL WINAPI myConsoleHandler(DWORD CEvent) { switch(CEvent) { case CTRL_C_EVENT: MessageBox(NULL,"CTRL+C received!","CEvent",MB_OK); break; case CTRL_BREAK_EVENT: MessageBox(NULL,"CTRL+BREAK received!","CEvent",MB_OK); break; } return true; }
#include <stdio.h> #include <stdlib.h> int main() { int c; while ((c = getchar()) != EOF) putchar(c); printf("%d",c); system("pause"); return 0; }
#include <windows.h> #include <stdio.h> const BOOL true = 1; const BOOL false = 0; BOOL WINAPI myConsoleHandler(DWORD CEvent); int main() { while(1) if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)myConsoleHandler,true)==false) { printf("unable to install handler!\n"); return -1; } return 0; } BOOL WINAPI myConsoleHandler(DWORD CEvent) { switch(CEvent) { case CTRL_C_EVENT: MessageBox(NULL,"CTRL+C received!","CEvent",MB_OK); break; case CTRL_BREAK_EVENT: MessageBox(NULL,"CTRL+BREAK received!","CEvent",MB_OK); break; case CTRL_CLOSE_EVENT: MessageBox(NULL,"Program being closed!","CEvent",MB_OK); exit(0); break; } return true; }