怎样让程序在WINDOWS关闭时,悄然关闭(50分)

  • 主题发起人 主题发起人 光子
  • 开始时间 开始时间
定义一个消息,截获WM_QUERYENDSESSION,然后进行处理。看一下代码:<br>////////////////////////////////////<br>//一个在系统关机时检测光驱中是否有光盘<br>//的程序。如有则弹出光驱。5秒后系统关闭<br>//<br>////////////////////////////////////<br>unit Main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,mmSystem,<br>&nbsp; ExtCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure CMEraseBkgnd(var Message:TWMEraseBkgnd);Message WM_ERASEBKGND;<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br>function DiskInDrive(const Drive: char): Boolean;stdcall;external 'DevWolf.DLL'<br>function DiskIsCDROM:Pchar;stdcall external 'DevWolf.Dll'<br><br>procedure TForm1.WMQueryEndSession(var Message: TWMQueryEndSession);<br>var<br>&nbsp; WaitPanel,bkPanel: TPanel;<br>&nbsp; i:integer;<br>&nbsp; Drive:Pchar;<br>begin<br>&nbsp; inherited;<br>&nbsp; i:=5;<br>&nbsp; Drive:=DiskIsCDROM;<br>&nbsp; copy(drive,0,1);<br>&nbsp; if Drive&lt;&gt;'0' then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;if DiskInDrive(Drive^) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; WaitPanel:=TPanel.create(Form1);<br>&nbsp; &nbsp; &nbsp; &nbsp; bkPanel:=Tpanel.Create(form1);<br>&nbsp; &nbsp; &nbsp; &nbsp; with BKPanel do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Parent:=Form1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Left:=5;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Top:=5;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Width:=200;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Height:=50;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color:=clBlack;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ctl3d:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visible:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;show;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; with WaitPanel do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Parent:=Form1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Left:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Top:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Width:=200;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Height:=50;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Font.Size:=11;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Font.Name:='宋体';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;visible:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;form1.Show;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;show;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mciSendString('Set cdaudio door open wait', nil, 0, handle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while i&gt;=0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WaitPanel.Caption:='光盘关闭的时间还余'+Inttostr(i)+'秒';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i:=i-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(1000);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Form1.Update;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mciSendString('Set cdaudio door closed wait', nil, 0, handle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;Showmessage('你的电脑没有安装光驱');<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.CMEraseBkgnd(var Message: TWMEraseBkgnd);<br>begin<br>brush.style:=bsClear;<br>Inherited;<br>end;<br><br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>Form1.Hide;<br>Timer1.Enabled:=False;<br>end;<br>end.
 
谢谢各位,我已经成功了,是使用ics控件实现的,其实就是TNMUDP出问题,其它的都没问题.
 
多人接受答案了。
 
[red][?]我可以在系统关闭时,怎么检测光驱内有无光盘,并发出警告。<br>我该如何做?<br>按"only you "的做法是不是完全可行[/red]
 
only you :<br>请教一下:<br>function DiskInDrive(const Drive: char): Boolean;stdcall;external 'DevWolf.DLL'<br>中的external'DevWolf.dll'是做什么用的?<br>我直接用你的代码不知道为什么用不起来。<br>我还要添加什么控件?<br>我是初学者。请赐教!!<br>
 
function DiskInDrive(const Drive: char): Boolean;stdcall;external 'DevWolf.DLL'<br>那是我写的动态连接库,不过我现在找不着了!
 
我定义了:<br>procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;<br>&nbsp; &nbsp; 在程序中也写了<br>procedure TForm1.WMQueryEndSession(var Message: TWMQueryEndSession);<br>begin<br>//*******<br>end;<br>但一运行,下面的程序都报错好像讲不匹配。为什么
 
后退
顶部