⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ledclockctrl.cs

📁 基于wince的LED时钟 c#实现,达到和电子表一样的效果
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Consomen
{

    public enum ColonType { Circular, Rectangular };
    public enum ClockType { DigitalClock, StopWatch, CountDown, Freeze };
    public enum DigitalColor { RedColor, BlueColor, GreenColor };
    // Clock format. For 12 hour format display 'A' (AM) or 'P' (PM)
    public enum ClockFormat { TwentyFourHourFormat, TwelveHourFormat };


    public partial class LEDClockCtrl : UserControl
    {
        private Color digitsColor = Color.Red; // color of the digits displayed
        private Color countdownColor = Color.White; // background color after count down expires
        private ClockFormat clockDisplayFormat = ClockFormat.TwelveHourFormat;

        private DigitalDisplay[] DigitDisplay = null; // panels on which digits are displayed
        private DigitalDisplay[] ColonDisplay = null; // panels for displaying colons
        private DigitalDisplay AmPmDisplay = null; // panel for AM/PM display
        private DigitalDisplay MicroSecDisplay = null; // panel for displaying 1/10 of a second

        // ShowTimer to refresh the time display
        private System.Windows.Forms.Timer ShowTimer;
        // ColonTimer to blink the colons between the digits
        private System.Windows.Forms.Timer ColonTimer;
        // type of clock to display (a normal clock, stop watch or count down)
        private ClockType clockType = ClockType.DigitalClock;

        // date time used to display stopwatch, count begins from this variable
        private DateTime stopwatchBegin = DateTime.Now;
        // count down in milli seconds, default of 10 seconds
        int countDownMilliSeconds = 10000;
        // whenever count down starts this time is set to Now + countDownMilliSeconds
        private DateTime countDownTo;

        // currently displayed numbers on the clock, useful to freeze
        int hour, min, sec, ms;
        char am_pm;

        // delegates called when the count down is finished
        public delegate void CountDown();
        public event CountDown CountDownDone = null;

        // delegates called when an alarm is set
        public delegate void Alarm();
        public event Alarm RaiseAlarm = null;
        private ArrayList AlarmTimes = new ArrayList();

        // graphics surface for the control on which the clock is displayed
        private static Graphics graphics;

        Bitmap bitmap;
        Bitmap bufferImg;

        public DigitalClockCtrl()
        {
            InitializeComponent();

            this.ShowTimer = new System.Windows.Forms.Timer();
            this.ColonTimer = new System.Windows.Forms.Timer();
            // 
            // ShowTimer
            // 
            this.ShowTimer.Tick += new System.EventHandler(this.OnClockTimer);
            // 
            // ColonTimer
            // 
            this.ColonTimer.Tick += new System.EventHandler(this.OnColonTimer);

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);

            bitmap = new Bitmap(Width, Height);
            bufferImg = new Bitmap(Width, Height);
            graphics = Graphics.FromImage(bitmap);
            PreparePanels();
            ShowTimer.Interval = 100;
            ShowTimer.Enabled = true;  // digits are refreshed on timer count
            ColonTimer.Interval = 1000;
            ColonTimer.Enabled = true; // this will blink the colon

            // adding the resize handler here so that it will be called
            // only after graphics variable is created
            this.Resize += new System.EventHandler(this.OnResize);
        }

        // set count down time in milli seconds
        public int CountDownTime
        {
            get { return countDownMilliSeconds; }
            set
            {
                if (value < 1000)
                    MessageBox.Show("Count down time cannot be less than 1000", "Error");
                else
                    countDownMilliSeconds = value;
            }
        }

        // set the alarm time
        public DateTime AlarmTime
        {
            set
            {
                if (value < DateTime.Now)
                    MessageBox.Show("Alarm time cannot be earlier.", "Error");
                else
                    AlarmTimes.Add(value);
            }
        }

        // set the display format, 12 Hr or 24 Hr
        public ClockFormat ClockDisplayFormat
        {
            set { this.clockDisplayFormat = value; }
        }

        // setting clock type
        // DigitalClock and StopWatch will automatically start the clock
        // For CountDown the number of seconds should be set before calling this property
        public ClockType SetClockType
        {
            get { return clockType; }
            set
            {
                clockType = value;
                switch (clockType)
                {
                    case ClockType.StopWatch:
                        stopwatchBegin = DateTime.Now; // start stopwatch clock
                        break;
                    case ClockType.CountDown:
                        countDownTo = DateTime.Now.AddMilliseconds(countDownMilliSeconds);
                        break;
                }
            }
        }

        // set the color in which the digits are displayed
        public DigitalColor SetDigitalColor
        {
            set
            {
                this.Invalidate();
                DigitalDisplay.SetPenColor(value);
            }
        }

        // OnPaint - called when regions of clock are invalidated
        private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            lock (this)
            {
//                 Graphics g = Graphics.FromImage(bufferImg);
//                 g.Clear(BackColor);
                DisplayTime(graphics);

                e.Graphics.DrawImage(bitmap,0,0);
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        // This function is aware which panel should display what number
        // and also the colons and PM and AM displays
        private bool blink = false; // toggle for blinking effect of colons
        private void DisplayTime(Graphics g)
        {
            DateTime dt = DateTime.Now;
            if (clockType != ClockType.Freeze)
            {
                hour = dt.Hour;
                min = dt.Minute;
                sec = dt.Second;
                ms = dt.Millisecond;
                am_pm = ' ';
            }
            TimeSpan ts = TimeSpan.Zero;

            // check if alarms are set, raise them
            for (int i = 0; i < AlarmTimes.Count; i++)
            {
                if (dt > (DateTime)AlarmTimes[i] && RaiseAlarm != null)
                {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -