C#中怎么制作时钟
3个回答
展开全部
下面是简易时钟的实现:
简易时钟的界面只有数字和指针。这里肯定要用到Timer控件啦,每秒钟刷新界面,这样的话就要求双缓冲。
第一步:添加用户控件,命名为myClock。
第二步:构造函数实例化控件和做双缓冲处理。
public myClock()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
myTimer = new Timer();
myTimer.Interval = 1000;
myTimer.Enabled = true;
myTimer.Tick += new EventHandler(myTimer_Tick);
}
第三步:Paint重绘事件
private void myClock_Paint(object sender, PaintEventArgs e)
{
Graphics gra = e.Graphics;//创建一个画板
/*********************************/
//表盘上的指针
using (SolidBrush sb = new SolidBrush(Color.Blue))
{
Font f = new Font("宋体", 18);
gra.DrawString("1", f, sb, 195, 20);
gra.DrawString("2", f, sb, 235, 60);
gra.DrawString("3", f, sb, 250, 123);
gra.DrawString("4", f, sb, 235, 183);
gra.DrawString("5", f, sb, 195, 225);
gra.DrawString("6", f, sb, 125, 245);
gra.DrawString("7", f, sb, 55, 225);
gra.DrawString("8", f, sb, 15, 183);
gra.DrawString("9", f, sb, 0, 123);
gra.DrawString("10", f, sb, 15, 60);
gra.DrawString("11", f, sb, 55, 20);
gra.DrawString("12", f, sb, 125, 0);
}
//表盘的框架:黑色,宽度是3
using (Pen p = new Pen(Color.Black, 3))
{
p.DashStyle = DashStyle.Solid;
gra.DrawEllipse(p, 0, 0, 270, 270);
}
//表盘上的三个指针i,j,k;
using (Pen p1 = new Pen(Color.Red))
{
using (Pen p2 = new Pen(Color.Blue))
{
using (Pen p3 = new Pen(Color.Yellow))
{
gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒
gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分
gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时
}
}
}
//指针旋转的代码
if (i < 360)
{
i += 6;
if (i == 270)
{
if (j <= 360)
{
j += 6;
if (j == 270)
{
if (k <= 360)
{
k += 30;
}
else
{
k = 30;
}
}
}
else
{
j = 6;
}
}
}
else
{
i = 6;
}
}
贴出所有代码:
//控件名:myClock
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace myControl
{
public partial class myClock : UserControl
{
public myClock()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
myTimer = new Timer();
myTimer.Interval = 1000;
myTimer.Enabled = true;
myTimer.Tick += new EventHandler(myTimer_Tick);
}
private Timer myTimer;
//定义三个变量
float i = 270, j = 270, k = 270;
private void myTimer_Tick(object sender, EventArgs e)
{
this.Invalidate();
}
private void myClock_Paint(object sender, PaintEventArgs e)
{
Graphics gra = e.Graphics;//创建一个画板
/*********************************/
//表盘上的指针
using (SolidBrush sb = new SolidBrush(Color.Blue))
{
Font f = new Font("宋体", 18);
gra.DrawString("1", f, sb, 195, 20);
gra.DrawString("2", f, sb, 235, 60);
gra.DrawString("3", f, sb, 250, 123);
gra.DrawString("4", f, sb, 235, 183);
gra.DrawString("5", f, sb, 195, 225);
gra.DrawString("6", f, sb, 125, 245);
gra.DrawString("7", f, sb, 55, 225);
gra.DrawString("8", f, sb, 15, 183);
gra.DrawString("9", f, sb, 0, 123);
gra.DrawString("10", f, sb, 15, 60);
gra.DrawString("11", f, sb, 55, 20);
gra.DrawString("12", f, sb, 125, 0);
}
//表盘的框架:黑色,宽度是3
using (Pen p = new Pen(Color.Black, 3))
{
p.DashStyle = DashStyle.Solid;
gra.DrawEllipse(p, 0, 0, 270, 270);
}
//表盘上的三个指针i,j,k;
using (Pen p1 = new Pen(Color.Red))
{
using (Pen p2 = new Pen(Color.Blue))
{
using (Pen p3 = new Pen(Color.Yellow))
{
gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒
gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分
gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时
}
}
}
//指针旋转的代码
if (i < 360)
{
i += 6;
if (i == 270)
{
if (j <= 360)
{
j += 6;
if (j == 270)
{
if (k <= 360)
{
k += 30;
}
else
{
k = 30;
}
}
}
else
{
j = 6;
}
}
}
else
{
i = 6;
}
}
}
}
简易时钟的界面只有数字和指针。这里肯定要用到Timer控件啦,每秒钟刷新界面,这样的话就要求双缓冲。
第一步:添加用户控件,命名为myClock。
第二步:构造函数实例化控件和做双缓冲处理。
public myClock()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
myTimer = new Timer();
myTimer.Interval = 1000;
myTimer.Enabled = true;
myTimer.Tick += new EventHandler(myTimer_Tick);
}
第三步:Paint重绘事件
private void myClock_Paint(object sender, PaintEventArgs e)
{
Graphics gra = e.Graphics;//创建一个画板
/*********************************/
//表盘上的指针
using (SolidBrush sb = new SolidBrush(Color.Blue))
{
Font f = new Font("宋体", 18);
gra.DrawString("1", f, sb, 195, 20);
gra.DrawString("2", f, sb, 235, 60);
gra.DrawString("3", f, sb, 250, 123);
gra.DrawString("4", f, sb, 235, 183);
gra.DrawString("5", f, sb, 195, 225);
gra.DrawString("6", f, sb, 125, 245);
gra.DrawString("7", f, sb, 55, 225);
gra.DrawString("8", f, sb, 15, 183);
gra.DrawString("9", f, sb, 0, 123);
gra.DrawString("10", f, sb, 15, 60);
gra.DrawString("11", f, sb, 55, 20);
gra.DrawString("12", f, sb, 125, 0);
}
//表盘的框架:黑色,宽度是3
using (Pen p = new Pen(Color.Black, 3))
{
p.DashStyle = DashStyle.Solid;
gra.DrawEllipse(p, 0, 0, 270, 270);
}
//表盘上的三个指针i,j,k;
using (Pen p1 = new Pen(Color.Red))
{
using (Pen p2 = new Pen(Color.Blue))
{
using (Pen p3 = new Pen(Color.Yellow))
{
gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒
gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分
gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时
}
}
}
//指针旋转的代码
if (i < 360)
{
i += 6;
if (i == 270)
{
if (j <= 360)
{
j += 6;
if (j == 270)
{
if (k <= 360)
{
k += 30;
}
else
{
k = 30;
}
}
}
else
{
j = 6;
}
}
}
else
{
i = 6;
}
}
贴出所有代码:
//控件名:myClock
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace myControl
{
public partial class myClock : UserControl
{
public myClock()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
myTimer = new Timer();
myTimer.Interval = 1000;
myTimer.Enabled = true;
myTimer.Tick += new EventHandler(myTimer_Tick);
}
private Timer myTimer;
//定义三个变量
float i = 270, j = 270, k = 270;
private void myTimer_Tick(object sender, EventArgs e)
{
this.Invalidate();
}
private void myClock_Paint(object sender, PaintEventArgs e)
{
Graphics gra = e.Graphics;//创建一个画板
/*********************************/
//表盘上的指针
using (SolidBrush sb = new SolidBrush(Color.Blue))
{
Font f = new Font("宋体", 18);
gra.DrawString("1", f, sb, 195, 20);
gra.DrawString("2", f, sb, 235, 60);
gra.DrawString("3", f, sb, 250, 123);
gra.DrawString("4", f, sb, 235, 183);
gra.DrawString("5", f, sb, 195, 225);
gra.DrawString("6", f, sb, 125, 245);
gra.DrawString("7", f, sb, 55, 225);
gra.DrawString("8", f, sb, 15, 183);
gra.DrawString("9", f, sb, 0, 123);
gra.DrawString("10", f, sb, 15, 60);
gra.DrawString("11", f, sb, 55, 20);
gra.DrawString("12", f, sb, 125, 0);
}
//表盘的框架:黑色,宽度是3
using (Pen p = new Pen(Color.Black, 3))
{
p.DashStyle = DashStyle.Solid;
gra.DrawEllipse(p, 0, 0, 270, 270);
}
//表盘上的三个指针i,j,k;
using (Pen p1 = new Pen(Color.Red))
{
using (Pen p2 = new Pen(Color.Blue))
{
using (Pen p3 = new Pen(Color.Yellow))
{
gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒
gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分
gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时
}
}
}
//指针旋转的代码
if (i < 360)
{
i += 6;
if (i == 270)
{
if (j <= 360)
{
j += 6;
if (j == 270)
{
if (k <= 360)
{
k += 30;
}
else
{
k = 30;
}
}
}
else
{
j = 6;
}
}
}
else
{
i = 6;
}
}
}
}
展开全部
使用GDI+制作简易时钟窗体程序(C#语言描述)
一、前期准备
1、创建一个WindowsApplication
2、放一个Timer控件
二、代码部分
//定义秒针,分针,时针的长度
private const int s_pinlen = 150;
private const int m_pinlen = 100;
private const int h_pinlen = 50;
private void MyDrawClock(int h, int m, int s)
...{
Graphics g = this.CreateGraphics();
//清除所有
g.Clear(Color.White);
//创建Pen
Pen myPen = new Pen(Color.Black, 1);
//绘制表盘
g.DrawEllipse(myPen, this.ClientRectangle);
//表中心点
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.Blue, 2);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.Red, 4);
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);
//在窗体标题上显示数字时钟
this.Text = String.Format("{0}:{1}:{2}", h, m, s);
}
使用GDI+制作简易时钟窗体程序(C#语言描述) 收藏
一、前期准备
1、创建一个WindowsApplication
2、放一个Timer控件
二、代码部分
//定义秒针,分针,时针的长度
private const int s_pinlen = 150;
private const int m_pinlen = 100;
private const int h_pinlen = 50;
private void MyDrawClock(int h, int m, int s)
...{
Graphics g = this.CreateGraphics();
//清除所有
g.Clear(Color.White);
//创建Pen
Pen myPen = new Pen(Color.Black, 1);
//绘制表盘
g.DrawEllipse(myPen, this.ClientRectangle);
//表中心点
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.Blue, 2);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.Red, 4);
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);
//在窗体标题上显示数字时钟
this.Text = String.Format("{0}:{1}:{2}", h, m, s);
}
这样一个简易时钟窗体程序就完成了
一、前期准备
1、创建一个WindowsApplication
2、放一个Timer控件
二、代码部分
//定义秒针,分针,时针的长度
private const int s_pinlen = 150;
private const int m_pinlen = 100;
private const int h_pinlen = 50;
private void MyDrawClock(int h, int m, int s)
...{
Graphics g = this.CreateGraphics();
//清除所有
g.Clear(Color.White);
//创建Pen
Pen myPen = new Pen(Color.Black, 1);
//绘制表盘
g.DrawEllipse(myPen, this.ClientRectangle);
//表中心点
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.Blue, 2);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.Red, 4);
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);
//在窗体标题上显示数字时钟
this.Text = String.Format("{0}:{1}:{2}", h, m, s);
}
使用GDI+制作简易时钟窗体程序(C#语言描述) 收藏
一、前期准备
1、创建一个WindowsApplication
2、放一个Timer控件
二、代码部分
//定义秒针,分针,时针的长度
private const int s_pinlen = 150;
private const int m_pinlen = 100;
private const int h_pinlen = 50;
private void MyDrawClock(int h, int m, int s)
...{
Graphics g = this.CreateGraphics();
//清除所有
g.Clear(Color.White);
//创建Pen
Pen myPen = new Pen(Color.Black, 1);
//绘制表盘
g.DrawEllipse(myPen, this.ClientRectangle);
//表中心点
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.Blue, 2);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.Red, 4);
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);
//在窗体标题上显示数字时钟
this.Text = String.Format("{0}:{1}:{2}", h, m, s);
}
这样一个简易时钟窗体程序就完成了
参考资料: http://blog.csdn.net/lishewen/archive/2007/12/08/1925013.aspx
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很简单的。
加一个时间控件,然后设置时间控件的触发为1000(MS)。然后在时间控件触发时执行: string b = DateTime.Now.ToLongDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString();
this.label6.Text = b.ToString();
调试过,绝对没问题,有问题的话,发至邮箱:zbs5010@sina.com
加一个时间控件,然后设置时间控件的触发为1000(MS)。然后在时间控件触发时执行: string b = DateTime.Now.ToLongDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString();
this.label6.Text = b.ToString();
调试过,绝对没问题,有问题的话,发至邮箱:zbs5010@sina.com
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询