How to make sure a dialog is always front of the main window

限于喜欢 提交于 2019-12-05 02:11:27

问题


I have not yet found the best solution for this. I have a non modal dialog that can be opened in unlimited instances by a hotkey in the application. Even the dialog itself can open a new instance. I want those dialogs to always be front of the main application window. I have tried a couple of things.

  • Set FormStyle to fsStayOntop. This works but have the advantage that the dialog will be front of ALL windows even other applications. I only want it to be front of my main window.
  • Set PopupMode to pmAuto. This also works except for the case when one dialog open another dialog. If the first dialog is closed then it automatically close the second one. This is not acceptable.
  • Use the default properties for a form. As soon as the main window is clicked on the opened dialogs is behind the main window.

Any other suggestions :-)


回答1:


From TCustomForm.PopupParent Property;

If the PopupMode property is set to pmExplicit and PopupParent is nil, then the Application.MainForm is implicitly used as the PopupParent

.




回答2:


AFAIK Delphi 2007 support MainFormOnTaskbar feature. With

   Application.MainFormOnTaskbar := True;

in project source ANY application form (with default parent window) is shown above main form.


If you are unsure what form is the Main Form, go to Project/Options/Forms and set the correct Main Form. Another probable reason is that you are upgrading a project from a previous Delphi version so the project source does not contain the above line of code - add this line manually.




回答3:


I think your first effort, fsstayontop, is the best option. The issue with displaying in front of other applications may be impossible to avoid since you are really using Windows function not something unique to Delphi.

As I recall it is possible to manually set the Z order, but that is tedious to impossible in most apps.

If your dialog is not so big as to hide other applications, it can be moved and users can still access the other apps without first interacting with your dialog. Seems not too bad.




回答4:


You can try watching the OnHide event and immediately making the Visible flag to TRUE. This will probably cause flicker though.



来源:https://stackoverflow.com/questions/3549119/how-to-make-sure-a-dialog-is-always-front-of-the-main-window

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