How to make a form always stay on top

走远了吗. 提交于 2021-02-18 11:19:30

问题


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:


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.



来源:https://stackoverflow.com/questions/30070603/how-to-make-a-form-always-stay-on-top

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!