1. 程式人生 > >WPF window 子窗口反饋效果(抖動/陰影漸變)

WPF window 子窗口反饋效果(抖動/陰影漸變)

roc 模態窗口 owin ons and anim keyframes com interop

原文:WPF window 子窗口反饋效果(抖動/陰影漸變)

當設置了owner的子窗口顯示後,點擊子窗口外部,需要一種反饋機制(反饋動畫)。

實現:

1.觸發源

每次點擊子窗口外部,即母窗口時,事件捕捉如下

HwndSource hwndSource = PresentationSource.FromVisual(this.Owner) as HwndSource;//窗口過程

hwndSource?.AddHook(WndProc);

也可以調用WindowInteropHelper,獲取母窗口句柄。

var hwnd = new WindowInteropHelper(this.Owner).Handle;
if (hwnd != IntPtr.Zero)
{
  var hwndSource = HwndSource.FromHwnd(hwnd);
  hwndSource?.AddHook(WndProc);
}

事件中,啟動動畫

 private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
         if (msg != 0x20) return IntPtr.Zero;
         if (lParam.ToInt32() == 0x201fffe) _storyboard?.Begin();
         return IntPtr.Zero;
    }

2.動畫設置

窗口抖動 動畫

            var
scaleXDoubleAnimation = new DoubleAnimationUsingKeyFrames(); var scaleYDoubleAnimation = new DoubleAnimationUsingKeyFrames(); scaleXDoubleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)),Value = 1.0}); scaleXDoubleAnimation.KeyFrames.Add(
new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(100)),Value = 0.95}); scaleXDoubleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200)),Value = 1.0}); scaleYDoubleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)),Value = 1.0}); scaleYDoubleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(100)),Value = 0.95}); scaleYDoubleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200)),Value = 1.0}); Storyboard.SetTarget(scaleXDoubleAnimation, window); Storyboard.SetTarget(scaleYDoubleAnimation, window); Storyboard.SetTargetProperty(scaleXDoubleAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)")); Storyboard.SetTargetProperty(scaleYDoubleAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)")); _storyboard = new Storyboard{Children =new TimelineCollection { scaleXDoubleAnimation, scaleYDoubleAnimation }};

設置後,點擊窗口外部,子窗口喚醒時,會有窗口大小變化(抖動的效果)

窗口陰影 動畫

            var animation = new DoubleAnimationUsingKeyFrames();

            animation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)),Value = 0});
            animation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200)),Value = 50});
            animation.KeyFrames.Add(new EasingDoubleKeyFrame{KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(400)),Value = 0});
            
            Storyboard.SetTarget(animation, window);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(FrameworkElement.Effect).(DropShadowEffect.BlurRadius)"));

            _storyboard = new Storyboard
            {
                Children = new TimelineCollection { animation }
            };

設置後,點擊窗口外部,子窗口喚醒時,會有窗口外部陰影變化

關鍵字:模態窗口,窗口抖動,窗口陰影

WPF window 子窗口反饋效果(抖動/陰影漸變)