显示对话框时WPF Bluring主窗体

时间:2016-05-25 10:23:18

标签: c# .net wpf

所以我在主窗体的主网格上添加了blureffect:

 <Grid.Effect>
    <BlurEffect x:Name="MainGridBlur" Radius="0" KernelType="Gaussian"/>
 </Grid.Effect>

并添加了在主窗体上打开对话框的自定义方法:

    public Window CreateDialogWindow(Window window)
    {
        window.Owner = this;
        window.WindowStartupLocation = WindowStartupLocation.CenterOwner;

        MainGridBlur.Radius = 10;
        window.ShowDialog();
        MainGridBlur.Radius = 0;

        return window;
    }

这就是我在创建对话框时从其他表单调用此方法的方法:

((MainWindow)Application.Current.MainWindow).CreateDialogWindow(new SomeDialog());

我的问题是,有没有更好的方法呢?

1 个答案:

答案 0 :(得分:1)

好的,这个答案已经很晚了,但迟到总比没有好,对吧? 我设法通过捕获WM_SETFOCUS上的WndProc消息WM_SETFOCUS和WM_KILLFOCUS以及WM_KILLFOCUS上的bluring形式和WM_SETFOCUS上的去模糊(??)来实现。

    using System.Windows.Interop;

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);
    }


    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            case 8: //WM_KILLFOCUS
                MainGridBlur.Radius = 10;
                break;
            case 7: //WM_SETFOCUS
                MainGridBlur.Radius = 0;
                break;
        }
        return IntPtr.Zero;
    }

希望这有助于某人。

EDIT。 我刚刚意识到我可以使用Got / LostKeyboardFocus事件来实现同样的事情,这里有一个例子:

    private void Main_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        MainGridBlur.Radius = 0;
    }

    private void Main_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        MainGridBlur.Radius = 10;
    }