如何捕获操作系统关机的消息?(100分)

  • 主题发起人 主题发起人 meckyhan
  • 开始时间 开始时间
M

meckyhan

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了个程序,只要他在运行,操作系统就关闭不了,该如何解决,我想只要能捕获到<br>操作系统关闭的消息,我自己退出就可以了,这个消息如何捕获,谢谢
 
你可以参考下边的例子,在这个例子中,当系统关机时在WindowsCloseTime.txt文件中写入关键时间。<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp;Dialogs, StdCtrls;<br><br>type<br>&nbsp;TForm1 = class(TForm)<br>&nbsp;private<br>&nbsp; &nbsp;{ Private declarations }<br>&nbsp;public<br>&nbsp; &nbsp;{ Public declarations }<br>&nbsp;protected<br>&nbsp; &nbsp;procedure OnWindowsEixt(var Msg:TWMQUERYENDSESSION);message WM_QUERYENDSESSION;<br>&nbsp;end;<br><br>var<br>&nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.OnWindowsEixt(var Msg:TWMQUERYENDSESSION);<br>var<br>&nbsp;f:TextFile;<br>&nbsp;FileHandle:Integer;<br>begin<br>&nbsp;//将退出时间记录在C:/WindowsCloseTime.txt文件中<br>&nbsp;FileHandle := FileCreate('C:/WindowsCloseTime.txt');<br>&nbsp;FileClose(FileHandle);<br>&nbsp;AssignFile(f,'C:/WindowsCloseTime.txt');<br>&nbsp;Rewrite(f);<br>&nbsp;Write(f,TimeToStr(Now));<br>&nbsp;CloseFile(f);<br>&nbsp;//<br>&nbsp;Msg.Result := 1;<br>end;<br><br>end. &nbsp;<br>
 
你可以在上班例子中的OnWindowsEixt中试着关闭自己的程序。
 
后退
顶部