如何使表格始终放在首位 [英] How to make a form always stay on top

查看:59
本文介绍了如何使表格始终放在首位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中包含一个带有FormStyle的窗体,该窗体声明为"fsStayOnTop",因此该窗体始终显示在所有其他窗口的顶部.现在,我想暂时显示另一个表单,用户可以在其中设置一些设置.该表单也应该显示在顶部,因此我将主表单的FormStyle属性更改为"fsNormal",并将我想显示的表单的FormStyle属性更改为"fsStayOnTop".临时表单关闭后,主表单将再次获得"fsStayOnTop".

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.

现在,设置表单仍位于顶部,但是直到我通过鼠标单击表单内部将其激活为止.之后,当我单击另一个窗口时,单击的表单位于顶部,并且定义的FormStyle似乎不再起作用.有人可以帮我吗?

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?

这是我的FormShow和FormClose方法:

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;

推荐答案

您可以使用以下代码将表单设置为始终位于顶部"状态:

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);

要进行尝试,只需在表单上放置两个按钮,然后将上面的代码与各自的OnClick处理程序相关联即可.

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

这篇关于如何使表格始终放在首位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆