如何在WPF中使所有屏幕区域变暗并突出显示我打开的窗口?

9
在WPF中,当打开新窗口时,如何使所有屏幕区域变暗?在窗口关闭后,如何恢复临时效果?

你可以查看这个链接: http://stackoverflow.com/questions/5825527/gray-out-the-desktop-screen-except-a-selected-control-in-c-sharp/38495792#38495792 - swapnil saha
4个回答

28

这是我的版本,如果您想要将父窗口变成灰色并模糊:

private void btnOpenSettings_Click(object sender, RoutedEventArgs e)
    {
        // settings for the parent window
        // set the transparency to the half
        this.Opacity = 0.5;
        // blur the whole window
        this.Effect = new BlurEffect();

        // Set the options for the settings (child) window
        SettingsForm wdwSettings = new SettingsForm() 
        { 
            Owner = this,
            ShowInTaskbar = false,
            Topmost = true
        };

        // Open the child window
        wdwSettings.ShowDialog();

        //restore Opacity and remove blur after closing the child window
        this.Opacity = 1;
        this.Effect = null;
    }

它运行得很好,但是一旦ShowDialog()关闭,我如何返回到原始状态? - Mugiwara
1
没关系 :) this.Effect = null; 将其恢复为正常状态。 - Mugiwara

14

你可以像这样创建一个背景透明的窗口:

var darkwindow = new Window() {
                            Background = Brushes.Black,
                            Opacity = 0.4,
                            AllowsTransparency = true,
                            WindowStyle = WindowStyle.None,
                            WindowState = WindowState.Maximized,
                            Topmost = true
                        };
darkwindow.Show();
MessageBox.Show("Hello");
darkwindow.Close();

MessageBox.Show("Hello");替换为mywindow.ShowModal();。 可能需要使mywindow始终在顶部。

编辑

不要使用darkwindow.Hide()代替Close()


感谢Mykola Bogdiuk。我在窗口上创建了一个黑色和透明的边框,然后显示了新窗口。 - hamed aj

9

降低当前窗口的不透明度,

例如:

{
    this.Opacity = 0.5;//Decrease opacity
    MessageBox.Show("Ur Window Darken");
    this.Opacity = 100;//Reset the opacity
}

简单的解决方案 - YoungStacker

0

最简单的方法:按照以下方式使用XAML弹出窗口

<Popup x:Name="pop" IsOpen="False" >

</Popup> 

更多细节请访问下面的链接。 http://www.c-sharpcorner.com/UploadFile/mahesh/using-xaml-popup-in-wpf/

在此之后,为了在事件处理程序中模糊主网格以显示弹出窗口,请按以下C#代码设置不透明度

if (pop.IsOpen == false)    
{    
pop.IsOpen = true;    
grdMain.Opacity = 0.4;    
}    
else    
{    
pop.isopen=false;    
}    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接