WPF 中的 7 段数码管时间显示小部件






4.75/5 (7投票s)
WPF 中未来感的语音 7 段数码管时间显示小部件
引言
本文介绍的是一款 7 段数码管时钟小部件,旨在呈现未来感,同时又高效实用。该应用的 UI 使用 WPF 构建,业务逻辑由 C# 处理。应用程序被设计成一种小部件的形式,因此不会占用任务栏。该应用还集成了语音合成器、调度器计时器等更多功能。对于科幻迷和科技爱好者来说,这款应用绝对会令人愉悦。
背景
如上所述,该应用涉及 C# 的许多概念,其中一些我已经写了下来。
调度器计时器 (Dispatcher Timer)
这是 `System.Windows.Threading` 命名空间下的一个典型计时器。它是一个非常有用的组件,可以帮助我们在指定的优先级顺序下,以一定的间隔处理代码片段。以下是从 MSDN 引用的一种调度器计时器的基本示例。
//
// DispatcherTimer setup
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();
//
// System.Windows.Threading.DispatcherTimer.Tick handler
//
// Updates the current seconds display and calls
// InvalidateRequerySuggested on the CommandManager to force
// the Command to raise the CanExecuteChanged event.
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// Updating the Label which displays the current second
lblSeconds.Content = DateTime.Now.Second;
// Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested();
}
语音合成器 (Speech Synthesizer)
这是 C# 在 `System.Speech.Synthesis` 命名空间下提供的另一个简单而有趣的功能。以下是一段从 MSDN 摘录的代码片段。
// Initialize a new instance of the SpeechSynthesizer.
SpeechSynthesizer synth = new SpeechSynthesizer();
// Configure the audio output.
synth.SetOutputToDefaultAudioDevice();
// Speak a string.
synth.Speak("This example demonstrates a basic use of Speech Synthesizer");
双精度动画 (Double Animation)
在 `System.Windows.Media.Animation` 命名空间中,使用指定的 `Duration` 通过线性插值,在两个目标值之间为 `Double` 属性的值设置动画。MSDN 链接。
<DoubleAnimation
Storyboard.TargetName="MyAnimatedRectangle"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="0:0:5"
AutoReverse="True" />
Using the Code
应用的 UI 变量命名设计概述如下。
XAML 前端设计代码 - 下面提供了
<Window x:Class="Futuristic_Seven_Segment_Time_Display.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="150" Width="500"
AllowsTransparency="True" WindowState="Normal"
WindowStyle="None" ResizeMode="NoResize"
WindowStartupLocation="Manual" Background="Transparent"
MouseLeftButtonDown="Window_MouseLeftButtonDown"
MouseRightButtonUp="Window_MouseRightButtonUp"
MouseDoubleClick="Window_MouseDoubleClick"
ShowInTaskbar="False" MouseWheel="Window_MouseWheel">
<Border Name="MainBorder" Background="Black"
CornerRadius="10,10,10,10" Width="450" Height="90"
HorizontalAlignment="Center" VerticalAlignment="Center" >
<Grid Name="ClockGrid" HorizontalAlignment="Center"
VerticalAlignment="Center" >
<Ellipse Name="Nyt_1_Ellipse" Margin="7.5,10,0,0"
Width="30" Height="30" Stroke="Cyan"
StrokeThickness="3" HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Ellipse Name="Nyt_2_Ellipse" Margin="13.5,16,0,0"
Width="18" Height="18" Stroke="Cyan"
StrokeThickness="1.5" HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Ellipse Name="Mrng_1_Ellipse" Margin="7.5,45,0,0"
Width="30" Height="30" Stroke="DarkOrange"
StrokeThickness="3" HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Ellipse Name="Mrng_2_Ellipse" Margin="13.5,51,0,0"
Width="18" Height="18" Stroke="#FFFFA100"
StrokeThickness="1.5" HorizontalAlignment="Left"
VerticalAlignment="Top" />
<Rectangle Name="Clock_Border" Margin="0,0,0,0"
Width="440" Height="85" Fill="#9B000000"
HorizontalAlignment="Left" VerticalAlignment="Top"
RadiusX="10" RadiusY="10" Panel.ZIndex="0" />
<Grid Name="Hours_0" Margin="40,2.5,0,0"
Width="60" Height="83"
HorizontalAlignment="Left" VerticalAlignment="Top" >
<Polyline Name="H_0_P_1" Fill="#FF00F2FF"
Points="10,5,47,5,50,8,47,11,10,11,7,8,10,5"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_0_P_2" Fill="#FF00F2FF"
Points="55,12,55,36,52,39,49,36,49,12,52,9,55,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_0_P_3" Fill="#FF00F2FF"
Points="10,38,47,38,50,41,47,44,10,44,7,41,10,38"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_0_P_4" Fill="#FF00F2FF"
Points="8,12,8,36,5,39,2,36,2,12,5,9,8,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_0_P_5" Fill="#FF00F2FF"
Points="55,46,55,70,52,73,49,70,49,46,52,42,55,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_0_P_6" Fill="#FF00F2FF"
Points="10,71,47,71,50,74,47,78,10,78,7,74,10,71"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_0_P_7" Fill="#FF00F2FF"
Points="8,46,8,70,5,73,2,70,2,46,5,42,8,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
</Grid>
<Grid Name="Hours_1" Margin="102,2.5,0,0"
Width="60" Height="83" HorizontalAlignment="Left"
VerticalAlignment="Top" >
<Polyline Name="H_1_P_1" Fill="#FF00F2FF"
Points="10,5,47,5,50,8,47,11,10,11,7,8,10,5"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_1_P_2" Fill="#FF00F2FF"
Points="55,12,55,36,52,39,49,36,49,12,52,9,55,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_1_P_3" Fill="#FF00F2FF"
Points="10,38,47,38,50,41,47,44,10,44,7,41,10,38"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_1_P_4" Fill="#FF00F2FF"
Points="8,12,8,36,5,39,2,36,2,12,5,9,8,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_1_P_5" Fill="#FF00F2FF"
Points="55,46,55,70,52,73,49,70,49,46,52,42,55,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_1_P_6" Fill="#FF00F2FF"
Points="10,71,47,71,50,74,47,78,10,78,7,74,10,71"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="H_1_P_7" Fill="#FF00F2FF"
Points="8,46,8,70,5,73,2,70,2,46,5,42,8,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
</Grid>
<Grid Name="HMColon" Width="15"
HorizontalAlignment="Left" Margin="163,0,0,0">
<Rectangle Name="HM_1_Colon" Margin="0,35,0,0"
Fill="Cyan" Height="7" Width="7"
Stroke="Cyan" HorizontalAlignment="Center"
VerticalAlignment="Center" ></Rectangle>
<Rectangle Name="HM_2_Colon" Margin="0,-25,0,0"
Fill="Cyan" Height="7" Width="7"
Stroke="Cyan" HorizontalAlignment="Center"
VerticalAlignment="Center" ></Rectangle>
</Grid>
<Grid Name="Minutes_0" Margin="180,2.5,0,0"
Width="60" Height="83" HorizontalAlignment="Left" VerticalAlignment="Top" >
<Polyline Name="M_0_P_1" Fill="#FF00F2FF"
Points="10,5,47,5,50,8,47,11,10,11,7,8,10,5"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_0_P_2" Fill="#FF00F2FF"
Points="55,12,55,36,52,39,49,36,49,12,52,9,55,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_0_P_3" Fill="#FF00F2FF"
Points="10,38,47,38,50,41,47,44,10,44,7,41,10,38"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_0_P_4" Fill="#FF00F2FF"
Points="8,12,8,36,5,39,2,36,2,12,5,9,8,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_0_P_5" Fill="#FF00F2FF"
Points="55,46,55,70,52,73,49,70,49,46,52,42,55,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_0_P_6" Fill="#FF00F2FF"
Points="10,71,47,71,50,74,47,78,10,78,7,74,10,71"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_0_P_7" Fill="#FF00F2FF"
Points="8,46,8,70,5,73,2,70,2,46,5,42,8,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
</Grid>
<Grid Name="Minutes_1" Margin="240,2.5,0,0"
Width="60" Height="83"
HorizontalAlignment="Left" VerticalAlignment="Top" >
<Polyline Name="M_1_P_1" Fill="#FF00F2FF"
Points="10,5,47,5,50,8,47,11,10,11,7,8,10,5"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_1_P_2" Fill="#FF00F2FF"
Points="55,12,55,36,52,39,49,36,49,12,52,9,55,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_1_P_3" Fill="#FF00F2FF"
Points="10,38,47,38,50,41,47,44,10,44,7,41,10,38"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_1_P_4" Fill="#FF00F2FF"
Points="8,12,8,36,5,39,2,36,2,12,5,9,8,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_1_P_5" Fill="#FF00F2FF"
Points="55,46,55,70,52,73,49,70,49,46,52,42,55,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_1_P_6" Fill="#FF00F2FF"
Points="10,71,47,71,50,74,47,78,10,78,7,74,10,71"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="M_1_P_7" Fill="#FF00F2FF"
Points="8,46,8,70,5,73,2,70,2,46,5,42,8,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
</Grid>
<Grid Name="MSColon" Width="15"
HorizontalAlignment="Left" Margin="302,0,0,0">
<Rectangle Name="MS_1_Colon" Margin="0,35,0,0"
Fill="Cyan" Height="7" Width="7"
Stroke="Cyan" HorizontalAlignment="Center"
VerticalAlignment="Center" ></Rectangle>
<Rectangle Name="MS_2_Colon" Margin="0,-25,0,0"
Fill="Cyan" Height="7" Width="7"
Stroke="Cyan" HorizontalAlignment="Center"
VerticalAlignment="Center" ></Rectangle>
</Grid>
<Grid Name="Seconds_0" Margin="318,2.5,0,0"
Width="60" Height="83" HorizontalAlignment="Left"
VerticalAlignment="Top" >
<Polyline Name="S_0_P_1" Fill="#FF00F2FF"
Points="10,5,47,5,50,8,47,11,10,11,7,8,10,5"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_0_P_2" Fill="#FF00F2FF"
Points="55,12,55,36,52,39,49,36,49,12,52,9,55,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_0_P_3" Fill="#FF00F2FF"
Points="10,38,47,38,50,41,47,44,10,44,7,41,10,38"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_0_P_4" Fill="#FF00F2FF"
Points="8,12,8,36,5,39,2,36,2,12,5,9,8,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_0_P_5" Fill="#FF00F2FF"
Points="55,46,55,70,52,73,49,70,49,46,52,42,55,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_0_P_6" Fill="#FF00F2FF"
Points="10,71,47,71,50,74,47,78,10,78,7,74,10,71"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_0_P_7" Fill="#FF00F2FF"
Points="8,46,8,70,5,73,2,70,2,46,5,42,8,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
</Grid>
<Grid Name="Seconds_1" Margin="380,2.5,0,0"
Width="60" Height="83" HorizontalAlignment="Left"
VerticalAlignment="Top" >
<Polyline Name="S_1_P_1" Fill="#FF00F2FF"
Points="10,5,47,5,50,8,47,11,10,11,7,8,10,5"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_1_P_2" Fill="#FF00F2FF"
Points="55,12,55,36,52,39,49,36,49,12,52,9,55,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_1_P_3" Fill="#FF00F2FF"
Points="10,38,47,38,50,41,47,44,10,44,7,41,10,38"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_1_P_4" Fill="#FF00F2FF"
Points="8,12,8,36,5,39,2,36,2,12,5,9,8,12"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_1_P_5" Fill="#FF00F2FF"
Points="55,46,55,70,52,73,49,70,49,46,52,42,55,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_1_P_6" Fill="#FF00F2FF"
Points="10,71,47,71,50,74,47,78,10,78,7,74,10,71"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
<Polyline Name="S_1_P_7" Fill="#FF00F2FF"
Points="8,46,8,70,5,73,2,70,2,46,5,42,8,46"
Stroke="Cyan" StrokeThickness="0.2" ></Polyline>
</Grid>
</Grid>
</Border>
</Window>
常规方法
Set_StartUP_Location
- 包含指定应用程序启动位置的代码
private void Set_StartUP_Location()
{
this.Left = SystemParameters.FullPrimaryScreenWidth - this.Width - 20;
this.Top = 20;
}
Initialize_NotifyIcon
- 包含使应用程序在隐藏图标面板中可用的通知图标的代码(应用程序不会出现在任务栏)。
private void Initialize_NotifyIcon()
{
System.Windows.Forms.MenuItem[] NotifyMenu = new System.Windows.Forms.MenuItem[]
{ new System.Windows.Forms.MenuItem("About", new System.EventHandler(About_Show)),
new System.Windows.Forms.MenuItem("Exit", new System.EventHandler(Close_Application)) };
System.Windows.Forms.ContextMenu ClickMenu = new System.Windows.Forms.ContextMenu(NotifyMenu);
this.NotifyWidget.Icon = Futuristic_Seven_Segment_Time_Display.Properties.Resources._7SegIcon_ico;
this.NotifyWidget.ContextMenu = ClickMenu;
this.NotifyWidget.Visible = true;
this.NotifyWidget.Click += new EventHandler(NotifyWidget_Click);
}
Initiate_Hour_Digits
、Initiate_Minutes_Digits
& Initiate_Seconds_Digits
- 此方法以 12 小时格式初始化当前时间的 UI。在此方法中,通过将位置值(小时、分钟、秒)和时间值传递给 `PassControls()` 方法,根据时间值使用相应的颜色绘制位置(小时、分钟、秒)。
private void Initiate_Hour_Digits()
{
Hours = DateTime.Now.Hour.ToString();
//Converting time from 24 hours format to 12 hours format
if (int.Parse(Hours) > 12)
{
Hours = (int.Parse(Hours) - 12).ToString();
}
Hrs = Hours.ToCharArray();
if (int.Parse(Hours) > 9)
{
PassControls("H_0_P_1", Hrs[0]);
PassControls("H_1_P_1", Hrs[1]);
}
else
{
PassControls("H_1_P_1", Hrs[0]);
//if the Minutes value is less than 9, initiating first Hours digit to '0'
PassControls("H_0_P_1", '0');
}
}
private void Initiate_Minutes_Digits()
{
Minutes = DateTime.Now.Minute.ToString();
Mins = Minutes.ToCharArray();
if (int.Parse(Minutes) > 9)
{
PassControls("M_0_P_1", Mins[0]);
PassControls("M_1_P_1", Mins[1]);
}
else
{
PassControls("M_1_P_1", Mins[0]);
//if the Minutes value is less than 9, initiating first Minutes digit to '0'
PassControls("M_0_P_1", '0');
}
}
private void Initiate_Seconds_Digits()
{
Seconds = DateTime.Now.Second.ToString();
Secs = Seconds.ToCharArray();
if (int.Parse(Seconds) > 9)
{
PassControls("S_0_P_1", Secs[0]);
PassControls("S_1_P_1", Secs[1]);
}
else
{
//if the Minutes value is less than 9, initiating first Seconds digit to '0'
PassControls("S_0_P_1", '0');
PassControls("S_1_P_1", Secs[0]);
}
}
Dispatcher_Timer
- 与初始化方法类似,此方法每秒也将值发送到 `PassControls()` 方法。
private void Dispatcher_Timer()
{
DispatcherTimer Sys_timer = new DispatcherTimer
(new TimeSpan(0, 0, 0, 1), DispatcherPriority.Normal, delegate
{
Seconds = DateTime.Now.Second.ToString();
Secs = Seconds.ToCharArray();
Minutes = DateTime.Now.Minute.ToString();
Mins = Minutes.ToCharArray();
Hours = DateTime.Now.Hour.ToString();
if (int.Parse(Hours) > 12)
{
Hours = (int.Parse(Hours) - 12).ToString();
}
Hrs = Hours.ToCharArray();
if (int.Parse(Seconds) > 9)
{
//To reduce lines of execution
//Control enters the below IF block only if the 2nd Digit of the Seconds value reaches 0.
//Only by then the 1st Digit of the Seconds value experience a change
if (Secs[1] == '0')
{
PassControls("S_0_P_1", Secs[0]);
}
PassControls("S_1_P_1", Secs[1]);
}
else
{
//To reduce lines of execution
//Control enters the below IF block only if the absolute Seconds value is 0.
//Only by then the Minutes value experience a change
if (Secs[0] == '0')
{
if (int.Parse(Minutes) > 9)
{
//To reduce lines of execution
//Control enters the below IF block only
//if the 2nd Digit of the Minutes value reaches 0.
//Only by then the 1st Digit of the Minutes value experience a change
if (Mins[1] == '0')
{
PassControls("M_0_P_1", Mins[0]);
}
PassControls("M_1_P_1", Mins[1]);
}
else
{
//To reduce lines of execution
//Control enters the below IF block only if the absolute Minutes value is 0.
//Only by then the Hours value experience a change
if (Mins[0] == '0')
{
if (int.Parse(Hours) > 9)
{
PassControls("H_0_P_1", Hrs[0]);
PassControls("H_1_P_1", Hrs[1]);
}
else
{
PassControls("H_1_P_1", Hrs[0]);
PassControls("H_0_P_1", '0');
}
}
PassControls("M_1_P_1", Mins[0]);
PassControls("M_0_P_1", '0');
}
}
PassControls("S_1_P_1", Secs[0]);
PassControls("S_0_P_1", '0');
}
}, this.Dispatcher);
}
PassControls
- 此方法接收位置值及其对应的时间值并分别绘制。为了减少代码行数,位置值被分配给新的折线 UI 变量。
public void PassControls(string Place_Value, char Time_Value)
{
Polyline x1;
Polyline x2;
Polyline x3;
Polyline x4;
Polyline x5;
Polyline x6;
Polyline x7;
if (Place_Value == "S_0_P_1")
{
x1 = S_0_P_1 as Polyline;
x2 = S_0_P_2 as Polyline;
x3 = S_0_P_3 as Polyline;
x4 = S_0_P_4 as Polyline;
x5 = S_0_P_5 as Polyline;
x6 = S_0_P_6 as Polyline;
x7 = S_0_P_7 as Polyline;
}
else if (Place_Value == "S_1_P_1")
{
x1 = S_1_P_1 as Polyline;
x2 = S_1_P_2 as Polyline;
x3 = S_1_P_3 as Polyline;
x4 = S_1_P_4 as Polyline;
x5 = S_1_P_5 as Polyline;
x6 = S_1_P_6 as Polyline;
x7 = S_1_P_7 as Polyline;
}
else if (Place_Value == "M_0_P_1")
{
x1 = M_0_P_1 as Polyline;
x2 = M_0_P_2 as Polyline;
x3 = M_0_P_3 as Polyline;
x4 = M_0_P_4 as Polyline;
x5 = M_0_P_5 as Polyline;
x6 = M_0_P_6 as Polyline;
x7 = M_0_P_7 as Polyline;
}
else if (Place_Value == "M_1_P_1")
{
x1 = M_1_P_1 as Polyline;
x2 = M_1_P_2 as Polyline;
x3 = M_1_P_3 as Polyline;
x4 = M_1_P_4 as Polyline;
x5 = M_1_P_5 as Polyline;
x6 = M_1_P_6 as Polyline;
x7 = M_1_P_7 as Polyline;
}
else if (Place_Value == "H_0_P_1")
{
x1 = H_0_P_1 as Polyline;
x2 = H_0_P_2 as Polyline;
x3 = H_0_P_3 as Polyline;
x4 = H_0_P_4 as Polyline;
x5 = H_0_P_5 as Polyline;
x6 = H_0_P_6 as Polyline;
x7 = H_0_P_7 as Polyline;
}
else
{
x1 = H_1_P_1 as Polyline;
x2 = H_1_P_2 as Polyline;
x3 = H_1_P_3 as Polyline;
x4 = H_1_P_4 as Polyline;
x5 = H_1_P_5 as Polyline;
x6 = H_1_P_6 as Polyline;
x7 = H_1_P_7 as Polyline;
}
if (Time_Value == '1')
{
x1.Fill = SegmentDisabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentDisabledColor_Brush;
x4.Fill = SegmentDisabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentDisabledColor_Brush;
x7.Fill = SegmentDisabledColor_Brush;
}
else if (Time_Value == '2')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentDisabledColor_Brush;
x5.Fill = SegmentDisabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentEnabledColor_Brush;
}
else if (Time_Value == '3')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentDisabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentDisabledColor_Brush;
}
else if (Time_Value == '4')
{
x1.Fill = SegmentDisabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentEnabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentDisabledColor_Brush;
x7.Fill = SegmentDisabledColor_Brush;
}
else if (Time_Value == '5')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentDisabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentEnabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentDisabledColor_Brush;
}
else if (Time_Value == '6')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentDisabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentEnabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentEnabledColor_Brush;
}
else if (Time_Value == '7')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentDisabledColor_Brush;
x4.Fill = SegmentDisabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentDisabledColor_Brush;
x7.Fill = SegmentDisabledColor_Brush;
}
else if (Time_Value == '8')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentEnabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentEnabledColor_Brush;
}
else if (Time_Value == '9')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentEnabledColor_Brush;
x4.Fill = SegmentEnabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentDisabledColor_Brush;
}
else if (Time_Value == '0')
{
x1.Fill = SegmentEnabledColor_Brush;
x2.Fill = SegmentEnabledColor_Brush;
x3.Fill = SegmentDisabledColor_Brush;
x4.Fill = SegmentEnabledColor_Brush;
x5.Fill = SegmentEnabledColor_Brush;
x6.Fill = SegmentEnabledColor_Brush;
x7.Fill = SegmentEnabledColor_Brush;
}
}
事件处理程序
NotifyWidget_Click
- 隐藏图标托盘中通知图标的单击事件。如果应用程序被隐藏或位于其他窗口后面,此方法会将应用程序带到屏幕前面。
void NotifyWidget_Click(object sender, System.EventArgs e)
{
//if the App is Minimized/Hidden and need to be Maximized or make available
if (MainBorder.Opacity == 0)
{
DoubleAnimation DA_Show_Widget = new DoubleAnimation
(1, new Duration(new TimeSpan(0, 0, 0, 0, 450)));
MainBorder.BeginAnimation(Border.OpacityProperty, DA_Show_Widget);
}
//if the App is behind the current window and need to be the Active Window
else
{
DoubleAnimation DA_Show_Widget = new DoubleAnimation
(0,1, new Duration(new TimeSpan(0, 0, 0, 0, 450)));
MainBorder.BeginAnimation(Border.OpacityProperty, DA_Show_Widget);
this.Activate();
}
}
Window_MouseLeftButtonDown
- 当您单击并拖动鼠标左键时,会触发此事件。这将小部件的位置更改为当前光标所在的位置。
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//Move the Entire Window to the Cursor Point
this.DragMove();
}
Window_MouseRightButtonUp
- 当释放鼠标右键时,会触发此事件。此方法会隐藏小部件,给您一种它被最小化的感觉。
private void Window_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (MainBorder.Opacity == 1)
{
DoubleAnimation DA_Hide_Widget = new DoubleAnimation
(0, new Duration(new TimeSpan(0, 0, 0, 0, 450)));
MainBorder.BeginAnimation(Border.OpacityProperty, DA_Hide_Widget);
}
}
Window_MouseDoubleClick
- 当双击小部件窗口时,会触发此事件。它会以 24 小时格式说出当前时间。
private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
string Speak_String = "Hi The Time is " +
DateTime.Now.Hour.ToString() + " " + DateTime.Now.Minute.ToString();
synthesizer.Volume = 100;
synthesizer.Rate = -1;
synthesizer.SpeakAsync(Speak_String);
}
Window_MouseWheel
- 当小部件是活动窗口且鼠标滚动时,会触发此事件。如果向上滚动鼠标,小部件的不透明度会增加;如果向下滚动鼠标,小部件的不透明度会降低。从而在下面的图像所示的两个背景之间切换。
private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0)
{
MainBorder.Background = Grid_Enabled_Color_Brush;
}
else
{
MainBorder.Background = Grid_Disabled_Color_Brush;
}
}
关注点
Dispatcher_Timer
和 PassControls
方法以最大化性能的方式编码,以使小部件与实时值同步。整个应用程序没有使用外部图像。甚至通知图标也是使用 Visual Studio 图标文件完成的。同样,通过使用鼠标事件,应用程序执行了 3 多个功能,避免了包含按钮或其他控件。
历史
- 初始版本发布于 2015/7/19