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/Silverlight
标签:
WPF
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)