基本信息
源码名称:C# 自绘钟表(Graphics入门级示例)
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2018-08-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍

本实例没有借助任何DLL文件,直接下载运行即可。



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

namespace WindowsFormsApplication1
{
    public partial class Draw : Form
    {
        public Draw()
        {
            InitializeComponent();
            bmp = (Bitmap)pictureBox1.Image;
            Init();
            Th = new Thread(TimerStart);
            Th.Start();
        }
        Bitmap bmp;
        Thread Th;
        bool bEnd = false;

        public PointF GetRotate(PointF center, PointF Target, double ThetaRadian)
        {
            PointF R_po = new PointF();

            PointF Temp = Target;
            Temp.X -= center.X;
            Temp.Y -= center.Y;

            R_po.X = (float)((Math.Cos(ThetaRadian) * Temp.X) - (Math.Sin(ThetaRadian) * Temp.Y));
            R_po.Y = (float)((Math.Sin(ThetaRadian) * Temp.X)   (Math.Cos(ThetaRadian) * Temp.Y));

            R_po.X  = center.X;
            R_po.Y  = center.Y;

            return R_po;
        }

        private void m_Draw()
        {
            string[] dt = DateTime.Now.ToString("hh:mm:ss").Split(new char[1] { ':' });

            Bitmap bmp1 = new Bitmap(bmp);
            Rectangle Rect = new Rectangle(50, 50, 200, 200);
            PointF Center = new PointF(Rect.X   Rect.Width / 2, Rect.Y   Rect.Height / 2);
            PointF po1_ss = new PointF(Rect.Right, Center.Y);
            PointF po1_mm = new PointF(Rect.Right - 10, Center.Y);
            PointF po1_H = new PointF(Rect.Right - 20, Center.Y);
            PointF po2_ss = GetRotate(Center, po1_ss, ((Convert.ToInt16(dt[2]) * 6 - 90) * Math.PI / 180d) - 0);
            PointF po2_mm = GetRotate(Center, po1_mm, ((Convert.ToInt16(dt[1]) * 6 - 90) * Math.PI / 180d) - 0);
            PointF po2_H = GetRotate(Center, po1_H, (((Convert.ToInt16(dt[0]) * 5   Convert.ToInt16(dt[1]) / 10) * 6 - 90) * Math.PI / 180d) - 0);
            using (Graphics g = Graphics.FromImage(bmp1))
            {
                g.DrawLine(new Pen(Color.Red, 2), Center, po2_ss);
                g.DrawLine(new Pen(Color.Green, 2), Center, po2_mm);
                g.DrawLine(new Pen(Color.Blue, 2), Center, po2_H);
                g.Dispose();
            }
            pictureBox1.Image = bmp1;
        }

        private void Init()
        {
            PointF po_ss;
            PointF po_mm;
            PointF po_H;
            Rectangle Rect_ss = new Rectangle(50, 50, 200, 200);

            PointF center = new PointF(Rect_ss.X   Rect_ss.Width / 2, Rect_ss.Y   Rect_ss.Height / 2);
            PointF po1_ss = new PointF(Rect_ss.Right, center.Y);
            PointF po1_mm = new PointF(Rect_ss.Right - 5, center.Y);
            PointF po1_H = new PointF(Rect_ss.Right - 15, center.Y);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawEllipse(new Pen(Color.Red, 2), Rect_ss);
                for (int k = 0; k < 60; k  )
                {
                    po_ss = GetRotate(center, po1_ss, ((k * 6) - 90) * Math.PI / 180d);
                    po_mm = GetRotate(center, po1_mm, ((k * 6) - 90) * Math.PI / 180d);
                    po_H = GetRotate(center, po1_H, ((k * 6) - 90) * Math.PI / 180d);
                    g.DrawLine(new Pen(Color.Red, 2), po_mm, po_ss);
                    if (k % 5 == 0)
                    {
                        g.DrawLine(new Pen(Color.Red, 2), po_H, po_ss);
                    }
                }

                g.DrawString("时针", new Font("宋体", 15), new SolidBrush(Color.Blue), new PointF(10, 10));
                g.DrawString("分针", new Font("宋体", 15), new SolidBrush(Color.Green), new PointF(10, 30));
                g.DrawString("秒针", new Font("宋体", 15), new SolidBrush(Color.Red), new PointF(10, 50));
                g.Dispose();

                pictureBox1.Image = bmp;
            }

        }


        private void TimerStart()
        {
            for (; ; Thread.Sleep(400))
            {
                if (bEnd)
                    return;
                m_Draw();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                bEnd = true;
                this.Close();
            }
        }

        private void Draw_FormClosing(object sender, FormClosingEventArgs e)
        {
            bEnd = true;
        }
    }
}