簡體   English   中英

顯示對話框時WPF模糊主窗體

[英]WPF Bluring main form when showing dialogs

所以我在主要形式的主網格上添加了模糊效果:

 <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());

我的問題是,有什么更好的方法嗎?

好的,這個答案太遲了,但是總比沒有好,對吧? 我設法通過捕獲WndProc消息WM_SETFOCUS和WM_KILLFOCUS並在WM_KILLFOCUS上模糊化表單並在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;
    }

希望這對某人有幫助。

編輯。 我只是意識到我可以使用Got / LostKeyboardFocus事件來實現相同的功能,下面是一個示例:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM