简体   繁体   English

如何隐藏所有者最小化后显示的拥有窗口?

[英]How to hide an Owned window shown after the Owner is minimized?

I know about the contract between Owned windows and their Owner and that the Owned windows are hidden when the Owner is hidden. 我知道拥有的窗口和它们的所有者之间的合同,并且隐藏所有者时隐藏了拥有的窗口。 This is actually what I want. 这实际上就是我想要的。 The problem is that if you show an Owned window after the Owner window is minimized then the Owned window is not hidden but is displayed normally on screen. 问题是,如果 “所有者”窗口最小化显示“拥有”窗口则“拥有”窗口不会隐藏,而是在屏幕上正常显示。 In this case the WindowState of the Window is Minimized at start up so any Owned window that is shown is displayed on screen. 在这种情况下,窗口的WindowState在启动时最小化,因此显示的任何Owned窗口都显示在屏幕上。

I know that I can call ShowOwnedPopups to force the OS to hide all the Owned windows but this only seems to work after the Owned window has actually been shown so you get a flicker on screen. 我知道我可以调用ShowOwnedPopups来强制操作系统隐藏所有拥有的窗口,但这似乎只有在实际显示Owned窗口后才能显示,因此屏幕上会出现闪烁。 I have tried explicitly hiding the window (eg removing the WS_VISIBLE bit or trying to alter the WM_SHOWCOMMAND such that it is shown hidden) but then the Owned window is not made visible when the Owner is restored. 我已经尝试显式隐藏窗口(例如,删除WS_VISIBLE位或尝试更改WM_SHOWCOMMAND以使其显示为隐藏)但是当还原所有者时,Owned窗口不可见。 This is consistent with the documentation for the ShowOwnedPopups API whereby the only windows that are made visible are those hidden by the ShowOwnedPopups call. 这与ShowOwnedPopups API的文档一致,其中唯一可见的窗口是ShowOwnedPopups调用隐藏的窗口。

So I'm looking for some way to either have the OS show the window in such a way that it is not made visible to the end user until the Owner is restored -or- a way to set a flag on the window so that the OS thinks it hid the window using ShowOwnedPopups and then I will manually hide the window and set that flag. 所以我正在寻找某种方法让操作系统以这样一种方式显示窗口,直到所有者恢复后才能让最终用户看到它 - 或者是一种在窗口上设置标志的方法,以便OS认为它使用ShowOwnedPopups隐藏窗口,然后我将手动隐藏窗口并设置该标志。

Note, the code that is showing the Owned windows isn't associated with the shell so it's not a simple matter of not showing the owned window until the owner is restored. 注意,显示Owned窗口的代码与shell没有关联,所以在恢复所有者之前不显示拥有的窗口并不是一件简单的事情。 This is part of a separate custom control and in this case it happens to be a WPF control so the control could even be used in an ElementHost so I can't just watch for the WindowState of the owning window either. 这是一个单独的自定义控件的一部分,在这种情况下,它恰好是一个WPF控件,因此控件甚至可以在ElementHost中使用,所以我不能只关注拥有窗口的WindowState。 Any windows API solution is welcomed though. 欢迎使用任何Windows API解决方案。

As a little dirty trick (which i have used previously at one of my projects), I suggest creating the window outside of the visible area, minimize and move its position to visible area again when it is minimized. 作为一个小肮脏的技巧(我之前在我的一个项目中使用过),我建议在可见区域之外创建窗口,在最小化时最小化并将其位置再次移动到可见区域。 Check the code below. 检查下面的代码。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var w = new Window();   // Create window
    w.Owner = this;         // Set this window as owner
    w.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual; // Set startup location as manual
    w.Left = -10000;        // Set x position to -10000 (or anything which makes it out of bounds)
    w.Top = -10000;         // Set y position to -10000
    w.Show();               // Show window (it will not be shown, not even blink)
    w.WindowState = System.Windows.WindowState.Minimized;   // Set window as minimized
    w.Left = 100;           // Set x position to 100 (or whatever you want)
    w.Top = 100;            // Set y position to 100 (or whatever you want)
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM