WPF窗口的动画宽高我想给一个wpf窗口的宽高做动画。我尝试了以下方法,不幸的是,它只是设置了宽度的动画……窗口的高度永远不会改变。我确定我遗漏了一些愚蠢的东西,希望通过在这里发布有人会看到我的错误!下面是一个简单窗口背后的代码,该窗口带有一个我已连线以调整大小的按钮:publicpartialclassMainWindow:Window}privatevoidButton_Click(objectsender,RoutedEventArgse){this.AnimateWindowSize(ActualWidth+200,ActualHeight+200);下面是我作为扩展方法编写的动画代码,因此它可以应用于任何窗口...{Duration=newDuration(newTimeSpan(0,0,0,0,200))};varaniWidth=newDoubleAnimationUsingKeyFrames();varaniHeight=newDoubleAnimationUsingKeyFrames();aniWidth.Duration=newDuration(newTimeSpan(0,0,0,0,200));aniHeight.Duration=newDuration(newTimeSpan(0,0,0,0,200));aniHeight.KeyFrames.Add(newEasingDoubleKeyFrame(target.ActualHeight,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,00))));aniHeight.KeyFrames.Add(新的EasingDoubleKeyFra我(newHeight,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,200))));aniWidth.KeyFrames.Add(newEasingDoubleKeyFrame(target.ActualWidth,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,00))));aniWidth.KeyFrames.Add(newEasingDoubleKeyFrame(newWidth,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,200))));Storyboard.SetTarget(aniWidth,target);Storyboard.SetTargetProperty(aniWidth,newPropertyPath(Window.WidthProperty));Storyboard.SetTarget(aniHeight,target);Storyboard.SetTargetProperty(aniHeight,newPropertyPath(Window.HeightProperty));sb.Children.Add(aniWidth);Children.Add(aniHeight);sb.开始();预先感谢您的帮助在乔对使用pinvoke和依赖属性发表评论之后,我最终得到了这段代码。如果代码很长,我深表歉意,我不应该把它全部放在这里。数学在规模上并不完美。WPF实际(高度/宽度)和Rect.Height/Width之间存在很大差异,可能需要一些计算才能获得您想要的确切尺寸。这已经添加到主窗口类中[StructLayout(LayoutKind.Sequential)]publicstructRECT{publicintX;公共诠释Y;公共int宽度;公共诠释高度;}publicenumSpecialWindowHandles{HWND_TOP=0,HWND_BOTTOM=1,HWND_TOPMOST=-1,HWND_NOTOPMOST=-2}[DllImport("user32.dll",SetLastError=true)]staticexternboolGetWindowRect(IntPtrhWnd,refRECTRect);[DllImport("user32.dll")][返回:MarshalAs(UnmanagedType.Bool)]staticexternboolSetWindowPos(IntPtrhWnd,IntPtrhWndInsertAfter,intX,intY,intcx,intcy,uintuFlags);publicstaticreadonlyDependencyPropertyWindowHeightAnimationProperty=DependencyProperty.Register("WindowHeightAnimation",typeof(double),typeof(MainWindow),newPropertyMetadata(OnWindowHeightAnimationChanged));privatestaticvoidOnWindowHeightAnimationChanged(DependencyObjectd,DependencyPropertyChangedEventArgse){varwindow=dasWindow;if(window!=null){IntPtrhandle=newWindowInteropHelper(w窗口)。句柄;varrect=newRECT();if(GetWindowRect(handle,refrect)){rect.X=(int)window.Left;rect.Y=(int)window.Top;rect.Width=(int)window.ActualWidth;rect.Height=(int)(double)e.NewValue;//从对象到double到int的双重转换}}}publicdoubleWindowHeightAnimation{get{return(double)GetValue(WindowHeightAnimationProperty);}set{SetValue(WindowHeightAnimationProperty,value);}}publicstaticreadonlyDependencyPropertyWindowWidthAnimationProperty=DependencyProperty.Register("WindowWidthAnimation",typeof(double),typeof(MainWindow),newPropertyMetadata(OnWindowWidthAnimationChanged));privatestaticvoidOnWindowWidthAnimationChanged(DependencyObjectd,DependencyPropertyChangedEventArgse){varwindow=dasWindow;if(window!=null){IntPtrhandle=newWindowInteropHelper(window).Handle;varrect=newRECT();if(GetWindowRect(handle,refrect)){rect.X=(int)window.Left;rect.Y=(int)window.Top;varwidth=(int)(double)e.NewValue;rect.Width=宽度;rect.Height=(int)window.ActualHeight;SetWindowPos(handle,newIntPtr((int)SpecialWindowHandles.HWND_TOP),rect.X,rect.Y,rect.Width,rect.Height,(uint)SWP.SHOWWINDOW);}}}publicdoubleWindowWidthAnimation{get{return(double)GetValue(WindowWidthAnimationProperty);}set{SetValue(WindowWidthAnimationProperty,value);}}privatevoidGrowClick(objectsender,RoutedEventArgse){this.AnimateWindowSize(Width+200,Height+200);}//////SetWindowPosFlags///publicstaticclassSWP{publicstaticreadonlyintNOSIZE=0x0001,NOMOVE=0x0002,NOZORDER=0x0004,NOREDRAW=0x0008,NOACTIVATE=0x0010,DRAWFRAME=0x0020,FRAMECHANGED=0x0020,SHOWWINDOW=0x0040,HIDEWINDOW=0x0080,NOCOPYBITS=0x0100,NOOWNERZORDER=0x0200,NOREPOSITION=0x0200,NOSENDCHANGING=0x0400,DEFERERASE=0x2000,ASYNCWINDOWPOS=0x4000;在OP的代码中,我相应地更改了高度和宽度目标属性Storyboard.SetTargetProperty(aniHeight,newPropertyPath(Window.HeightProperty));故事板。SetTargetProperty(aniWidth,newPropertyPath(Window.WidthProperty));到Storyboard.SetTargetProperty(aniHeight,newPropertyPath(MainWindow.WindowHeightAnimationProperty));Storyboard.SetTargetProperty(aniWidth,newPropertyPath(MainWindow.WindowWidthAnimationProperty));根据我原来的回答:发现,你的代码没有问题当我改变动画添加到故事板(sb.Children.Add)实例的顺序时,我得到了没有宽度的高度动画。这让我相信当第一个动画发生时,另一个动画变得无效。我所能想到的就是让一个动画比另一个稍微长一点,然后让它们一个接一个地制作动画。第一个动画完成后,会出现更长的动画。varsb=newStoryboard{Duration=newDuration(newTimeSpan(0,0,0,0,300))};varaniWidth=newDoubleAnimationUsingKeyFrames();varaniHeight=newDoubleAnimationUsingKeyFrames();aniWidth.Duration=newDuration(newTimeSpan(0,0,0,0,300));aniHeight.Duration=newDuration(newTimeSpan(0,0,0,0,150));aniHeight.KeyFrames.Add(newEasingDoubleKeyFrame(target.ActualHeight,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,00))));aniHeight.KeyFrames.Add(newEasingDoubleKeyFrame(newHeight,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,150))));aniWidth.KeyFrames.Add(newEasingDoubleKeyFrame(target.ActualWidth,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,150))));aniWidth.KeyFrames.Add(newEasingDoubleKeyFrame(newWidth,KeyTime.FromTimeSpan(newTimeSpan(0,0,0,0,300))));即使不使用XAML故事板,我也可以调整窗口的高度和宽度。使用较新DP的一个建议对我来说似乎有点矫枉过正,特别是因为该解决方案承认它仍然不会同时调整大小。添加甚至1毫秒的延迟(通过Task.Run())在我的快速实验中实现了最终结果(窗口调整大小)。这个解决方案也不会同时调整大小,所以动画没有它应该的那么优雅,但它最终“起作用”了。以上就是C#学习教程的全部内容:动画WPF窗口的宽高。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。侵权请点击右侧联系管理员删除。如需转载请注明出处:
