WPF前端:时间控件(1)时钟

这篇博客介绍了一个使用WPF实现的数字时钟控件。作者通过XAML定义了时钟的布局,包括时、分、秒针及时间标签,并在后台代码中使用System.Timers.Timer定时更新指针角度和时间显示,实现了实时同步系统时间的功能。此外,博客还提醒读者注意时钟背景刻度的问题,需要自行替换正确的背景图片。
摘要由CSDN通过智能技术生成

时钟

在这里插入图片描述
TClockView.xaml


    <Label Name="lab商标" Foreground="White" Margin="0,0,0,211" HorizontalAlignment="Center" VerticalAlignment="Bottom" Height="Auto" Width="Auto" FontSize="13" >JackMoon</Label>
    <Label Name="lab创建时间" Foreground="White" Margin="0,91,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Height="Auto" Width="Auto">1987</Label>
    <!-- 秒针定义  -->
    <Rectangle Margin="0.2,0,0,9" Name="rectangleSecond" Stroke="White" Height="16" VerticalAlignment="Bottom" Width="1">
        <Rectangle.RenderTransform>
            <RotateTransform x:Name="secondPointer" CenterX="0" CenterY="0" Angle="0" />
        </Rectangle.RenderTransform>
    </Rectangle>
    <!-- -->

    <!-- 分钟定义  -->
    <Rectangle Margin="0,0,0,0" Name="rectangleMinute" Height="12" Stroke="LightGreen" Width="1">
        <Rectangle.RenderTransform>
            <RotateTransform x:Name="minutePointer" CenterX="0" CenterY="9" Angle="45" />
        </Rectangle.RenderTransform>
    </Rectangle>
    <!-- -->

    <!-- 时针定义  -->
    <Rectangle Margin="1,0,0,4" Name="rectangleHour" Height="8" Stroke="red" Width="1">
        <Rectangle.RenderTransform>
            <RotateTransform x:Name="hourPointer" CenterX="1" CenterY="7" Angle="90" />
        </Rectangle.RenderTransform>
    </Rectangle>
    <!---->
    <Label Content="08:08:08" FontSize="16" Foreground="White" Height="Auto" HorizontalAlignment="Center" Margin="114,0,113,86" Name="labTime" VerticalAlignment="Bottom" Width="Auto" />
</Grid>
</UserControl>

注意:时分秒的位置调整,还是需要根据自己打开后效果为准
(这个时钟的背景图片刻度有问题,大家自己找一个正确的哈~)

TClockView.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace CableMonitor.UserControls
{
using System.Windows.Threading;

/// <summary>
/// TClockView.xaml 的交互逻辑
/// </summary>
public partial class TClockView : UserControl
{
    //计时器
    System.Timers.Timer timer = new System.Timers.Timer(1000);
    public TClockView()
    {
        InitializeComponent();

        #region 初始化时间
        secondPointer.Angle = DateTime.Now.Second * 6;
        minutePointer.Angle = DateTime.Now.Minute * 6;
        hourPointer.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5);
        this.labTime.Content = DateTime.Now.ToString("HH:mm:ss");

        #endregion
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;
        timer.Start();
    }

    //private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    //{
    //    //进行拖放移动
    //    this.DragMove();
    //}
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //UI异步更新
        this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
        {
            //秒针转动,秒针绕一圈360度,共60秒,所以1秒转动6度
            secondPointer.Angle = DateTime.Now.Second * 6;
            //分针转动,分针绕一圈360度,共60分,所以1分转动6度
            minutePointer.Angle = DateTime.Now.Minute * 6;
            //时针转动,时针绕一圈360度,共12时,所以1时转动30度。
            // 另外同一个小时内,随着分钟数的变化(绕一圈60分钟),时针也在缓慢变化(转动30度,30 / 60 = 0.5)
            hourPointer.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5);
            //更新时间值
            this.labTime.Content = DateTime.Now.ToString("HH:mm:ss");
        }));
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {

    }
}}

主界面

<StackPanel Grid.ColumnSpan="1" Grid.Column="1" Margin="35,7,0,7" Orientation="Vertical" HorizontalAlignment="Left">
                    <localCon:TClockView  Width="50" Height="50"/>
                </StackPanel>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值