下面是我写的一个时钟 每次在运行的时候 时针和分针存在角度都是相同的 而且时针也像分针一样 秒针走一圈时针走 6°;
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.Drawing.Drawing2D;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int w, h,s=(360/60);
private void Form1_Load(object sender, EventArgs e)
{
} private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
w = pictureBox1.Width;
h = pictureBox1.Height; pictureBox1.Parent = this;
//时分秒三种针
Pen penm = new Pen(Color.Blue,2);
Pen penh = new Pen(Color.DarkGoldenrod,3);
Pen pen = new Pen(Color.Purple, 1);
Pen penK = new Pen(Color.Purple,4);
Graphics g = e.Graphics;
Brush brush=new SolidBrush(Color.Black);
Font font=new System.Drawing.Font("Arail",10,FontStyle.Bold);
//三种针分别旋转的角度
float mAngle = s * DateTime.Now.Minute + (float)DateTime.Now.Second / 10;
float hAngle = 30 * DateTime.Now.Hour ;
g.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsState state = g.Save(); //保存坐标变换后的状态
g.TranslateTransform(h / 2, h / 2); //话整点
for (int i = 0; i < 12; i++)
{
g.RotateTransform(30);
g.DrawLine(penK, new Point(0, -h/2), new Point(0, -h/2+12)); g.DrawString((i + 1).ToString(), font, brush, new Point(-10, -h / 2 + 10));
} //画出小的刻标
Pen penM=new Pen(Color.Purple,1);
for (int i = 0; i < 60;i++ )
{
g.RotateTransform(6);
g.DrawLine(penM,new Point(0,-h/2),new Point(0,-h/2+10));
}
g.DrawEllipse(pen, -h / 2, -h / 2, h, h);
g.RotateTransform((DateTime.Now.Second * s));
g.DrawLine(pen, new Point(0, 15), new Point(0,-h/2+15));
g.Restore(state);
//分针旋转的角度
g.TranslateTransform(h / 2, h / 2);
g.RotateTransform(mAngle);
g.DrawLine(penm,new Point(0,15),new Point(0,-h/2+20)); //时针旋转的角度
g.RotateTransform(hAngle);
g.DrawLine(penh,new Point(0,15),new Point(0,-h/2+25));
//Rectangle tec = new Rectangle(new Point(0,0),new System.Drawing.Size);
//Font font=new Font("隶书",5,FontStyle.Italic|FontStyle.Underline);
} private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
}
}
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.Drawing.Drawing2D;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int w, h,s=(360/60);
private void Form1_Load(object sender, EventArgs e)
{
} private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
w = pictureBox1.Width;
h = pictureBox1.Height; pictureBox1.Parent = this;
//时分秒三种针
Pen penm = new Pen(Color.Blue,2);
Pen penh = new Pen(Color.DarkGoldenrod,3);
Pen pen = new Pen(Color.Purple, 1);
Pen penK = new Pen(Color.Purple,4);
Graphics g = e.Graphics;
Brush brush=new SolidBrush(Color.Black);
Font font=new System.Drawing.Font("Arail",10,FontStyle.Bold);
//三种针分别旋转的角度
float mAngle = s * DateTime.Now.Minute + (float)DateTime.Now.Second / 10;
float hAngle = 30 * DateTime.Now.Hour ;
g.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsState state = g.Save(); //保存坐标变换后的状态
g.TranslateTransform(h / 2, h / 2); //话整点
for (int i = 0; i < 12; i++)
{
g.RotateTransform(30);
g.DrawLine(penK, new Point(0, -h/2), new Point(0, -h/2+12)); g.DrawString((i + 1).ToString(), font, brush, new Point(-10, -h / 2 + 10));
} //画出小的刻标
Pen penM=new Pen(Color.Purple,1);
for (int i = 0; i < 60;i++ )
{
g.RotateTransform(6);
g.DrawLine(penM,new Point(0,-h/2),new Point(0,-h/2+10));
}
g.DrawEllipse(pen, -h / 2, -h / 2, h, h);
g.RotateTransform((DateTime.Now.Second * s));
g.DrawLine(pen, new Point(0, 15), new Point(0,-h/2+15));
g.Restore(state);
//分针旋转的角度
g.TranslateTransform(h / 2, h / 2);
g.RotateTransform(mAngle);
g.DrawLine(penm,new Point(0,15),new Point(0,-h/2+20)); //时针旋转的角度
g.RotateTransform(hAngle);
g.DrawLine(penh,new Point(0,15),new Point(0,-h/2+25));
//Rectangle tec = new Rectangle(new Point(0,0),new System.Drawing.Size);
//Font font=new Font("隶书",5,FontStyle.Italic|FontStyle.Underline);
} private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货