在C#里怎么利用timer控件重复执行绘图来实现一个动态的时钟,最好有代码,O(∩_∩)O谢谢
展开全部
//定义秒针,分针,时针的长度
private const int s_pinlen = 95;
private const int m_pinlen = 75;
private const int h_pinlen = 50;
private void MyDrawClock(int h, int m, int s)
{
Graphics g = this.CreateGraphics();
//清除所有
//得到当前矩形值
Rectangle rect = this.ClientRectangle;
//新建矩形对象距离 底边640 右边480 横纵半径都是300
rect = new Rectangle(this.ClientRectangle.Right - 640, this.ClientRectangle.Bottom - 480, 300, 300);
g.Clear(Color.Gray);
//创建Pen
Pen myPen = new Pen(Color.White, 3);
//绘制表盘
g.DrawEllipse(myPen, rect);
//表中心点
Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
//计算出秒针,分针,时针的另外端点
Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(6 * s * Math.PI / 180)) * s_pinlen),
(int)(centerPoint.Y - (Math.Cos(6 * s * Math.PI / 180)) * s_pinlen));
Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(6 * m * Math.PI / 180)) * m_pinlen),
(int)(centerPoint.Y - (Math.Cos(6 * m * Math.PI / 180)) * m_pinlen));
Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(((30 * h) + (m / 2)) * Math.PI / 180)) * h_pinlen),
(int)(centerPoint.Y - (Math.Cos(((30 * h) + (m / 2)) * Math.PI / 180)) * h_pinlen));
//以不同的颜色绘制
g.DrawLine(myPen, centerPoint, secPoint);
myPen = new Pen(Color.LightPink, 4);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.YellowGreen, 5);
g.DrawLine(myPen, centerPoint, hourPoint);
}
private void timer1_Tick(object sender, EventArgs e)
{
//得到当前的时、分、秒
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
//调用MyDrawClock绘制图形表盘
MyDrawClock(h, m, s);
}
private const int s_pinlen = 95;
private const int m_pinlen = 75;
private const int h_pinlen = 50;
private void MyDrawClock(int h, int m, int s)
{
Graphics g = this.CreateGraphics();
//清除所有
//得到当前矩形值
Rectangle rect = this.ClientRectangle;
//新建矩形对象距离 底边640 右边480 横纵半径都是300
rect = new Rectangle(this.ClientRectangle.Right - 640, this.ClientRectangle.Bottom - 480, 300, 300);
g.Clear(Color.Gray);
//创建Pen
Pen myPen = new Pen(Color.White, 3);
//绘制表盘
g.DrawEllipse(myPen, rect);
//表中心点
Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
//计算出秒针,分针,时针的另外端点
Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(6 * s * Math.PI / 180)) * s_pinlen),
(int)(centerPoint.Y - (Math.Cos(6 * s * Math.PI / 180)) * s_pinlen));
Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(6 * m * Math.PI / 180)) * m_pinlen),
(int)(centerPoint.Y - (Math.Cos(6 * m * Math.PI / 180)) * m_pinlen));
Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(((30 * h) + (m / 2)) * Math.PI / 180)) * h_pinlen),
(int)(centerPoint.Y - (Math.Cos(((30 * h) + (m / 2)) * Math.PI / 180)) * h_pinlen));
//以不同的颜色绘制
g.DrawLine(myPen, centerPoint, secPoint);
myPen = new Pen(Color.LightPink, 4);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.YellowGreen, 5);
g.DrawLine(myPen, centerPoint, hourPoint);
}
private void timer1_Tick(object sender, EventArgs e)
{
//得到当前的时、分、秒
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
//调用MyDrawClock绘制图形表盘
MyDrawClock(h, m, s);
}
追问
可是这是静态的啊,不会重复绘图
追答
你用了就知道了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询