[纪念]C#控制台彩色时钟源码

这是C#第一次作业时写的程序,复制粘贴后可直接运行。


using System;

using System.Collections.Generic;

using System.Text;

 

using System.Threading;

 

namespace MyClock

{

    class Clock

    {

        static void Main(string[] args)

        {

            Console.Title = "电子时钟";  //设置窗口标题和长度

            Console.WindowHeight = 40;

            Timer timer = new Timer();

            timer.Start();

 

        }

    }

 

    class Timer

    {

 

        private System.DateTime currentTime;

 

        private int year;

        private int month;

        private int day;

        private int hour;

        private int mintue;

        private int second;

 

 

 

 

        public Timer()

        {

            currentTime = new System.DateTime();

        }

 

        /*

         * 获取当前系统时间

         *

         */

        private void getCurTime()

        {

            currentTime = System.DateTime.Now;

 

            year = currentTime.Year;

            month = currentTime.Month;

            day = currentTime.Day;

            hour = currentTime.Hour;

            mintue = currentTime.Minute;

            second = currentTime.Second;

        }

 

 

        /*

         * 开启线程

         *

         */

        public void Start()

        {

 

            Thread timeThread = new Thread(new ThreadStart(Process));

 

            timeThread.Start();

 

            Console.CursorVisible = false;

 

            //按下任意键退出程序

            Console.ReadKey(true);

 

            timeThread.Abort();

 

        }

 

 

        /*

         * 处理线程

         *

         */

        private void Process()

        {

            while (true)

            {

 

                getCurTime();

 

                Display();

 

                Thread.Sleep(1000);

 

            }

 

        }

 

        /*        

         * 显示信息

         *

         */

        private void Display()

        {    /*

              * 输出日期信息

              *

              */

            int pos = 17;

            int beteen = 2;

 

            Console.SetCursorPosition(33, 4);

            Console.ForegroundColor = ConsoleColor.Blue;

            Console.WriteLine(currentTime.Year + "" + currentTime.Month + "" + currentTime.Day + "");

 

            DisplayNum(hour / 10, pos, 8);

            DisplayNum(hour % 10, pos + 6 + beteen, 8);

 

            Console.SetCursorPosition(pos + 13 + beteen + 1, 9);

            Console.WriteLine("*");

            Console.SetCursorPosition(pos + 13 + beteen + 1, 11);

            Console.WriteLine("*");

 

            DisplayNum(mintue / 10, pos + 16 + beteen, 8);

            DisplayNum(mintue % 10, pos + 24 + beteen, 8);

 

            Console.SetCursorPosition(pos + 30 + beteen, 9);

            Console.WriteLine("*");

            Console.SetCursorPosition(pos + 30 + beteen, 11);

            Console.WriteLine("*");

 

            DisplayNum(second / 10, pos + 33 + beteen, 8);

            DisplayNum(second % 10, pos + 39 + beteen + 1, 8);

 

            /*输出日历*/

            DisplayCalender();

 

 

 

            Console.ForegroundColor = ConsoleColor.Red;

            Console.SetCursorPosition(0, 32);

            Console.WriteLine("按下任意键结束. .  .");

 

        }

        private void DisplayNum(int num, int x, int y)

        {

            Console.ForegroundColor = ConsoleColor.DarkBlue;

            string[][] digital ={new string[]{"▇▇▇"," ","  "," ","▇▇▇"},

                                 new string[]{"    ","    ","    ", "    ","    "},

                                 new string[]{"▇▇▇","   ","▇▇▇","    ","▇▇▇"},

                                 new string[]{"▇▇▇","   ","▇▇▇","   ","▇▇▇"},

                                 new string[]{" "," ","▇▇▇" ,"   ","    "},

                                 new string[]{"▇▇▇","    ","▇▇▇","   ","▇▇▇"},

                                 new string[]{"▇▇▇","    ","▇▇▇"," ","▇▇▇"},

                                 new string[]{"▇▇▇","   ","    ","   ","    "},

                                 new string[]{"▇▇▇"," ","▇▇▇"," ","▇▇▇"},

                                 new string[]{"▇▇▇"," ","▇▇▇","   ","▇▇▇"},

                                };

 

            for (int i = 0; i <= 9; i++)

            {

                if (i == num)

                {

                    for (int j = 0; j <= 4; j++)

                    {

                        Console.SetCursorPosition(x, y + j);

                        Console.WriteLine(digital[i][j]);

                    }

                    break;

                }

            };

 

 

        }

 

        private void DisplayCalender()

        {

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.SetCursorPosition(18, 15);

            Console.WriteLine("Sun    Mon    Tue    Wed    Thr    Fri    Sat");

 

            int space = 5;//日历中相邻两个数字的间距

            int num_long = 2;//日历日期数字的宽度

 

            DateTime today = new DateTime();//指向今天

            DateTime pos = new DateTime();//用于移动的

 

            int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);//获取当前月份的天数       

 

            today = DateTime.Now;

            pos = today.AddDays(1 - today.Day);

            //Console.WriteLine(pos.DayOfWeek);

 

 

            int C_x = 18, C_y = 17;//C_x表示此刻的横坐标,C_y表示此刻的纵坐标

 

            C_x += (pos.DayOfWeek - DayOfWeek.Monday + 1) * (space + num_long) + 1;//设置好C_x的初始位置

 

            for (int day = 1; day <= days; day++)

            {

                Console.ForegroundColor = ConsoleColor.Green;

                if (pos == today)

                    Console.ForegroundColor = ConsoleColor.Red;

                Console.SetCursorPosition(C_x, C_y);

                Console.Write(pos.Day);

                if (pos.DayOfWeek == DayOfWeek.Saturday)  //如果此刻的日期是这一行的最后一列则换行

                {

                    C_x = 19;

                    C_y += 1;

                }

                else                                      //否则继续向后书写

                {

                    C_x += (space + num_long);

                }

                pos = pos.AddDays(1);

            }

        }

    }

 

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值