再谈Delphi关机消息拦截 -- 之控制台程序 SetConsoleCtrlHandler(控制台使用回调函数拦截,比较有意思)

这里补充一下第一篇文章中提到的拦截关机消息

Delphi消息拦截:http://blog.csdn.net/cwpoint/archive/2011/04/05/6302314.aspx

 

下面我再介绍一种在控制台程序中拦截关机消息的方法。

使用SetConsoleCtrlHandler函数。经过测试这个函数不能取消操作。

 

[delphi] view plain copy
 
  1. program Project1;  
  2.   
  3. {$APPTYPE CONSOLE}  
  4.   
  5. uses  
  6.   SysUtils,  
  7.   Windows,  
  8.   Messages;  
  9.   
  10. function EndMsg(fdwCtrlType: Cardinal): Boolean; stdcall;  
  11. begin  
  12.   Result := False;  
  13.   case fdwCtrlType of  
  14.     CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT:  
  15.       MessageBox(0, PChar('注销、重启、关机'), PChar(''), 0);  
  16.   end;  
  17. end;  
  18.   
  19. begin  
  20.   
  21.   if not SetConsoleCtrlHandler(@EndMsg, True) then  
  22.     Exit;  
  23.   
  24.   while True do  
  25.     Sleep(1000);  
  26.   
  27. end.  

 

 

http://blog.csdn.net/cwpoint/article/details/6373025

posted @ 2016-09-28 21:35  findumars  Views(611)  Comments(0Edit  收藏  举报