要拦截消息的话可以通过SetConsoleCtrlHandler和HandlerRoutine函数(msdn一下),下面是简单例子:
#include
#include
BOOL MyHandler( DWORD dwCtrlType )
{
if ( dwCtrlType == CTRL_CLOSE_EVENT )
{
printf("Cannot close...\n");
return TRUE;
}
return FALSE;
}
void main()
{
SetConsoleCtrlHandler((PHANDLER_ROUTINE)MyHandler, TRUE);
printf("Please try to clsoe...\n");
while(1) {};
}
要禁止关闭按钮的话可以直接从系统菜单里移除,比如:
#define _WIN32_WINNT 0x0500
#include
#include
void main()
{
DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND);
DrawMenuBar(GetConsoleWindow());
printf("Now you cannot close this window...\n");
system("pause");
}