有时候程序在运行当中,不允许别的程序或人为的关闭计算机,除非应用程序知道windows将要退出,其实这样很简单,我们都知道系统将要关闭时,会向每一个程序发送WM_QUERYENDSESSION这条关机消息,只要我们的程序接受到此消息后,做恰当的处理即刻完成我们所需要的。
处理windows消息有好几种,在这里我们利用Application的OnMessage事件,建立响应该事件的过程即可!如下面的例子:unit unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm1 = class(TForm) private { Private declarations } public procedure AppMessageHandler(var Msg:TMsg; var Handled:Boolean);//声明系统处理消息过程,响应Application的OnMessage事件的过程必须为TMessageEvent类型; { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.AppMessageHandler(var Msg:TMsg; var Handled:Boolean);begin if Msg.message=WM_QueryEndSession then//如果收到的消息为关闭计算机的消息时,进行特别处理,因为只是一个例子,我只写出弹出对话框,大家可以根据自己程序的需要进行响应的处理; begin if messagedlg(''''shutdown?'''',mtconfirmation,mbyesnocancel,0)= mryes then Handled:=true else Handled:=false; end;end;end.最后在程序的DPR文件中,创建窗体之后但在调用Application.Run前加入Application.OnMessage:=Form1.AppMessageHandler;即可!
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18