數字時鐘DigClock
首先建立數字顯示類:
using System;
using System.Drawing;
namespace CsDev
{
class SevenSegmentDispay
{
Graphics grph;
//0,1,2,3,4,5,6,7,8,9
static byte[,] bySegment = { { 1, 1, 1, 0, 1, 1,1 }, { 0, 0, 1, 0, 0,1, 0 },
{1,0,1,1,1,0,1},{1,0,1,1,0,1,1},
{0,1,1,1,0,1,0},{1,1,0,1,0,1,1},
{1,1,0,1,1,1,1},{1,0,1,0,0,1,0},
{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
readonly Point[][] apt = new Point[7][];
public SevenSegmentDispay(Graphics grph)
{
this.grph = grph;
apt[0] = new Point[] { new Point(3, 2), new Point(39, 2), new Point(31, 10), new Point(11, 10) };
apt[1] = new Point[] { new Point(2, 3), new Point(10, 11), new Point(10, 31), new Point(2, 35) };
apt[2] = new Point[] { new Point(40, 3), new Point(40, 35), new Point(32, 31), new Point(32, 11) };
apt[3] = new Point[] { new Point(3, 36), new Point(11, 32), new Point(31, 32), new Point(39, 36),
new Point(31, 40),new Point(11, 40)};
apt[4] = new Point[] { new Point(2, 37), new Point(10, 41), new Point(10, 61), new Point(2, 69) };
apt[5] = new Point[] { new Point(40, 37), new Point(40, 69), new Point(32, 61), new Point(32, 41) };
apt[6] = new Point[] { new Point(11, 62), new Point(31, 62), new Point(39, 70), new Point(3, 70) };
}
public SizeF MeasureString(string str, Font font)
{
SizeF sizef = new SizeF(0, grph.DpiX * font.SizeInPoints / 72);
for (int i = 0; i < str.Length; i++)
{
if (Char.IsDigit(str[i]))
sizef.Width += 42 * grph.DpiX * font.SizeInPoints / 72 / 72;
else if (str[i] == ':')
sizef.Width += 12 * grph.DpiX * font.SizeInPoints / 72 / 72;
}
return sizef;
}
void Fill(Point[] apt, Font font, Brush brush, float x, float y)
{
PointF[] aptf = new PointF[apt.Length];
for (int i = 0; i < apt.Length; i++)
{
aptf[i].X = x + apt[i].X * grph.DpiX * font.SizeInPoints / 72 / 72;
aptf[i].Y = y + apt[i].Y * grph.DpiY * font.SizeInPoints / 72 / 72;
}
grph.FillPolygon(brush, aptf);
}
float Number(int num, Font font, Brush brush, float x, float y)
{
for (int i = 0; i < apt.Length; i++)
if (bySegment[num, i] == 1)
Fill(apt[i], font, brush, x, y);
return x + 42 * grph.DpiX * font.SizeInPoints / 72 / 72;
}
float Colon(Font font, Brush brush, float x, float y)//畫冒號
{
Point[][] apt = new Point[2][];
apt[0] = new Point[] { new Point(2, 21), new Point(6, 17), new Point(10, 21), new Point(6, 25) };
apt[1] = new Point[] { new Point(2, 51), new Point(6, 47), new Point(10, 51), new Point(6, 55) };
for (int i = 0; i < apt.Length; i++)
Fill(apt[i], font, brush, x, y);
return x + 12 * grph.DpiX * font.SizeInPoints / 72 / 72;
}
public void DrawString(string str, Font font, Brush brush, float x, float y)
{
for (int i = 0; i < str.Length; i++)
{
if (Char.IsDigit(str[i]))
x = Number(str[i] - '0', font, brush, x, y);
else if (str[i] == ':')
x = Colon(font, brush, x, y);
}
}
}
}
實現類:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;
namespace CsDev
{
class DigClock:Form
{
DateTime dt;
public static void Main()
{
Application.Run(new DigClock());
}
public DigClock()
{
Text = "數字時鐘";
ResizeRedraw = true;
MinimumSize = SystemInformation.MinimumWindowSize + new Size(0, 1);
dt = DateTime.Now;
Timer timer = new Timer();
timer.Tick+=new EventHandler(timer_Tick);
timer.Interval = 100;
timer.Enabled = true;
}
void timer_Tick(object obj, EventArgs e)
{
DateTime dtNow = DateTime.Now;
dtNow = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, dtNow.Hour, dtNow.Minute, dtNow.Second);
if (dt != dtNow)
{
dt = dtNow;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
SevenSegmentDispay ssd = new SevenSegmentDispay(e.Graphics);
string strTime = dt.ToString("T",DateTimeFormatInfo.InvariantInfo);
SizeF sizef = ssd.MeasureString(strTime, Font);
float fScale = Math.Min(ClientSize.Width/sizef.Width,ClientSize.Height/sizef.Height);
Font font = new Font(Font.FontFamily, fScale * Font.SizeInPoints);
sizef = ssd.MeasureString(strTime, font);//注意font是自定義
ssd.DrawString(strTime, font, Brushes.Red, (ClientSize.Width-sizef.Width)/2,(ClientSize.Height-sizef.Height)/2);
base.OnPaint(e);
}
}
}
效果圖:
相關文章
- 【JS】javascript實現頁面顯示數字時鐘JSJavaScript
- windows10桌面數字時鐘的新增方法Windows
- JavaScript 原生 小案例 秒殺時鐘倒數計時JavaScript
- JavaScript 天小時分鐘和秒倒數計時JavaScript
- win10怎樣在桌面顯示鐘錶_win10電腦桌面顯示數字時鐘怎麼操作Win10
- LikeLib:形成可信數字生態,重塑數字新時代
- 系統時鐘與硬體時鐘
- 600 字每分鐘
- 簡黑時鐘 AClock 2.8 中文版 ,一款全屏翻頁時鐘/倒數計時/番茄鍾
- input 限制字數輸入時候 限制字數會出現負數
- 實時時鐘、系統時鐘和主機伺服器時鐘的區別伺服器
- 【數字化】重新思考數字化時代的製造業
- 如何使用原生技術寫一個倒數計時時鐘
- 洞察時尚行業,數字化時代行業
- Clock——時鐘
- 數字化時代,如何運用數字營銷為企業加成
- 三分鐘介紹數字展廳的製作流程
- Windows10系統如何將純數字時間顯示設定成傳統鐘錶樣式Windows
- 網頁時鐘網頁
- 中國銀髮群體數字生活洞察:數字時代的“慢”人群
- 標準與數字化轉型:數字時代的良好治理手冊
- CSS動畫篇之炫酷時鐘之時鐘牆CSS動畫
- 數字化運營是做什麼的?一分鐘看懂
- 後疫情時代,數字中臺成為數字化轉型必選項
- js中在將字串數字轉為數字時,使用哪種方式更安全JS字串
- 數字經濟時代:AI+引領企業數字化新高度AI
- 數字化時代,商家如何運用數字化營銷在618狂歡?
- 《金融時報》:現金在數字時代仍具魅力
- 在數字化轉型時代下,現在是入局智慧數字經營的好時機嗎?
- 時鐘——CSS 動畫CSS動畫
- DS1302時鐘
- 運動的時鐘
- A.T. Kearney:數字時代的競爭
- 數字化時代的市場營銷
- 企業要邁入數字化時代
- 數字時代,智慧數字經營成實體行業發展助推器?行業
- 數字經濟時代,合理運用好數字化轉型工具,降低經營成本
- 數字經濟時代,企業如何更好實現數字化轉型發展
- 簡黑時鐘AClock 2.3 Mac (Mac自定義全屏時鐘軟體)Mac