简体   繁体   中英

WPF Bluring main form when showing dialogs

So i've added blureffect on main grid of main form:

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

and added custom method for opening dialogs on main form:

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

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

        return window;
    }

This is how i'm calling this method from other forms when they are creating dialogs:

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

My question is, is there any better way to do this?

Okay, this answer is super late, but better late than never, right? I managed to do it by catching WndProc messages WM_SETFOCUS and WM_KILLFOCUS and bluring form on WM_KILLFOCUS and de-bluring (??) on 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;
    }

Hope this helps someone.

EDIT. I just realised i could use Got/LostKeyboardFocus events to achieve same thing, here is an example:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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