C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > WPF桌面程序

基于WPF编写有趣的久坐提醒桌面小程序

作者:VipSoft

这篇文章主要为大家详细介绍了如何基于WPF编写一个有趣的久坐提醒桌面小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以动手尝试一下

久坐提醒桌面小程序: 干这行职业病比较多,之前用爱丽(即:玻璃酸钠滴眼液),用的时候挺舒服,缓解吧,不过治标不治本。注意休息,加强锻炼非常有必要,这周开始每工作1小时,休息10分钟(程序中有锁键盘鼠标的功能,那个太狠了,万一领导要东西电脑锁住了尴尬了,被我注释了),看看远方,绿值,站个桩,或者高架马步,马步扎得好,到老疾病少,不过注意别把膝盖搞坏了,我玩得狠现在有积液了 

依赖库

网上下的一些小程序,达不到自己想要的效果,要么就是功能复杂,于是选择了自己开发。
采用WPF 基于HandyControl 开发的小程序。

说明

设置提醒时间,及休息时间

默认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/releases/download/V1.0/Reminder.zip

源码地址:https://gitee.com/VipSoft/Reminder

到此这篇关于基于WPF编写有趣的久坐提醒桌面小程序的文章就介绍到这了,更多相关WPF桌面程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文