AD
AD

防止关闭windows

时间:2007-12-26 23:05:54  来源:  作者:
AD

    有时候/doc/">程序在运行当中,不允许别的/doc/">程序或人为的关闭计算机,除非应用/doc/">程序知道windows将要退出,其实这样很简单,我们都知道系统将要关闭时,会向每一个/doc/">程序发送WM_QUERYENDSESSION这条关机消息,只要我们的/doc/">程序接受到此消息后,做恰当的处理即刻完成我们所需要的。


    处理windows消息有好几种,在这里我们利用Application的OnMessage事件,建立响应该事件的过程即可!如下面的例子:
unit unit1;
interface
uses
  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 }
字串4

  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AppMessageHandler(var Msg:TMsg; var Handled:Boolean);
begin
    if Msg.message=WM_QueryEndSession then//如果收到的消息为关闭计算机的消息时,进行特别处理,因为只是一个例子,我只写出弹出对话框,大家可以根据自己/doc/">程序的需要进行响应的处理;
       begin
         if messagedlg('shutdown?',mtconfirmation,mbyesnocancel,0)= mryes then
            Handled:=true
         else
            Handled:=false;
       end;
end;
end.
最后在/doc/">程序的DPR文件中,创建窗体之后但在调用Application.Run前加入
Application.OnMessage:=Form1.AppMessageHandler;即可!
字串7


文章评论

共有 0位编程爱好者发表了评论 查看完整内容

    评论加载中…
忒好程序员:www.teihao.com

推荐信息

     
忒好程序员
AD