.NET 绘制钟表


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 判断系统当前是星期几
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void We(object sender, EventArgs e)
        {
            string dt, week = "0";//定义两个变量
            dt = DateTime.Today.DayOfWeek.ToString();//把系统星期赋予dt
            switch (dt)//判断
            {
                case "Sunday":
                    week = "星期日";
                    break;
                case "Monday":
                    week = "星期一";
                    break;
                case "Tuesday":
                    week = "星期二";
                    break;
                case "Wednesday":
                    week = "星期三";
                    break;
                case "Thursday":
                    week = "星期四";
                    break;
                case "Friday":
                    week = "星期五";
                    break;
                case "Saturday":
                    week = "星期六";
                    break;
            }
            textBox1.Text = week;
        }
        //定义三个变量
        float i = 270, j = 270, k = 270;
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Refresh();
            Graphics gra = this.CreateGraphics();//创建一个画板
            /*********************************/
            //表盘上的指针
            SolidBrush sb = new SolidBrush(Color.Blue);
            Font f = new Font("宋体", 18);
            gra.DrawString("1", f, sb, 195, 20);
            gra.DrawString("2", f, sb, 235, 60);
            gra.DrawString("3", f, sb, 250, 123);
            gra.DrawString("4", f, sb, 235, 183);
            gra.DrawString("5", f, sb, 195, 225);
            gra.DrawString("6", f, sb, 125, 245);
            gra.DrawString("7", f, sb, 55, 225);
            gra.DrawString("8", f, sb, 15, 183);
            gra.DrawString("9", f, sb, 0, 123);
            gra.DrawString("10", f, sb, 15, 60);
            gra.DrawString("11", f, sb, 55, 20);
            gra.DrawString("12", f, sb, 125, 0);

            //表盘的框架:黑色,宽度是3
            Pen p = new Pen(Color.Black, 3);
            We(sender, e);
            p.DashStyle = DashStyle.Solid;
%0@            gra.DrawEllipse(p, 0, 0, 270, 270);

            //表盘上的三个指针i,j,k;
            Pen p1 = new Pen(Color.Red);
            Pen p2 = new Pen(Color.Blue);
            Pen p3 = new Pen(Color.Yellow);
            gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒          
            gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分
            gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时
            //指针旋转的代码
            if (i < 360)
            {
                i += 6;
                if (i == 270)
                {
                    if (j <= 360)
                    {
                        j += 6;
                        if (j == 270)
                        {
                            if (k <= 360)
                            {
                                k += 30;
                            }
                            else
                            {
                                k = 30;
                            }
                        }
                    }
                    else
                    {
                        j = 6;
                    }
                }
            }
            else
            {
                i = 6;
            }
        }
    }
}

posted @ 2011-05-24 20:45  clown  阅读(381)  评论(0编辑  收藏  举报