13.C#编程学习——定时器

107 阅读1分钟

13.C#编程学习——定时器

示例源码

usingSystem;

usingSystem.Drawing;

usingSystem.Windows.Forms;

 

classSimpleClock : Form

{

    publicstaticvoid Main()

    {

        Application.Run(newSimpleClock());

    }

    public SimpleClock()

    {

        Text = "Simple Clock";

        BackColor = SystemColors.Window;

        ForeColor = SystemColors.WindowText;

 

        Timer timer = newTimer();

        timer.Tick += newEventHandler(TimerOnTick);

        timer.Interval = 1000;

        timer.Start();

    }

    privatevoid TimerOnTick(object sender, EventArgs ea)

    {

        Invalidate();

    }

    protectedoverridevoid OnPaint(PaintEventArgs pea)

    {

        StringFormat strfmt = newStringFormat();

        strfmt.Alignment = StringAlignment.Center;

        strfmt.LineAlignment = StringAlignment.Center;

 

        pea.Graphics.DrawString(DateTime.Now.ToString("F"),

                                Font, newSolidBrush(ForeColor),

                               ClientRectangle, strfmt);

    }

}