zoukankan      html  css  js  c++  java
  • WPF计时器

    using System;
    using System.Windows;
    using System.Windows.Threading;
    
    namespace WpfStopWatch
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            /// <summary>
            /// 状态
            /// </summary>
            enum State
            {
                Start,
                Pause,
                End
            }
    
            /// <summary>
            /// 状态
            /// </summary>
            State _state = State.End;
    
            /// <summary>
            /// 计时用
            /// </summary>
            TimeSpan _timeSpan = new TimeSpan(0,0,0,0,0);
    
            public MainWindow()
            {
                InitializeComponent();
    
                var t = new DispatcherTimer();
                t.Interval = new TimeSpan(0,0,0,1);
                t.Tick += OnTimer;
                t.IsEnabled = true;
                t.Start();
            }
    
            /// <summary>
            /// 时钟回调
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void OnTimer(object sender, EventArgs e)
            {
                switch (_state)
                {
                        case State.Start:
                        {
                            _timeSpan += new TimeSpan(0,0,0,1);
                        }break;
                        case State.Pause:
                        {
                            
                        }
                        break;
                        case State.End:
                        {
                            _timeSpan = new TimeSpan();
                            //_timeSpan = new TimeSpan(0, 23, 12, 45, 54);
                        }
                        break;
                }
    
                var time = string.Format("{0:D2}:{1:D2}:{2:D2}", _timeSpan.Hours, _timeSpan.Minutes, _timeSpan.Seconds);
                textBox1.Text = time;
            }
    
            /// <summary>
            /// 退出
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button4_Click(object sender, RoutedEventArgs e)
            {
                Close();
            }
    
            /// <summary>
            /// 开始
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                _state = State.Start;
            }
            /// <summary>
            /// 暂停
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                _state = State.Pause;
            }
            /// <summary>
            /// 停止
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button3_Click(object sender, RoutedEventArgs e)
            {
                _state = State.End;
            }
        }
    }
    

  • 相关阅读:
    每一次要fix的pr
    docker && k8s
    beisen
    c++迭代器失效问题
    视频分析
    视频分析记录
    深信服实习生笔试题-20190315
    laravel接口设计
    tensorflow利用预训练模型进行目标检测(四):检测中的精度问题以及evaluation
    tensorflow利用预训练模型进行目标检测(三):将检测结果存入mysql数据库
  • 原文地址:https://www.cnblogs.com/simonryan/p/3678581.html
Copyright © 2011-2022 走看看