SetConsoleCtrlHandler
GenerateConsoleCtrlEvent
SetConsoleMode
ReadConsole
WriteConsole
SetConsoleCP
SetConsoleOutputCP
GetConsoleScreenBufferInfo
SetConsoleTextAttribute

MSDN上有详细说明,并有其它相关函数可供参考

Console主程序执行前加一句
SetConsoleCtrlHandler(@Handler, True);
就能判断出对Console程序外部传来的命令比如“Ctrl+C”,自己可根据信息采取相应的处置。

function Handler(dwCtrlType: DWORD): BOOL; stdcall;
begin
Result := True;
case dwCtrlType of
CTRL_C_EVENT: ShowMessage('Ctrl-C');
CTRL_BREAK_EVENT: ShowMessage('Break');
CTRL_CLOSE_EVENT: ShowMessage('Close');
CTRL_LOGOFF_EVENT: ShowMessage('Logoff');
CTRL_SHUTDOWN_EVENT: ShowMessage('Shutdown');
else MessageBeep(0);
end;
end;

http://bbs.2ccc.com/topic.asp?topicid=506402

04-14 17:33