I built the code using Delphi XE2. It creates Form1, and Form1 instantly creates an instance of Form2. When I press the button on Form2, the second form 2 is created.
Now, if I hover over the button on this second, topmost, Form2 and wait for the tooltip to appear, at the moment when the tooltip appears, the first Form2 comes forward, stealing focus.
The problem only occurs if Application.MainFormOnTaskbar is True . It also relies on the first Form2 created from Form1's FormCreate method. If I use PostMessage() to delay the creation of the first Form2 until the application completes the initialization, the problem will disappear.
I would like to understand why this is happening. I already learned that the Delphi application object handles a lot of things, including the tooltip, and I know that Delphi can recreate the window handle during initialization, but I could not follow this to fully explain the behavior described above (or are these two facts even relevant).
Project1.dpr
program Project1; uses Vcl.Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2}; {$R *.res} begin Application.Initialize; Application.MainFormOnTaskbar := True; // False makes problem go away Application.CreateForm(TForm1, Form1); Application.Run; end.
Unit1.pas
unit Unit1; interface uses Vcl.Forms, Unit2; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); public procedure CreateForm2; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin CreateForm2; end; procedure TForm1.CreateForm2; var frm : TForm2; begin frm := TForm2.Create(Application); // (Could pass Self - makes no difference) frm.Show; end; end.
unit2.pas
unit Unit2; interface uses Vcl.Forms, System.Classes, Vcl.Controls, Vcl.StdCtrls, WinApi.Windows; type TForm2 = class(TForm) Button1: TButton;