Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a form always stay on top

Tags:

forms

delphi

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;
like image 391
Airogat Avatar asked May 06 '15 07:05

Airogat


People also ask

How do you make a form always on top?

You can bring a Form on top of application by simply setting the Form. topmost form property to true will force the form to the top layer of the screen, while leaving the user able to enter data in the forms below. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.

How to keep a Form always on top c#?

dll that is SetWindowPos() that will position our window's Z order so that our window will be always on top . SetWindowPos(this. Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); Now when we run our application our form will be top of every application..


1 Answers

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.

like image 79
fpiette Avatar answered Sep 28 '22 10:09

fpiette