WPF定时器

不知道怎么写,就用渐变边框带着旋转来模拟动态  
 
 
xaml代码:
   <Border  BorderThickness="4" Width="60" Height="60" Grid.Row="0"   CornerRadius="33,33,33,33" Margin="0,295,0,0" Name="PART_Background">
                <Border.BorderBrush>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <LinearGradientBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                                <SkewTransform CenterY="0.5" CenterX="0.5"/>
                                <RotateTransform Angle="90" CenterY="0.5" CenterX="0.5"/>

                            </TransformGroup>
                        </LinearGradientBrush.RelativeTransform>
                        <GradientStop Color="blue" Offset="0.016"/>
                        <GradientStop Color="#FF0000" Offset="0.016"/>
                    </LinearGradientBrush>
                </Border.BorderBrush>
                <Border.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <LinearGradientBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                                <SkewTransform CenterY="0.5" CenterX="0.5"/>
                                <RotateTransform Angle="20" CenterY="0.5" CenterX="0.5"/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </LinearGradientBrush.RelativeTransform>
                        <GradientStop Color="Transparent" Offset="0"/>
                        <GradientStop Color="Transparent" Offset="0.5"/>
                        <GradientStop Color="Transparent" Offset="0"/>
                    </LinearGradientBrush>
                </Border.Background>
                <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Border>

 

c#代码:
  DispatcherTimer t = new DispatcherTimer();
     
        public void times()
        {
            //设置两个GradientStop  第一个是已经变色的,第二个是等待变色的
            GradientStop gd = new GradientStop();
            gd.Color = Color.FromRgb(255, 0, 0);
            GradientStop gd2 = new GradientStop();
            gd2.Color = Color.FromRgb(0, 0, 255);
            //设置两个渐变的停止点
            gd.Offset = 0.00;
            gd2.Offset = 0.01;
            //设置绘制区域
            LinearGradientBrush lgb = new LinearGradientBrush();
            //设置起始和终止坐标
            lgb.StartPoint = new Point(0.5, 0);
            lgb.EndPoint = new Point(0.5, 1);
            //旋转
            RotateTransform rotate = new RotateTransform(0, 0.5, 0.5);
            //计时器
            t.Tick += new EventHandler((object sender, EventArgs e) =>
            {
                if (360 - rotate.Angle != 0)
                {
                    la1.Content = (360 - (int)rotate.Angle) / 6;
                }
                else
                {
                    la1.Content = 0;
                }
                //每次加6°
                rotate.Angle += 6;
                //旋转区域赋值给它
                lgb.RelativeTransform = rotate;
                //每次加0.016
                gd.Offset += 0.01666666;
                gd2.Offset += 0.01666666;
                lgb.GradientStops = new GradientStopCollection() { gd, gd2 };
                PART_Background.BorderBrush = lgb;
                //60s end
                if (rotate.Angle == 360)
                {
                    la1.Content = 0;
                    t.Stop();
                    MessageBox.Show("结束");
                }
            });
            t.Interval = new TimeSpan(0, 0, 0, 1);
            t.Start();
        }

效果图:

 

 

新手  欢迎指点

posted @ 2020-09-17 11:41  AetlySaber  阅读(387)  评论(0)    收藏  举报