c# 控制台电子时钟源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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);
}
}
}
}。