How to keep a window on top of all other windows in my application only?

半城伤御伤魂 提交于 2019-12-23 20:00:31

问题


I would like to display a status window in my C# Windows Forms application that informs the user when the application is waiting to acquire a lock. This is an application-defined thing, however, the window should be visible and always remain on top of all other windows of my application, even when the user clicks on another window (like for example the larger main window behind it).

It must not be modal (so ShowDialog() cannot be used) because the app needs to keep trying in the background and close the window automatically if the lock could eventually be acquired, and it really should not be top-most for the entire window station (i.e. all applications running in that terminal session).

I know the Form.TopMost property, but it can only bring and keep a single window above all others, even those from other applications. This is clearly not what I'm looking for.

I know that this is possible, I've seen it many times before in other applications. I just don't know how it can be done.


回答1:


If you pass your main form into the Show method of the status form, it will stay on top of the main form, but not on top of other applications. So, in the main form you can have code like so:

StatusForm statusForm = new StatusForm();
statusForm.Show(this);

However, this will only point out one single window of your application as the owner.




回答2:


You have to set the Owner property of the child form to the parent form, and use Show to show the child form.



来源:https://stackoverflow.com/questions/1393260/how-to-keep-a-window-on-top-of-all-other-windows-in-my-application-only

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