http://www.lzhm.net/download/yulanClock.rar

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Drawing.Drawing2D;namespace clock
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.ComponentModel.IContainer components; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.contextMenu1 = new System.Windows.Forms.ContextMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // contextMenu1
    // 
    this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
     this.menuItem1});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.Text = "&X.退出";
    this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Interval = 900;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
    this.ClientSize = new System.Drawing.Size(200, 200);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Name = "Form1";
    this.ShowInTaskbar = false;
    this.TopMost = true;
    this.TransparencyKey = System.Drawing.Color.Blue;
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private Point mouseOffset;
    private System.Windows.Forms.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private bool isMouseDown = false; Graphics g;
    Pen p;
    SolidBrush sb;
    const int r = 100;
    PointF p0; //表盘的中点
    PointF ps1 = new PointF(0,0);
    PointF ps2 = new PointF(0,0);
    PointF pm1 = new PointF(0,0);
    PointF pm2 = new PointF(0,0);
    PointF ph1 = new PointF(0,0);
    private System.Windows.Forms.Timer timer1;
    PointF ph2 = new PointF(0,0); private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    int xOffset;
    int yOffset; if (e.Button == MouseButtons.Left) 
    {
    xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
    yOffset = -e.Y - SystemInformation.CaptionHeight - 
    SystemInformation.FrameBorderSize.Height;
    mouseOffset = new Point(xOffset, yOffset);
    isMouseDown = true;
    }
    else if(e.Button == MouseButtons.Right)
    {
    contextMenu1.Show(this, new Point(e.X, e.Y));
    }
    } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    // Changes the isMouseDown field so that the form does
    // not move unless the user is pressing the left mouse button.
    if (e.Button == MouseButtons.Left) 
    {
    isMouseDown = false;
    }
    } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (isMouseDown) 
    {
    Point mousePos = Control.MousePosition;
    mousePos.Offset(mouseOffset.X+4, mouseOffset.Y+23);
    Location = mousePos;
    }
    } private void menuItem1_Click(object sender, System.EventArgs e)
    {
    Application.Exit();
    } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    drawGraduation();
    drawHourFinger();
    drawMinuteFinger();
    drawSecondFinger();
    } public void drawGraduation()
    {
    //p = new Pen(Color.CornflowerBlue, 2);
    //g.DrawEllipse(p, 0, 0, r*2, r*2);
    p = new Pen(Color.Black, 2);
    g.DrawLine(p, r, 0, r, r*0.08F);
    g.DrawLine(p, 2*r*0.96F, r, 2*r, r);
    g.DrawLine(p, r, 2*r*0.96F, r, 2*r);
    g.DrawLine(p, 0, r, r*0.08F, r);
    for(int i = 1; i < 13; i++) //1~13方便写时间
    {
    float x = (float)Math.Sin(Math.PI/6*i)*(r-2);
    float y = (float)Math.Cos(Math.PI/6*i)*(r-2);
    sb = new SolidBrush(Color.Red);
    if(Math.PI/6*i%(Math.PI/2) != 0)
    g.FillEllipse(sb, p0.X+x-r*0.016F, p0.Y-y-r*0.016F, r*0.032F, r*0.032F);
    /*else
    {
    Font f = new Font("Arial", 10);
    sb = new SolidBrush(Color.Black);
    g.DrawString(i.ToString(), f, sb, p0.X+x-r*0.016F, p0.Y-y-r*0.016F);
    }*/
    }
    } public void drawSecondFinger()
    {
    p = new Pen(Color.Bisque, 1);
    g.DrawLine(p, ps1, ps2);
    double s = DateTime.Now.Second/60.0*2*Math.PI;
    float x = (float)Math.Sin(s)*r*0.92F;
    float y = (float)Math.Cos(s)*r*0.92F;
    PointF p1 = new PointF(p0.X+x, p0.Y-y);
    x *= 0.31F;
    y *= 0.31F;
    PointF p2 = new PointF(p0.X-x, p0.Y+y);
    p = new Pen(Color.Black, 1);
    g.DrawLine(p, p2, p1);
    ps1 = p1;
    ps2 = p2;
    sb = new SolidBrush(Color.CornflowerBlue);
    g.FillEllipse(sb, r*0.95F, r*0.95F, r*0.1F, r*0.1F);
    } public void drawMinuteFinger()
    {
    p = new Pen(Color.Bisque, 6);
    g.DrawLine(p, pm1, pm2);
    double s = DateTime.Now.Minute/60.0*2*Math.PI;
    float x = (float)Math.Sin(s)*r*0.8F;
    float y = (float)Math.Cos(s)*r*0.8F;
    PointF p1 = new PointF(p0.X+x, p0.Y-y);
    x *= 0.3F;
    y *= 0.3F;
    PointF p2 = new PointF(p0.X-x, p0.Y+y);
    p = new Pen(Color.Navy, 6);
    g.DrawLine(p, p1, p2);
    pm1 = p1;
    pm2 = p2;
    } public void drawHourFinger()
    {
    p = new Pen(Color.Bisque, 10);
    g.DrawLine(p, ph1, ph2);
    double s = (DateTime.Now.Hour%12*60+DateTime.Now.Minute)/720.0*2*Math.PI;
    float x = (float)Math.Sin(s)*r*0.64F;
    float y = (float)Math.Cos(s)*r*0.64F;
    PointF p1 = new PointF(p0.X+x, p0.Y-y);
    x *= 0.2F;
    y *= 0.2F;
    PointF p2 = new PointF(p0.X-x, p0.Y+y);
    p = new Pen(Color.Brown, 10);
    g.DrawLine(p, p1, p2);
    ph1 = p1;
    ph2 = p2;
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    g = this.CreateGraphics();
    g.SmoothingMode=SmoothingMode.AntiAlias; //平滑
    p0 = new PointF(r, r);
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    drawHourFinger();
    drawMinuteFinger();
    drawSecondFinger();
    this.Refresh();
    }
    }
    }
    这是钟。呵呵