创建一个WPF应用程序WpfApplication1,新建个窗体DialogWin
- <Windowx:Class="WpfApplication1.DialogWin"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="DialogWin"Height="632"Width="635"WindowStyle="None"
- Name="DW1" Background="Transparent"AllowsTransparency="True"WindowStartupLocation="CenterScreen"WindowState="Normal">
- <Window.Resources>
- <!--此样式能去除按钮点击后的虚线框-->
- <Stylex:Key="MyFocusVisual">
- <SetterProperty="Control.Template">
- <Setter.Value>
- <ControlTemplate>
- <RectangleMargin="-2"StrokeThickness="0"Stroke="Red"StrokeDashArray="1 2"/>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- <!--原始旋转出来样例-->
- <Storyboardx:Key="showDW">
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="Opacity"
- From="0.2"To="1"Duration="0:0:2.5">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[1].Angle"
- From="70"To="0"Duration="0:0:2">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="0"To="1"Duration="0:0:2"
- AccelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="0"To="1"Duration="0:0:2"
- AccelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- To="0.98"BeginTime="0:0:2"Duration="0:0:0.05"
- DecelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- To="0.98"BeginTime="0:0:2"Duration="0:0:0.05"
- DecelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- To="1"BeginTime="0:0:2.05"Duration="0:0:0.2"
- AccelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- To="1"BeginTime="0:0:2.05"Duration="0:0:0.2"
- AccelerationRatio="1">
- </DoubleAnimation>
- </Storyboard>
- <!--原始的旋转回去样例-->
- <Storyboardx:Key="closeDW">
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[1].Angle"
- To="360"Duration="0:0:1.5">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="Opacity"
- To="0"Duration="0:0:3">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- To="0"Duration="0:0:1.5"AccelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- To="0"Duration="0:0:1.5"AccelerationRatio="1">
- </DoubleAnimation>
- </Storyboard>
- <!--转出来-->
- <Storyboardx:Key="showDW1">
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="Opacity"
- From="0"To="1"Duration="0:0:0.2">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[1].Angle"
- From="180"To="0"Duration="0:0:0.2">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="0.2"To="1"Duration="0:0:0.2"
- AccelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="0.2"To="1"Duration="0:0:0.2"
- AccelerationRatio="1">
- </DoubleAnimation>
- </Storyboard>
- <!--弹出来-->
- <Storyboardx:Key="showDW2">
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="0.2"To="0.5"Duration="0:0:0.04">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="0.2"To="0.5"Duration="0:0:0.04">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="0.5"To="1"Duration="0:0:0.04"BeginTime="0:0:0.04">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="0.5"To="1"Duration="0:0:0.04"BeginTime="0:0:0.04">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="1"To="1.2"Duration="0:0:0.04"BeginTime="0:0:0.08"AutoReverse="True">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="1" To="1.2"Duration="0:0:0.04"BeginTime="0:0:0.08"AutoReverse="True">
- </DoubleAnimation>
- </Storyboard>
- <!--转回去-->
- <Storyboardx:Key="closeDW1">
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[1].Angle"
- To="180"Duration="0:0:0.2">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="Opacity"
- To="0"Duration="0:0:0.2">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- To="0.2"Duration="0:0:0.2"AccelerationRatio="1">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- To="0.2"Duration="0:0:0.2"AccelerationRatio="1">
- </DoubleAnimation>
- </Storyboard>
- <!--弹回去-->
- <Storyboardx:Key="closeDW2">
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="1"To="1.2"Duration="0:0:0.04" AutoReverse="True">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="1"To="1.2"Duration="0:0:0.04" AutoReverse="True">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="1"To="0.5"Duration="0:0:0.04"BeginTime="0:0:0.04">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="1"To="0.5"Duration="0:0:0.04"BeginTime="0:0:0.04">
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
- From="0.5"To="0.2"Duration="0:0:0.04"BeginTime="0:0:0.08" >
- </DoubleAnimation>
- <DoubleAnimationStoryboard.TargetName="Canvas1"
- Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY"
- From="0.5" To="0.2"Duration="0:0:0.04"BeginTime="0:0:0.08">
- </DoubleAnimation>
- </Storyboard>
- </Window.Resources>
- <Window.Triggers>
- <!--设置DW1的事件触发-->
- <!--注意对窗体window的name取为DW1-->
- <EventTriggerSourceName="DW1"RoutedEvent="Window.Loaded">
- <BeginStoryboardName="showQueryCanvasStoryboard2"
- Storyboard="{StaticResource showDW}">
- </BeginStoryboard>
- </EventTrigger>
- <!--设置button1的事件触发-->
- <!--注意对按钮的name取为button1-->
- <EventTriggerSourceName="button1"RoutedEvent="Button.Click">
- <BeginStoryboardName="closeQueryCanvasStoryboard"
- Storyboard="{StaticResource closeDW}">
- </BeginStoryboard>
- </EventTrigger>
- </Window.Triggers>
- <CanvasBackground="Transparent"Height="257"Width="214">
- <CanvasBackground="Brown"Height="236"Width="201"Canvas.Left="9"Canvas.Top="14"Name="Canvas1">
- <!--设置动画-->
- <Canvas.RenderTransform>
- <TransformGroup>
- <ScaleTransformCenterX="100"CenterY="50"></ScaleTransform><!--缩放中心为100,50。RenderTransform.Children[0]-->
- <RotateTransformCenterX="100"CenterY="50"></RotateTransform><!--旋转中心为100,50。RenderTransform.Children[1]-->
- </TransformGroup>
- </Canvas.RenderTransform>
- <ButtonMargin="98,107,124,127"Name="button1" FocusVisualStyle="{DynamicResource MyFocusVisual}"
- Click="button1_Click" Height="28"Width="112"Canvas.Left="-53"Canvas.Top="77">关闭</Button>
- <CheckBoxHeight="31"HorizontalAlignment="Left"Margin="32,25,0,0"Name="checkBox1"VerticalAlignment="Top"Width="75"Canvas.Left="11"Canvas.Top="-1">CheckBox</CheckBox>
- <TextBlockHeight="35"Margin="124,21,34,0"Name="textBlock1"VerticalAlignment="Top"Background="BlueViolet"Text="dfdf"Width="60"Canvas.Left="-73"Canvas.Top="43"/>
- </Canvas>
- </Canvas>
- </Window>
cs代码:
添加引用
- using System.Windows.Threading
- privatevoid button1_Click(object sender, RoutedEventArgs e)
- {
- // this.Close();
- /////timer定义
- tm.Tick += new EventHandler(tm_Tick);
- tm.Interval = TimeSpan.FromSeconds(0.2);
- tm.Start();
- }
- DispatcherTimer tm = new DispatcherTimer();
- void tm_Tick(object sender, EventArgs e)
- { this.Close(); }
其中动画目标为Canvas1,包括:
慢速旋转显示(showDW)、慢速旋转消失(closeDW)
快速旋转显示(showDW1)、快速旋转消失(closeDW1)
快速弹出显示(showDW2)、快速旋转消失(closeDW2)
快速旋转显示(showDW1):
time:0 → 0.2
旋转角度:180→0
透明度: 0→1
放大: X向:0.2→1 Y向:0.2→1
快速弹出显示(showDW2):
time:0 → 0.04 → 0.08 →← 0.12
放大 放大 放大(并复原)
X向:0.2→0.5 0.5→1 1→1.2→1
Y向:0.2→0.5 0.5→1 1→1.2→1
最后在主窗口的
1.MainWindow.xaml里的<Grid>之间放上按钮:
<Grid>
<Button Content="显示子窗口" Height="23" Click="ShowWindow_Click" Margin="114,138,141,138" />
</Grid>
2.MainWindow.xaml.cs里加上相应的按钮事件:
public partial class MainWindow : Window
{
DialogWin myChildWindow = new DialogWin();
public MainWindow()
{
InitializeComponent();
//子窗口事件绑定
myChildWindow.Closed += new EventHandler(myChildWindow_Closed);
}
#region 子窗口事件代码
void myChildWindow_Closed(object sender, EventArgs e)
{
}
private void ShowWindow_Click(object sender, RoutedEventArgs e)
{
myChildWindow.Title = "子窗口信息";
SolidColorBrush(Colors.Yellow);
myChildWindow.Opacity = 0.9;
myChildWindow.Foreground = new SolidColorBrush(Colors.Red);
myChildWindow.FontSize = 14;
myChildWindow.Show();
}
#endregion
}