简单请求 - 我希望能够在WPF应用程序窗口中显示当前时间。那里有免费控制吗?只需要显示时间,别无其他。
答案 0 :(得分:5)
我找到了我需要的内容:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a813d92b-3d15-4f10-9984-5381517157d4/
答案 1 :(得分:3)
在CodeProject上查看this project。
答案 2 :(得分:2)
您可以拥有标签或文本块,并将其内容绑定到System.DateTime.Now
答案 3 :(得分: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;
});
}
}
}
结果: