How to prevent loss of focus when showing and / or closing secondary forms from the main form?

Displaying 2 secondary forms from the main form and then closing both forms will cause the main form to lose focus. (another application is activated instead of mine)

Secondary forms are created either directly by the main form, or are created by the third form from the second form.

Secondary forms set by caFree in the OnClose event:

procedure TForm1.FormClose (Sender: TObject; var Action: TCloseAction);
begin
  Action: = caFree;
end;

Using Delphi 2009 (Update 3 and 4) with XP SP3.

Here are my steps to reproduce the problem:

  • Create New VCL Forms Applications
  • Set an OnClose event as above
  • Drag the button into the created form
  • In the click handler, create a new TForm1 and show it below

. , . , . .

:

with TForm1.Create(Application) do
    show;

?

(, , )


, delphi, caFree OnClose, .

, Parent Main Form, , d . ( )

+3
4

"owning" api , :

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  SetForegroundWindow(GetWindowLong(Handle, GWL_HWNDPARENT));
end;

( ), .

MainFormOnTaskBar, , . .

SetForegroundWindow - , , MainFormOnTaskBar , , ( , , , ).

+3

mainform,

// Application.MainFormOnTaskBar := True;

@Serg . , , , . , PopupMode pmAuto, , , , , .

, , , , , , . :

  • MainForm Secondary1
  • Secondary1 Secondary2 Secondary3

Secondary1 Secondary2 Secondary3.

, , PopupParent . , "" :

procedure TForm1.FormCreate(Sender: TObject);
begin
  PopupMode := pmAuto;
  if Self <> Application.MainForm then
    PopupParent := Application.MainForm;
end;

, Application.MainForm ; ; .

+1

, MainFormOnTaskbar :

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
//  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

If you want MainForm to always remain behind other forms, you must also override CreateParams. The following code works as you expect, although I suspect that it may not be suitable for any other reason:

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TForm1.Create(Application) do
    show;
end;

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  if Application.MainForm <> nil
    then Params.WndParent:= Application.MainForm.Handle
    else Params.WndParent:= 0;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.
0
source

If this problem is for the Toolwindow pop-up form, the application would lose focus when you click the close button.

Fix: Self.Hide in the OnClose event.

procedure TPopupForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Self.Hide;
  Action := caFree;
end;
0
source

Source: https://habr.com/ru/post/1768426/


All Articles