How to keep a window on top of all other windows i

2019-07-04 10:56发布

问题:

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.