简体   繁体   English

如何使子窗体保持在所有事物之上,但父窗体不必如此?

[英]How to keep a child form on top of everything, but the parent doesn't have to be?

I have seen a few postings that are similar to this question, but this has a subtle difference. 我已经看到一些与此问题类似的帖子,但这有细微的差别。 I have a program, the user interface of which can sit behind other programs, but it has a child form that is displayed in the top left corner that displays results, this part of the monitor is duplicated and is displayed to the public, it is a "scoreboard". 我有一个程序,其用户界面可以位于其他程序后面,但是它有一个子窗体,该子窗体显示在左上角,以显示结果,监视器的这一部分是重复的并向公众显示,它是一个“记分板”。 I want this "results" form to sit on top of everything (Taskmanager is OK, I can live with that). 我希望此“结果”表单位于所有内容之上(Taskmanager可以,我可以接受)。 Any other applications that would be run will not be trying to climb to the top, eg "Word", "IE", "Excel" etc. 将要运行的任何其他应用程序都不会尝试爬到顶部,例如“ Word”,“ IE”,“ Excel”等。

If I set the parent "SetWindowPos" to "TopMost" and the child also to "TopMost", both of my windows are on top, however, the user interface form also sits over anything. 如果将父级“ SetWindowPos”设置为“ TopMost”,而子级也设置为“ TopMost”,则我的两个窗口都位于顶部,但是,用户界面窗体也位于任何位置。 If the user needs to start another application, our form will sit over it, making the other application unusable, however, it will sit under our results form. 如果用户需要启动另一个应用程序,则我们的表单将位于其上方,从而使另一个应用程序无法使用,但是,它将位于我们的结果表单下。 We cannot use a timer to move it to the top as it will be covered until the timer fires, this is not allowed to happen. 我们不能使用计时器将其移到顶部,因为在计时器触发之前,它将被覆盖,这是不允许发生的。

If I only set the results form to topmost, it does not stay at the top of everything. 如果仅将结果表单设置为最高,则它不会停留在所有表单的顶部。 It appears that the parent must also be topmost? 看来父母也一定是最高的吗? Is there a way of getting around this and make the child on top, but its parent can sit below? 有没有一种解决方法,可以让孩子在上面,但其父母可以坐在下面? Or does the parent have to be topmost in order to have children also topmost? 还是父母必须居于最顶层才能使孩子也居于首位? The parent can not sit over the child as I grab the move event and prevent it moving over the child. 当我抓住移动事件并防止其移到孩子上方时,父母不能坐在孩子上方。

I am programming in Delphi, so any answers would be best in Delphi, but anything more generic is quite ok, I expect it would be a call to Windows anyway. 我在Delphi中编程,因此任何答案在Delphi中都是最好的,但是任何更通用的方法都可以,我希望无论如何这都是Windows的调用。

Thanks 谢谢

If I understand you correctly, your program has a main form ( MainForm ) and a results form ( ResultsForm ) and probably some other ones too, but those should 'behave' normally. 如果我对您的理解正确,那么您的程序将具有一个主窗体( MainForm )和一个结果窗体( ResultsForm ),也可能还有其他一些窗体,但是这些程序应该正常运行。

The MainForm should also behave normally, that is, move to the background if other applications are activated but the ResultsForm should always stay on top of all other forms, also those of other applications. MainForm也应正常运行,也就是说,如果激活了其他应用程序,则移至后台,但ResultsForm应始终位于所有其他表单(包括其他应用程序)的顶部。

The following is tested in Windows 7 only, so may or may not work with other OS's. 以下内容仅在Windows 7中进行了测试,因此可能与其他操作系统兼容或不兼容。 I will later today test with Windows 10. 我今天晚些时候将使用Windows 10进行测试。

First outcomment any previous attempts that might interfere. 首先胜过任何先前可能会干扰的尝试。 Leave MainForm.FormStyle as the default fsNormal . MainForm.FormStyle保留为默认的fsNormal Set ResultsForm.FormStyle to fsStayOnTop . ResultsForm.FormStyle设置为fsStayOnTop

Then add a TApplicationEvents to ResultsForm and add an event handler for OnDeactivate : 然后将TApplicationEvents添加到ResultsForm并为OnDeactivate添加事件处理程序:

procedure TResultsForm.ApplicationEvents1Deactivate(Sender: TObject);
begin
  SetForegroundWindow(Handle);
end;

The ResultsForm will now stay on top of other windows, also of other applications. 现在, ResultsForm将保留在其他窗口以及其他应用程序的顶部。

Caveat : If you start a second instance (or if some other application is doing the same trick) you will end up in a loop where the apps are fighting for being on top. 警告 :如果启动第二个实例(或者其他一些应用程序正在执行相同的技巧),则最终将陷入循环,使这些应用程序争夺一流地位。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将父表单数据网格读取为子表单 - How to read the parent form datagrid into child form 如何在父进程被杀死/完成时保持子进程处于活动状态(在 Windows 中) - How to keep child process active when parent is killed/finished (in windows) 在子窗口的OnShowWindow中将焦点设置到父窗口不起作用 - Setting focus to the parent window in the OnShowWindow of the child window doesn't work 设置为父级标准IO时,Windows Java子进程不输入或输出(命令提示符) - Windows Java child process doesn't input or output when set to parent's standard IO (Command Prompt) 在Windows中,对system()的调用不会在我的进程之间创建父子关系 - A call to system() doesn't create a parent-child relation between my processes in Windows 如何避免或限制MDI子窗体菜单条与MDI父窗体菜单条合并? - How to avoid or restrict MDI Child Form menu strip getting merged with MDI Parent Form menu strip? 看不见的父母的孩子形式永远不会出现? - Child form of invisible parent never being shown? 如何设置folderBrowser或SHBrowseForFolder的父级,以便对话框显示在窗体顶部-Visual C ++? - how to set parent of folderBrowser or SHBrowseForFolder so dialog shows on top of form - Visual C++? 运行3周后,为什么应用程序弹出错误“控制没有父窗口”? - Why would an application popup an error, “Control doesn't have parent window,” after running for 3 weeks? 显示父窗体 C# 的窗体侧面/侧面或顶部/底部 - Show A Form Side/Side Or Top/Bottom Of Parent Form C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM