WPF add a clock to my GUI

只谈情不闲聊 提交于 2019-12-23 11:47:18

问题


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.


回答1:


I found what I needed at: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a813d92b-3d15-4f10-9984-5381517157d4/




回答2:


Have a look at this project over at CodeProject.




回答3:


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




回答4:


You can create an analog clock with The Gauge Library from 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.dll library 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/")

<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.

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:



来源:https://stackoverflow.com/questions/1810780/wpf-add-a-clock-to-my-gui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!