luckapple2

wpf纯xaml写的clock

12.26

    这是今天参照wpf程序指南利用xaml写的clock,而且运行起来不错.不过有个缺点就是要在xaml中设计初始时间.如过为3:19就要把BeginTime设计为<Storyboard BeginTime="-3:19">.如没设计默认为从十二点开始.xaml代码如下:

<Window x:Class="Clock.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <FrameworkElement x:Key="dt" Tag="{x:Static s:DateTime.Now}"/>
        <TransformGroup x:Key="angleHour">
            <TranslateTransform X="{Binding Source={StaticResource dt},Path=Tag.Hour}"
                                Y="{Binding Source={StaticResource dt},Path=Tag.Minute}"/>
            <MatrixTransform Matrix="30 0 0.5 1 0 0"/>
        </TransformGroup>
        <TransformGroup x:Key="angleMinute">
        <TranslateTransform X="{Binding Source={StaticResource dt},Path=Tag.Minute}"
                                Y="{Binding Source={StaticResource dt},Path=Tag.Second}"/>
            <MatrixTransform Matrix="6 0 0.1 1 0 0"/>
            </TransformGroup>
        <TransformGroup x:Key="angleSecond">
            <ScaleTransform ScaleX="{Binding Source={StaticResource dt},Path=Tag.Second}"/>
            <ScaleTransform ScaleX="6"/>
           
       </TransformGroup>
        <Style TargetType="{x:Type Path}">
            <Setter Property="Stroke" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
            <Setter Property="StrokeThickness" Value="2"/>
            <Setter Property="StrokeStartLineCap" Value="Round"/>
            <Setter Property="StrokeEndLineCap" Value="Round"/>
            <Setter Property="StrokeLineJoin" Value="Round"/>
            <Setter Property="StrokeDashCap" Value="Round"/>
        </Style>
    </Window.Resources>
    <Viewbox>
        <Canvas Width="200" Height="200">
            <Canvas.RenderTransform>
                <TranslateTransform X="100" Y="100"/>
               
            </Canvas.RenderTransform>
            <Path Data="M 0 -90 A 90 90 0 1 1 -0.01 -90" StrokeDashArray="0 3.14159" StrokeThickness="3"/>
            <Path Data="M 0 -90 A 90 90 0 1 1 -0.01 -90" StrokeDashArray="0 7.854" StrokeThickness="6"/>
            <Path Data="M 0 -60 C 0 -30, 20 -30,5 -20 L 5 0 C -20 -30,0 -30 0 -60"
                 Fill="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
                <Path.RenderTransform>
                    <RotateTransform x:Name="xformHour" Angle="{Binding Source={StaticResource angleHour},Path=Value.offsetX}"/>
                </Path.RenderTransform>
            </Path>
            <Path Data="M 0 -80 C 0 -75,0 -70,2.5 -60 L 2.5 0 C 2.5 5, -2.5 5, -2.5 0 L -2.5 -60 C 0 -70,0 -75,0 -80"
                  Fill="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}">
                <Path.RenderTransform>
                    <RotateTransform x:Name="xformMinute" Angle="{Binding Source={StaticResource angleMinute},Path=Value.offsetX}"/>
                </Path.RenderTransform>
            </Path>
            <Path Data="M 0 10 L 0 -80">
                <Path.RenderTransform>
                    <RotateTransform x:Name="xformSecond" Angle="{Binding Source={StaticResource angleSecond},Path=Value.Mll}"/>
                   
                </Path.RenderTransform>
            </Path>
        </Canvas>
    </Viewbox>
    <Window.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
                <Storyboard BeginTime="-3:19">
                    <DoubleAnimation Storyboard.TargetName="xformHour" Storyboard.TargetProperty="Angle"
                      From="0" To="360" Duration="12:0:0" IsAdditive="True" RepeatBehavior="Forever"/>
                    <DoubleAnimation Storyboard.TargetName="xformMinute" Storyboard.TargetProperty="Angle"
                      From="0" To="360" Duration="1:0:0" IsAdditive="True" RepeatBehavior="Forever"/>
                    <DoubleAnimation Storyboard.TargetName="xformSecond" Storyboard.TargetProperty="Angle"
                      From="0" To="360" Duration="0:1:0" IsAdditive="True" RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
</Window>

posted on 2008-12-26 23:49  luckapple2  阅读(706)  评论(0编辑  收藏  举报

导航