C# Windows 程序设计中代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace CsStudy
{
class SimpleClock:Form
{
public static void Main()
{
Application.Run(new SimpleClock());
}
public SimpleClock()
{
Text = "简单时钟";
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(TimerOnTick);
timer.Start();
}
void TimerOnTick(object obj, EventArgs e)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics grph=e.Graphics;
string strTime=DateTime.Now.ToString("F");
SizeF sizef = grph.MeasureString(strTime, Font);
float f = Math.Min(ClientSize.Width / sizef.Width, ClientSize.Height / sizef.Height);
Font font = new Font(Font.FontFamily, f * Font.SizeInPoints);
sizef = grph.MeasureString(strTime, font);
grph.DrawString(strTime, font, new SolidBrush(Color.Red),
( ClientSize.Width - sizef.Width)/2,( ClientSize.Height - sizef.Height)/2);
base.OnPaint(e);
}
}
}
效果图: