Why does hiding an owned window switches app in WinForms (.NET)?
I have an application window which covers the screen and there are several minor tool windows which are hidden by default and their 'Owner' properties are set to this main window.
When I show a tool window and click one of its buttons it hides itself to show another window. When I hide that anothe开发者_高级运维r tool window Windows switches app just like when you press alt+tab. The only way to prevent this is clicking on the main application window before closing (hiding) the tool window.
Why does the application switching happens and how to prevent this? Well I can tell explicitly those windows to bring the main window front on hide (I haven't tried that yet, but I don't like to do that.) This only happens when the Owner property is set on the that windows to prevent them from showing behind the main window.
ps: This full screen app is a game and those windows are the menus and the setting screens etc...
Murky problem, I can't repro and haven't heard of this before. This behavior is normal when the Windows window manager is forced to hunt for another window to give the focus to when the current one closes. And the app doesn't have any window left that is enabled. That should not be the case in your app, unless you intentionally set your main window's Enable property to False if the tool window gets the focus. Running windows on different threads is a long-shot explanation.
Things to try are to set the tool window's ShowInTaskbar property to false so the user must always go through the main window to refocus the app. And to make sure the window only closes due to a MouseUp event so mouse capture can't mess things up. And, yes, to explicitly focus the main window in the FormClosing event.
精彩评论