WPF 向我的 GUI 添加时钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1810780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:16:47  来源:igfitidea点击:

WPF add a clock to my GUI

wpftimeclock

提问by Unknown Coder

Simple Request - I want to be able to display the current time within my WPF application window. Are there free controls out there for this? Just need to display the time, nothing else.

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

回答by André Hauptfleisch

Have a look at this projectover at CodeProject.

在 CodeProject 上查看这个项目

回答by gprasant

You could have a label or a text block and have its content bound to System.DateTime.Now

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

回答by VDWWD

You can create an analog clock with The Gauge Libraryfrom Arction. The library is not open source but it is free, so no problems there. Download the library from their site (by requesting a link per email) and add the Arction.WPF.Gauges.dlllibrary to your project. Then add the Gauge Control to a Window. (Don't forget to add the line xmlns:agc="http://www.arction.com/gauges/")

你可以创建一个模拟时钟计图书馆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>

Then all you need is some code to create a Timer that will handle each second of the clock and the styling/scales of the clock.

然后,您只需要一些代码来创建一个 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;
            });
        }
    }
}

Result:

结果:

enter image description here

在此处输入图片说明