📜  wpf 时钟控制 - C# (1)

📅  最后修改于: 2023-12-03 14:48:34.870000             🧑  作者: Mango

WPF时钟控制 - C#

WPF时钟控制是一种在WPF应用程序中创建可视化时钟的方法。这个控件可以用来显示当前时间,或者是用来模拟动画效果。

实现步骤
1. 创建WPF应用程序

首先我们需要创建一个WPF应用程序。这可以通过Visual Studio或手动创建XAML和C#文件来完成。

2. 添加时钟控件

在XAML中添加一个Canvas控件,并在其中添加时钟控件。代码如下:

<Canvas>
    <Ellipse Canvas.Left="50" Canvas.Top="50" Width="200" Height="200" Stroke="Black" StrokeThickness="4" />
    <Line X1="150" Y1="100" X2="150" Y2="50" Stroke="Black" StrokeThickness="3" />
    <Line X1="150" Y1="150" X2="150" Y2="100" Stroke="Black" StrokeThickness="3" />
    <Rectangle Canvas.Left="145" Canvas.Top="60" Width="10" Height="90" Fill="Black"></Rectangle>
    <Rectangle Canvas.Left="60" Canvas.Top="145" Width="90" Height="10" Fill="Black"></Rectangle>
    <Ellipse Canvas.Left="145" Canvas.Top="145" Width="10" Height="10" Fill="Black"></Ellipse>
    <Line X1="150" Y1="150" X2="120" Y2="120" Stroke="Black" StrokeThickness="3" />
    <Line X1="150" Y1="150" X2="180" Y2="120" Stroke="Black" StrokeThickness="3" />
    <Line X1="150" Y1="75" X2="120" Y2="100" Stroke="Black" StrokeThickness="2" />
    <Line X1="150" Y1="75" X2="180" Y2="100" Stroke="Black" StrokeThickness="2" />
    <Line X1="150" Y1="175" X2="120" Y2="150" Stroke="Black" StrokeThickness="2" />
    <Line X1="150" Y1="175" X2="180" Y2="150" Stroke="Black" StrokeThickness="2" />
</Canvas>

这段代码添加了一个简单的时钟UI。现在打开应用程序,看看时钟是否正常显示。

3. 设计时钟控制器

在C#中添加一个Timer控制器,并在每秒钟更新时钟UI。代码如下:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(1);
    timer.Tick += timer_Tick;
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    double hours = DateTime.Now.Hour % 12;
    double minutes = DateTime.Now.Minute;
    double seconds = DateTime.Now.Second;

    double hourAngle = (hours * 30) + (minutes / 2);
    double minuteAngle = (minutes * 6);
    double secondAngle = (seconds * 6);

    HourRotation.Angle = hourAngle;
    MinuteRotation.Angle = minuteAngle;
    SecondRotation.Angle = secondAngle;
}

这段代码创建了一个DispatcherTimer,每秒钟触发一次Tick事件。在这个事件处理程序中,我们获取当前时间,更新旋转角度,并将它们分配给HourRotation、MinuteRotation和SecondRotation。

4. 完整代码

最终的代码如下所示:

<Window x:Class="WpfClock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF时钟控制" Height="300" Width="300"
        Loaded="Window_Loaded">
    <Canvas>
        <Ellipse Canvas.Left="50" Canvas.Top="50" Width="200" Height="200" Stroke="Black" StrokeThickness="4" />
        <Line X1="150" Y1="100" X2="150" Y2="50" Stroke="Black" StrokeThickness="3" />
        <Line X1="150" Y1="150" X2="150" Y2="100" Stroke="Black" StrokeThickness="3" />
        <Rectangle Canvas.Left="145" Canvas.Top="60" Width="10" Height="90" Fill="Black"></Rectangle>
        <Rectangle Canvas.Left="60" Canvas.Top="145" Width="90" Height="10" Fill="Black"></Rectangle>
        <Ellipse Canvas.Left="145" Canvas.Top="145" Width="10" Height="10" Fill="Black"></Ellipse>
        <Line X1="150" Y1="150" X2="120" Y2="120" Stroke="Black" StrokeThickness="3" />
        <Line X1="150" Y1="150" X2="180" Y2="120" Stroke="Black" StrokeThickness="3" />
        <Line X1="150" Y1="75" X2="120" Y2="100" Stroke="Black" StrokeThickness="2" />
        <Line X1="150" Y1="75" X2="180" Y2="100" Stroke="Black" StrokeThickness="2" />
        <Line X1="150" Y1="175" X2="120" Y2="150" Stroke="Black" StrokeThickness="2" />
        <Line X1="150" Y1="175" X2="180" Y2="150" Stroke="Black" StrokeThickness="2" />

        <Line X1="150" Y1="50" X2="150" Y2="90" Stroke="Black" StrokeThickness="4" RenderTransformOrigin="0.5,0.5">
            <Line.RenderTransform>
                <RotateTransform x:Name="HourRotation" />
            </Line.RenderTransform>
        </Line>

        <Line X1="150" Y1="50" X2="150" Y2="120" Stroke="Black" StrokeThickness="2" RenderTransformOrigin="0.5,0.5">
            <Line.RenderTransform>
                <RotateTransform x:Name="MinuteRotation" />
            </Line.RenderTransform>
        </Line>

        <Line X1="150" Y1="50" X2="150" Y2="140" Stroke="Red" StrokeThickness="1" RenderTransformOrigin="0.5,0.5">
            <Line.RenderTransform>
                <RotateTransform x:Name="SecondRotation" />
            </Line.RenderTransform>
        </Line>
    </Canvas>
</Window>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += timer_Tick;
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        double hours = DateTime.Now.Hour % 12;
        double minutes = DateTime.Now.Minute;
        double seconds = DateTime.Now.Second;

        double hourAngle = (hours * 30) + (minutes / 2);
        double minuteAngle = (minutes * 6);
        double secondAngle = (seconds * 6);

        HourRotation.Angle = hourAngle;
        MinuteRotation.Angle = minuteAngle;
        SecondRotation.Angle = secondAngle;
    }
}
结论

WPF时钟控制是一种在WPF应用程序中创建可视化时钟的方法,它可以用来展示当前时间,或是用来模拟动画效果。使用WPF时钟控制,我们可以轻松构建自己的时钟控件,并为其添加各种可视化效果。