日期:2014-05-18  浏览次数:20963 次

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); }
        se