WPF向我的GUI添加时钟

问题描述 投票:5回答:4

简单请求-我希望能够在WPF应用程序窗口中显示当前时间。那里有免费的控件吗?只需要显示时间,别无其他。

wpf time clock
4个回答

3
投票

在CodeProject上查看this project


2
投票

您可以具有标签或文本块,并将其内容绑定到System.DateTime.Now


0
投票

您可以使用Gauge Library中的Arction创建一个模拟时钟。该库不是开源的,但它是免费的,因此没有问题。从其站点下载库(通过请求每个电子邮件的链接),然后将Arction.WPF.Gauges.dll库添加到您的项目中。然后将仪表控件添加到窗口。 (不要忘记添加xmlns:agc="http://www.arction.com/gauges/"行)

<Window x:Class="ClockDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:agc="http://www.arction.com/gauges/" 
        xmlns:local="clr-namespace:ClockDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="400">
    <Grid>

        <agc:Gauge Name="Clock"/>

    </Grid>
</Window>

然后,您只需要一些代码来创建一个Timer,即可处理时钟的每一秒以及时钟的样式/刻度。

using System;
using System.Timers;
using Arction.Gauges;
using Arction.Gauges.Dials;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ClockDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //window title
            this.Title = "VDWWD Clock Demo";    

            //create the clock
            CreateClock();

            //create the timer
            var timer = new Timer()
            {
                Interval = 1000,
                Enabled = true
            };
            timer.Elapsed += new ElapsedEventHandler(timer_Tick);
        }


        private void CreateClock()
        {
            //declare some variables
            var ClockColor = new SolidColorBrush(Color.FromRgb(86, 104, 149));
            var ClockArcColor = new SolidColorBrush(Color.FromRgb(40, 44, 65));
            var ClockFontSize = 30;
            var ClockSize = 300;

            var DialColorHours = Color.FromRgb(131, 146, 187);
            var DialColorMinutes = Color.FromRgb(131, 146, 187);

            var LabelColorHours = Brushes.White;
            var LabelColorMinutes = Brushes.White;
            var LabelColorSeconds = Brushes.White;
            var FontFamily = new FontFamily("Arial");

            //some clock default stuff
            Clock.Fill = ClockColor;
            Clock.FontSize = ClockFontSize;
            Clock.Height = ClockSize;
            Clock.Stroke = ClockArcColor;
            Clock.StrokeThickness = 5;
            Clock.Width = ClockSize;

            //create the hour scales
            var Hours = new Scale()
            {
                AngleBegin = 60,
                AngleEnd = 60,
                ArcStrokeThickness = 2,
                ArcStroke = ClockArcColor,
                DialColor1 = DialColorHours,
                DialLengthFactor = 0.6,
                DialShape = DialShape.DefaultNeedle,
                Label = new TextBlock(),
                MajorTickCount = 12,
                MinorTickCount = 0,
                RangeBegin = 1,
                RangeEnd = 13,
                ValueIndicatorDistance = -99999,
                MajorTicks = new MajorTicksLine()
                {
                    FontFamily = FontFamily,
                    FontWeight = FontWeights.Bold,
                    LabelBrush = LabelColorHours,
                    LabelOffset = -12,
                    OffsetA = -5,
                    TickStroke = LabelColorHours,
                }
            };

            //create the minute scales
            var Minutes = new Scale()
            {
                AngleBegin = 90,
                AngleEnd = 90,
                DialColor1 = DialColorMinutes,
                DialLengthFactor = 0.8,
                DialShape = DialShape.DefaultNeedle,
                MajorTickCount = 60,
                MinorTickCount = 0,
                RangeBegin = 0,
                RangeEnd = 60,
                Theme = ScaleThemeEnum.None,
                MajorTicks = new MajorTicksLine()
                {
                    LabelBrush = LabelColorMinutes,
                    LabelsEnabled = false,
                    OffsetA = -4,
                    OffsetB = -2,
                    TickStroke = LabelColorMinutes,
                    TickThickness = 2
                }
            };

            //create the second scales
            var Seconds = new Scale()
            {
                AngleBegin = 90,
                AngleEnd = 90,
                DialLengthFactor = 0.8,
                DialShape = DialShape.Line,
                MajorTickCount = 60,
                MinorTickCount = 0,
                RangeBegin = 0,
                RangeEnd = 60,
                Theme = ScaleThemeEnum.None,
                MajorTicks = new MajorTicksLine()
                {
                    LabelsEnabled = false,
                    OffsetA = -4,
                    OffsetB = -2,
                    TickStroke = LabelColorSeconds,
                    TickThickness = 2
                }
            };

            //add the scales to the clock
            Clock.PrimaryScale = Hours;
            Clock.SecondaryScales.Add(Minutes);
            Clock.SecondaryScales.Add(Seconds);

            //set the current time
            Clock.PrimaryScale.Value = DateTime.Now.Hour;
            Clock.SecondaryScales[0].Value = DateTime.Now.Minute;
            Clock.SecondaryScales[1].Value = DateTime.Now.Second;
        }


        private void timer_Tick(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                Clock.PrimaryScale.Value = DateTime.Now.Hour;
                Clock.SecondaryScales[0].Value = DateTime.Now.Minute;
                Clock.SecondaryScales[1].Value = DateTime.Now.Second;
            });
        }
    }
}

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.