GDI+绘制时钟

using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WebService
{
    class Clock
    {
        private Point mickeyMouse = new Point(0, 0);

        public void DrawClock(Graphics g)
        {
            ///centre(120, 130);
            Rectangle outRect = new Rectangle(0, 0, 240, 260);
            Rectangle midRect = new Rectangle(7, 7, 226, 246);
            Rectangle inRect = new Rectangle(10, 10, 220, 240);

            LinearGradientBrush outlBlueBrush = new LinearGradientBrush(outRect, Color.FromArgb(0, 0, 100),
                Color.FromArgb(0, 0, 255), LinearGradientMode.BackwardDiagonal);
            LinearGradientBrush midlBlueBrush = new LinearGradientBrush(midRect, Color.FromArgb(0, 0, 255),
                Color.FromArgb(0, 0, 100), LinearGradientMode.BackwardDiagonal);
            LinearGradientBrush inlBlueBrush = new LinearGradientBrush(inRect, Color.FromArgb(0, 0, 100),
               Color.FromArgb(0, 0, 255), LinearGradientMode.BackwardDiagonal);

            g.FillEllipse(outlBlueBrush, outRect);
            g.FillEllipse(midlBlueBrush, midRect);
            g.FillEllipse(inlBlueBrush, inRect);
            outlBlueBrush.Dispose();
            midlBlueBrush.Dispose();
            inlBlueBrush.Dispose();
            Font myFont = new Font("Arial", 20, FontStyle.Bold);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            g.DrawString("12", myFont, whiteBrush, 100, 10);
            g.DrawString("6", myFont, whiteBrush, 110, 223);
            g.DrawString("3", myFont, whiteBrush, 210, 120);
            g.DrawString("9", myFont, whiteBrush, 10, 120);
            g.DrawString("1", myFont, whiteBrush, 160, 26);
            g.DrawString("2", myFont, whiteBrush, 194, 64);
            g.DrawString("5", myFont, whiteBrush, 156, 210);
            g.DrawString("4", myFont, whiteBrush, 192, 174);
            g.DrawString("11", myFont, whiteBrush, 55, 28);
            g.DrawString("10", myFont, whiteBrush, 22, 66);
            g.DrawString("7", myFont, whiteBrush, 64, 210);
            g.DrawString("8", myFont, whiteBrush, 28, 174);
            myFont.Dispose();
            whiteBrush.Dispose();
            //DateTime;
            g.TranslateTransform(120, 130, MatrixOrder.Append);
            Pen hourPen = new Pen(Color.White, 6);
            hourPen.SetLineCap(LineCap.RoundAnchor, LineCap.ArrowAnchor, DashCap.Flat);
            Pen minutePen = new Pen(Color.White, 4);
            minutePen.SetLineCap(LineCap.RoundAnchor, LineCap.ArrowAnchor, DashCap.Flat);
            Pen secondPen = new Pen(Color.Red, 2);
            int sec = DateTime.Now.Second;
            int min = DateTime.Now.Minute;
            int hour = DateTime.Now.Hour;
            double secondAngle = 2.0 * Math.PI * sec / 60.0;
            double minuteAngle = 2.0 * Math.PI * (min + sec / 60.0) / 60.0;
            double hourAngle = 2.0 * Math.PI * (hour + min / 60.0) / 12.0;
            Point centre = new Point(0, 0);
            Point hourHand = new Point((int)(40 * Math.Sin(hourAngle)),
            (int)(-40 * Math.Cos(hourAngle)));
            g.DrawLine(hourPen, centre, hourHand);

            Point minHand = new Point((int)(80 * Math.Sin(minuteAngle)),
            (int)(-80 * Math.Cos(minuteAngle)));
            g.DrawLine(minutePen, centre, minHand);

            Point secHand = new Point((int)(80 * Math.Sin(secondAngle)),
            (int)(-80 * Math.Cos(secondAngle)));
            g.DrawLine(secondPen, centre, secHand);
           
            hourPen.Dispose();
            minutePen.Dispose();
            secondPen.Dispose();
        }
        //private void timer1_Tick(object sender, EventArgs e)
        //{
        //    this.Invalidate();
        //}
        //private void hideToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    if (contextMenuStrip1.Items[0].ToString() == "Hide")
        //    {
        //        this.Hide();
        //        contextMenuStrip1.Items[0].Text = "Show";
        //        return;
        //    }

        //    if (contextMenuStrip1.Items[0].ToString() == "Show")
        //    {
        //        this.Show();
        //        contextMenuStrip1.Items[0].Text = "Hide";
        //        return;
        //    }
        //}
        //private void Form1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    mickeyMouse = new Point(-e.X, -e.Y);
        //}
        //private void Form1_MouseMove(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == MouseButtons.Left)
        //    {
        //        Point mousePos = Control.MousePosition;
        //        mousePos.Offset(mickeyMouse.X, mickeyMouse.Y);
        //        Location = mousePos;
        //    }
        //}
        //private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        //{
        //    if (contextMenuStrip1.Items[0].ToString() == "Show")
        //    {
        //        this.Show();
        //        contextMenuStrip1.Items[0].Text = "Hide";
        //        return;
        //    }
        //}
    }
}

posted @ 2012-03-27 10:21  欢喜王子  阅读(708)  评论(0编辑  收藏  举报