clock.cs
来自「《c#技术内幕代码》」· CS 代码 · 共 53 行
CS
53 行
namespace Ch9_7
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Globalization;
using System.Timers;
/// <summary>
/// Summary description for Clock.
/// </summary>
///
public class Clock : System.Windows.Forms.Control
{
private System.Timers.Timer aTimer;
private void InitializeComponent ()
{
}
public Clock()
{
aTimer = new System.Timers.Timer();
aTimer.Tick+=new EventHandler(OnTimedEvent);
// Set the Interval to 1 second.
aTimer.Interval=100;
aTimer.Enabled=true;
}
public void OnTimedEvent(object source, EventArgs e)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
// Get the current time
DateTime dt = DateTime.Now;
// Format it as a string
string t = dt.ToShortTimeString();
// And display it
StringFormat style = new StringFormat();
style.Alignment = StringAlignment.Center;
pe.Graphics.DrawString(t, Font, new SolidBrush (ForeColor), ClientRectangle, style);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?