前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[纪念]C#控制台彩色时钟源码

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

作者头像
陈黎栋
发布2020-02-18 09:37:34
6710
发布2020-02-18 09:37:34
举报
文章被收录于专栏:陈黎栋的专栏啦

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

代码语言:javascript
复制
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);
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档