读书人

WPF+blend 怎么制作 这种效果的倒计时

发布时间: 2012-05-05 17:21:10 作者: rapoo

WPF+blend 如何制作 这种效果的倒计时器!


用WPF写代码还是Blend里画? Blend里怎么画呢? = = 求高手。。。。。

[解决办法]
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
x:Class="WpfApplication12.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(ed:Arc.EndAngle)" Storyboard.TargetName="arc">
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
</EventTrigger>
</Window.Triggers>

<Grid x:Name="LayoutRoot">
<Ellipse Fill="#FFF7F7F7" Margin="83,80,251,72" Stroke="Black"/>
<ed:Arc x:Name="arc" ArcThickness="1" ArcThicknessUnit="Percent" EndAngle="0" Fill="#FF8F1D1D" Margin="112,109,279,96" Stretch="None" Stroke="Black" StartAngle="0"/>
</Grid>
</Window>
[解决办法]

C# code
public class Pie : Shape {    protected override System.Windows.Media.Geometry DefiningGeometry {        get {            StreamGeometry geometry = new StreamGeometry();            geometry.FillRule = FillRule.EvenOdd;            using (StreamGeometryContext context = geometry.Open()) {                DrawGeometry(context);            }            geometry.Freeze();            return geometry;        }    }    private void DrawGeometry(StreamGeometryContext context) {        var startPoint = new Point(CenterX, CenterY);        var endPoint = GetEndPoint();        context.BeginFigure(startPoint, true, true);        context.LineTo(new Point(CenterX, CenterY - Radius), true, true);        context.ArcTo(endPoint, new Size(Radius, Radius), 0, Angle > 180, SweepDirection.Clockwise, true, true);    }    private Point GetEndPoint() {        Point result = new Point();        double a = Angle / 180.0f * Math.PI;        result.X = CenterX + Radius * Math.Sin(a);        result.Y = CenterY - Radius * Math.Cos(a);        return result;    }    protected override void OnRender(DrawingContext dc) {        //dc.DrawRectangle(Brushes.Yellow, null, new Rect(RenderSize));        base.OnRender(dc);    }    public double Radius {        get { return (double)GetValue(RadiusProperty); }        set { SetValue(RadiusProperty, value); }    }    public static readonly DependencyProperty RadiusProperty =        DependencyProperty.Register("Radius", typeof(double), typeof(Pie), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));    public double Angle {        get { return (double)GetValue(AngleProperty); }        set { SetValue(AngleProperty, value); }    }    public static readonly DependencyProperty AngleProperty =        DependencyProperty.Register("Angle", typeof(double), typeof(Pie), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));    public double CenterX {        get { return (double)GetValue(CenterXProperty); }        set { SetValue(CenterXProperty, value); }    }    public static readonly DependencyProperty CenterXProperty =        DependencyProperty.Register("CenterX", typeof(double), typeof(Pie), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));    public double CenterY {        get { return (double)GetValue(CenterYProperty); }        set { SetValue(CenterYProperty, value); }    }    public static readonly DependencyProperty CenterYProperty =        DependencyProperty.Register("CenterY", typeof(double), typeof(Pie), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));}[code=XML]<Window x:Class="Test21.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local="clr-namespace:Test21"        Title="MainWindow" Height="350" Width="525">    <Grid>        <Ellipse Width="100" Height="100" Stroke="Black" />        <local:Pie x:Name="pie" Angle="0" Fill="Yellow" HorizontalAlignment="Center" Width="100" Height="100" VerticalAlignment="Center" Stroke="Red" Radius="50" CenterX="50" CenterY="50" />    </Grid>    <Window.Triggers>        <EventTrigger RoutedEvent="Window.Loaded">            <BeginStoryboard>                <Storyboard>                    <DoubleAnimation Storyboard.TargetName="pie" Storyboard.TargetProperty="Angle" To="360" RepeatBehavior="Forever" />                </Storyboard>            </BeginStoryboard>        </EventTrigger>    </Window.Triggers></Window> 

读书人网 >C#

热点推荐