Board index » delphi » Detect if last window is open and close MainForm (MainForm Hidden)

Detect if last window is open and close MainForm (MainForm Hidden)

Hello All;

I need to let my application "Detect if a Form, (Window) is the Last one
opened, And then if it is. I need it to Close the MainForm. The MainForm, is
hidden from "Window & Taskbar)

I am using this to Hide the MainForm and Show Form2

procedure TForm1.FormActivate(Sender: TObject);
begin
ShowWindow(Form1.Handle,SW_HIDE);
Form2.Show;
end;

I am using this to Close the MainForm.

TForm2.OnClose;
 Application.MainForm.Close;
end;

I need it to act a little (Or allot) like IE does.
How you can open multiple windows in IE, and thus allowed to close either
one. Dreamweaver gives this same feature as well.

Any idea's on how this can be accomplished??

Wayne

 

Re:Detect if last window is open and close MainForm (MainForm Hidden)


Hello All;

I need to let my application "Detect if a Form, (Window) is the Last one
opened, And then if it is. I need it to Close the MainForm. When the last
window is closed.?
The MainForm, is hidden from "Window & Taskbar)

I am using this to Hide the MainForm and Show Form2

procedure TForm1.FormActivate(Sender: TObject);
begin
ShowWindow(Form1.Handle,SW_HIDE);
Form2.Show;
end;

I am using this to Close the MainForm.

TForm2.OnClose;
Application.MainForm.Close;
end;

I need it to act a little (Or allot) like IE does.
How you can open multiple windows in IE, and thus allowed to close either
one. Dreamweaver gives this same feature as well.

Any idea's on how this can be accomplished??

Wayne

Re:Detect if last window is open and close MainForm (MainForm Hidden)


I found this on a Group (Not my language "English here" Japan I think it
was, so I was unable to read what the posting was, and if this is what I
would need. But hopefully you may be able to assist in this. It seems that
it may be what I need, But not really sure???

==========
var
 i, MaxOpen, LastCount: Integer;
 toClose: TCustomForm;
begin
 Result := True;
 try
   MaxOpen := 1 + Ord((toKeep <> nil) and (Application.MainForm <> toKeep));
   with Screen do begin
     i := CustomFormCount;
     while CustomFormCount > MaxOpen do begin
       Dec(i);
       toClose := CustomForms[i];
       if not((toClose=toKeep) or (toClose=Application.MainForm)) then begin
         LastCount := CustomFormCount;
         toClose.Perform(WM_CLOSE, 0, 0); // imho besser als Close
         if CustomFormCount >= LastCount then begin
           Inc(MaxOpen,CustomFormCount-LastCount+1); // falls Neue
entstanden sind
           Result := False;
         end;
       end;
     end;
   end;
 except
   Result := False;
 end;
end;

==========
If this is what I am needing, Could someone please let me know, Where I
should put this at? And what I may need to change? & (or) add? to make it
compile and work properly?

Thank You, And sorry for all the questions here :-)

Wayne

Re:Detect if last window is open and close MainForm (MainForm Hidden)


This has been resolved

Thank You
Wayne

Other Threads