目录
久坐提醒桌面小程序
依赖库
- .Net Framework 4.5
- HandyControl 3.2
网上下的一些小程序,达不到自己想要的效果,要么就是功能复杂,于是选择了自己开发。
采用WPF 基于HandyControl 开发的小程序。
- 有2年不写WPF开发了,练练手
- 平时工作一坐就是半天,眼睛不怎么好了,干眼严重
说明
设置提醒时间,及休息时间
默认50分钟休息10分钟,按整点算(强迫症),如果工作不足10分钟,则将时间加到下一轮中。
取消整点勾选,从开始时计划
久坐提醒倒计时
开始后右下角有小球球,显示倒计时,像360那样的窗口悬停不知道怎么做,没继续研究
休息提醒倒计时
距休息10秒时,窗体会显示到最上层,同时字体颜色变为黄色
休息到计时
从网上找的护眼图片,可以照着练练,再起来走一走。
代码说明
设置界面、倒计时、休息界面,三个页面,通过 VmReminder 进行数据共享
主窗体设置
这是基本框架,参考Office设计,右上解可扩展【设置】、【帮助】功能,可用于其它项目
设置工作时长、休息时长,默认从整点开始计时(有强迫症)
MainWindow.xaml
private void BtnStart_OnClick(object sender, RoutedEventArgs e)
{
_vm.IsHourly = chkHourly.IsChecked.HasValue && chkHourly.IsChecked.Value;
_vm.WorkValue = (int)numWork.Value;
_vm.BreakValue = (int)numBreak.Value;
//开始后,调用倒计时窗体,并将倒计时窗体设为主窗体,否则点拖盘图标,会弹出设置窗体
CountDown countDown = new CountDown();
countDown.DataContext = this._vm;
countDown.InitData();
Application.Current.MainWindow = countDown;
countDown.Show();
this.Close();
}
工作到计时
窗口倒计时时,显示在窗口右下角,
CountDown.xml
托盘图标--放在这个窗体下的原因,是因为 休息转工作时,避免托盘下会出现多个图标
<hc:NotifyIcon x:Name="NotifyIcon" Click="NotifyIconContextContent_OnClick"
Text="久坐提醒"
Visibility="Visible"
Icon="/Application.ico">
<hc:NotifyIcon.ContextMenu>
<ContextMenu>
<MenuItem Click="Settings_OnClick" Header="设置"/>
<MenuItem Click="Exit_OnClick" Header="退出"/>
</ContextMenu>
</hc:NotifyIcon.ContextMenu>
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="Click">
<!--<hc:EventToCommand Command="hc:ControlCommands.PushMainWindow2Top"/>-->
</hc:EventTrigger>
</hc:Interaction.Triggers>
</hc:NotifyIcon>
WPF StringFormat="{}{0:0#}:{1:0#}" 显示倒计时,格式化,如果个位数时,前面补0
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >
<TextBlock HorizontalAlignment="Center" Margin="5" Style="{StaticResource TextBlockLargeBold}" Foreground="White">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:0#}:{1:0#}">
<Binding Path="CountDownWorkMinute" />
<Binding Path="CountDownWorkSecond" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
/// <summary>
/// 窗口显示在右下角
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CountDown_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;
}
通过定时器,工作实现倒计时功能
private void Timer_OnTick(object sender, EventArgs e)
{
Remind();
//倒计时时结束,弹出休息窗口
if (_vm.CountDownWorkMinute == 0 && _vm.CountDownWorkSecond == 0)
{
_timer.Stop();
NotifyIcon.Visibility = Visibility.Hidden;
WorkBreak workBreak = new WorkBreak();
Application.Current.MainWindow = workBreak;
workBreak.DataContext = this._vm;
workBreak.InitData();
workBreak.ShowDialog();
this.Close();
return;
}
//如果分钟大于0,秒=0,那分-1,秒>0 秒-1
if (_vm.CountDownWorkMinute > 0)
{
if (_vm.CountDownWorkSecond > 0)
{
_vm.CountDownWorkSecond--;
}
else
{
_vm.CountDownWorkMinute--;
_vm.CountDownWorkSecond = 60;
}
}
if (_vm.CountDownWorkMinute == 0)
{
_vm.CountDownWorkSecond--;
}
}
休息倒计时
休息窗体显示两护眼张图片和一个休息到计时,按ESC时可取消本次休息,
WorkBreak.xaml
<base:BaseWindow x:Class="VipReminder.Views.WorkBreak"
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:base="clr-namespace:Base.Base"
mc:Ignorable="d" KeyDown="WorkBreak_OnKeyDown"
Title="WorkBreak" Height="450" Width="800">
<DockPanel Background="#00a54f">
<DockPanel Dock="Bottom" >
<TextBlock HorizontalAlignment="Center" Margin="5" Style="{StaticResource TextBlockLargeBold}" Foreground="White">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:0#}:{1:0#}">
<Binding Path="CountDownBreakMinute" />
<Binding Path="CountDownBreakSecond" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DockPanel>
<DockPanel Dock="Left">
<Image Source="/Styles/Images/001.jpg"/>
</DockPanel>
<DockPanel Dock="Right">
<Image Source="/Styles/Images/002.jpg"/>
</DockPanel>
</DockPanel>
</base:BaseWindow>
WorkBreak.xaml.cs
namespace VipReminder.Views
{
/// <summary>
/// WorkBreak.xaml 的交互逻辑
/// </summary>
public partial class WorkBreak
{
// 定时器
private DispatcherTimer _timer;
public VmReminder _vm;
public WorkBreak()
{
InitializeComponent();
WindowState = WindowState.Maximized; //全屏显示
ShowInTaskbar = false; //任务栏不显示图标
Topmost = true; //窗体最前端显示
//this.countDown = countDown; == TODO CountDown 单例
}
private void WorkBreak_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)//Esc键
{
OpenCountDown();
}
}
private void OpenCountDown()
{
CountDown countDown = new CountDown();
Application.Current.MainWindow = countDown;
countDown.DataContext = this._vm;
countDown.InitData();
countDown.Show();
_timer.Stop();
this.Close();
}
public void InitData()
{
_vm = DataContext as VmReminder;
this.ShowInTaskbar = false; //任务栏不显示图标
_vm.CountDownBreakMinute = _vm.BreakValue;
_vm.CountDownBreakSecond = 0;
_timer = new DispatcherTimer();
_timer.Tick += Timer_OnTick;
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Start();
//KeyboardBlocker.off();//锁键盘
}
private void Timer_OnTick(object sender, EventArgs e)
{
//倒计时时结束,弹出休息窗口
if (_vm.CountDownBreakMinute == 0 && _vm.CountDownBreakSecond == 0)
{
_timer.Stop();
OpenCountDown();
return;
}
//如果分钟大于0,秒=0,那分-1,秒>0 减秒
if (_vm.CountDownBreakMinute > 0)
{
if (_vm.CountDownBreakSecond > 0)
{
_vm.CountDownBreakSecond--;
}
else
{
_vm.CountDownBreakMinute--;
_vm.CountDownBreakSecond = 60;
}
}
if (_vm.CountDownBreakMinute == 0)
{
_vm.CountDownBreakSecond--;
}
}
}
}
源码
源码地址:https://gitee.com/VipSoft/Reminder
标签:countDown,C#,vm,倒计时,--,窗体,WPF,久坐,休息 From: https://www.cnblogs.com/vipsoft/p/17603959.html