Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TForm.Show doesn't bring form to front (D2007 or CPP2007)

152 views
Skip to first unread message

David Charron

unread,
Oct 30, 2007, 1:18:48 PM10/30/07
to
Hi,

I have problem to bring a form to front.
Here is a test project.

Create an Application
Add a form. (form2)
Add a button on the 2 form.
Reference each from in the other unit.
Set the button1 click on each form to show the other form.
When Form1.Buttonclick1 show form2 it works fine.
When form2.Buttonclick1 show form1 it doesn't bring form1 to front. Actually
form1 always stays "under" form2 event if calling Form1.BringToFront.
This used to work (form1 would be brought to front) in Delphi7 (and C++
builder 6).
Now in Delphi2007 or C++ Builder 2007 form1 always stays "under" form2. It
receives the focus but it is not brought to front.

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation

uses Unit2;

{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;
end;

end.

================================================

unit Unit2;
interface

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

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
implementation
uses Unit1;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.Show;
end;

end.


Peter Below (TeamB)

unread,
Oct 30, 2007, 2:47:08 PM10/30/07
to
David Charron wrote:

> Hi,
>
> I have problem to bring a form to front.
> Here is a test project.
>
> Create an Application
> Add a form. (form2)
> Add a button on the 2 form.
> Reference each from in the other unit.
> Set the button1 click on each form to show the other form.
> When Form1.Buttonclick1 show form2 it works fine.
> When form2.Buttonclick1 show form1 it doesn't bring form1 to front.
> Actually form1 always stays "under" form2 event if calling
> Form1.BringToFront. This used to work (form1 would be brought to
> front) in Delphi7 (and C++ builder 6). Now in Delphi2007 or C++
> Builder 2007 form1 always stays "under" form2. It receives the focus
> but it is not brought to front.

That is connected to the new Application.ShowMainformOnTaskbar
property. It defaults to true for new projects and it makes the main
form the API owner of all secondary forms. This wires them to the main
form in Z order. For modal forms that is in fact a good idea, but for
modeless forms that should behave like siblings of the main form in Z
order it is not.

You can fix that problem either by setting
Application.ShowMainformOnTaskbar to false (edit the DPR file for this,
some things will not work properly on Vista with this setting), or by
overriding the secondary forms CreateParams method like

procedure TForm2.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
Params.WndParent := GetDesktopWindow;
end;

A side effect of this is that the form now gets its own taskbar button
and it will no longer minimize when the main form is minimized.


--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com

David Charron

unread,
Oct 30, 2007, 3:25:34 PM10/30/07
to
> You can fix that problem either by setting
> Application.ShowMainformOnTaskbar to false> --
> Peter Below (TeamB)

That fixed it. I don't have to support vista right now anyway.

Thank you Peter :)

David.


Glynn Snider

unread,
Dec 9, 2022, 6:54:06 AM12/9/22
to
Please make this solution more widely available. I spent hours researching this problem before I ran across this article.

Thanks,
Glynn Snider
0 new messages