7
votes

I have an application which consists of a form with FormStyle declared as "fsStayOnTop", so that it always is shown on top of all other windows. Now I would like to temporarily show another form where the user can set some settings. That form should also be shown on top, so I change the FormStyle property of the main form to "fsNormal" and the FormStyle of the form I want to show to "fsStayOnTop". When the temporary form closes the main form gets "fsStayOnTop" again.

Now the settings form stays on top, but only until I activate it by a mouse click inside the form. When I click on another window after that, the clicked form is on top and the defined FormStyle seems to have no effect anymore. Can anyone help me with that?

Here's my FormShow and FormClose mehtod:

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ScaleOpen := false;
  SetForegroundWindow(TempHandle);
  Form1.FormStyle := fsStayOnTop;
end;


procedure TForm3.FormShow(Sender: TObject);
begin
  TempHandle := GetForegroundWindow;
  OldScaleM := Form1.GetScale;
  SaveChanges := False;
  ScaleOpen := true;
  Form1.FormStyle := fsNormal;
  Form3.FormStyle := fsStayOnTop;
end;
1
Have you verified that when the "FormStyle seems to have no effect" it is indeed fsStayOnTop and not something else?ain
you need to use ShowModal when you want to show your settings form...whosrdaddy
Yes I use showModal to show itAirogat
@Airogat If you are showing your settings form as modal form then you don't need to change the form style of your main form becouse modal forms with fsStayOnTop are always placed above any normal form even thou it has style of fsStayOnTop .SilverWarior
The solution you accepted is surely the wrong way to solve the problem. It's not clear that you really want to get to the bottom of this.David Heffernan

1 Answers

22
votes

You can set a form to the "always on top" state using this code:

        SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
                     SWP_NoMove or SWP_NoSize);

You go back to normal mode with this code:

        SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0,
                     SWP_NoMove or SWP_NoSize);

To try it, just drop two buttons on your form and associate the above code to their respective OnClick handlers.