Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Where is CanClose?

Posted on 2004-09-28
6
Medium Priority
?
1,157 Views
Last Modified: 2008-03-17
I want to prevent the user from closing the application.
When windows closes I want the application to do some tasks then close and then windows close.

In Form1.FormCloseQuery is CanClose := false; // user cannot close the app.

Form1.OnWindowsShutDown;
begin
 DoTasks;

 Form1.CanClose := true;  // No such variable.
 Application.CanClose := true;  // No such variable.

 Application.Terminate;
end;

Application.Terminate needs CanClose to be true but I cannot find a way to set it true, it doesn't seem to be a property of the form or of the application.

0
Comment
Question by:frog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Accepted Solution

by:
Hypoviax earned 252 total points
ID: 12176440
You assign the action property in formclose:

action:=canone;

By setting action to canone the user cannot close the application

The other settings you can assign to the action property are:

caNone, caHide, caFree, caMinimize

The cafree would enable the form to be closed

Regards
0
 
LVL 4

Assisted Solution

by:ceoworks
ceoworks earned 248 total points
ID: 12176461
Hi frog,

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnCloseAllow: TButton;
    btnCloseDecline: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnCloseAllowClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnCloseDeclineClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  IsCloseAllowed: Boolean;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  IsCloseAllowed := False;
end;

procedure TForm1.btnCloseAllowClick(Sender: TObject);
begin
  IsCloseAllowed := True;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := IsCloseAllowed;
end;

procedure TForm1.btnCloseDeclineClick(Sender: TObject);
begin
  IsCloseAllowed := False;
end;

end.

Regards,

Oktay Sancak
0
 

Expert Comment

by:manganzon
ID: 12176674
Hi. I understand some parts of your question: 1) u dont want the user to close your app.
2) u want ur app to do some stuff when it detects the user is closing windows.

1) To acompplisth the first goal u can certainly use canclose property but the user will still be able to kill the process. You can create a library to prevent the user to close the process too with madshi code hook (use this is your app runs in the background cause if your app is visible user will be able to close it, in this case you should use both canclose property and this code so that it will be very hard for the user to close your program):

library  dontcloseme;



uses
  Windows,
  madRemote,
  madCodeHook,
  madStrings;

var TerminateProcessNext : function (processHandle, exitCode: dword) : bool; stdcall; //this is the original function

function ThisIsOurProcess(processHandle: dword) : boolean; //just a function to check is this is our process
var pid   : dword;
    arrCh : array [0..MAX_PATH] of char;
begin
  pid := ProcessHandleToId(processHandle);
  result := (pid <> 0) and ProcessIdToFileName(pid, arrCh) and
            (PosText('Your_application_exe_name_here.EXE', arrCh) > 0);
end;

function TerminateProcessCallback(processHandle, exitCode: dword) : bool; stdcall;// the callback function
begin
  if ThisIsOurProcess(processHandle) then begin
    result := false;
    SetLastError(ERROR_ACCESS_DENIED);  //if this is our process, then set an error
  end else
    result := TerminateProcessNext(processHandle, exitCode);//else terminate the process
end;

begin
  HookAPI('kernel32.dll', 'TerminateProcess', @TerminateProcessCallback, @TerminateProcessNext); //hook the API
end.

You can download the mad code hooks in madshi's webpage, here: www.madshi.net


To do the second thing you could use this code:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)

    procedure EndSMsg(var T : TMessage);message  WM_QUERYENDSESSION;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Plop: Boolean;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Plop:=False;
end;

procedure TForm1.EndSMsg(var T : TMessage);
begin
  T.result := 0;{this will stop windows shutdown}
  if T.lparam = 0 then
    begin
{The tasks you want to do}
    end
  else if (DWORD(T.lparam) and ENDSESSION_LOGOFF) =
                                 ENDSESSION_LOGOFF then
    begin
{The tasks you want to do}
    end;
Plop:=True;
T.result:=1;{after you do the stuff you will continue with windows shutdown}
Application.Terminate;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
Canclose:= Plop {THIS PART IS CEOWORKS CODE FROM THE PREVIOUS POST, Im just mixing this with my code}
end;


end.


Hope this helps.
Regards.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:ceoworks
ID: 12176774
Oh sorry, i didn't noticed that you are talking about to prevent the Windows Shutdown. My answer is not quite..
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12176835
I actually think our suggestions ceoworks are quite correct. The questioner is asking how can he prevent users closing the program, both yourself and i have answered that in similar fashion so i do believe that our answers are correct.

Regards,

Hypoviax
0
 

Author Comment

by:frog
ID: 12197980
Thanks All.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question