c#图形编程
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string GetTime() { string TimeInString = ""; //DateTime.Now.Hour返回当前时 int hour = DateTime.Now.Hour; //DateTime.Now.Hour返回当前分 int min = DateTime.Now.Minute; //DateTime.Now.Hour返回当前秒 int sec = DateTime.Now.Second; //将时、分、秒连在一起得到TimeInString TimeInString = (hour < 10) ? "0" hour.ToString() : hour.ToString(); TimeInString = ":" ((min < 10) ? "0" min.ToString() : min.ToString()); TimeInString = ":" ((sec < 10) ? "0" sec.ToString() : sec.ToString()); return TimeInString; } 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); //在statusbar上显示数字时钟 // statusBar1.Text = String.Format("{0}:{1}:{2}", h, m, s); //在lbtime上显示数字时钟 lbTime.Text = GetTime(); } //定义秒针,分针,时针的长度 private const int s_pinlen = 75; private const int m_pinlen = 50; private const int h_pinlen = 25; private void MyDrawClock(int h, int m, int s) { Graphics g = this.CreateGraphics(); //清除所有 //得到当前矩形值 Rectangle rect = this.ClientRectangle; //新建矩形对象距离 底边250 右边230 横纵半径都是150 如果不都是150 两个不相等 //比如一个是140 一个是160 则画出来的是一个椭圆 rect = new Rectangle(this.ClientRectangle.Right - 250, this.ClientRectangle.Bottom - 230, 150, 150); g.Clear(Color.White); //创建Pen Pen myPen = new Pen(Color.Blue, 1); //绘制表盘 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.Green, 2); g.DrawLine(myPen, centerPoint, minPoint); myPen = new Pen(Color.Red, 4); g.DrawLine(myPen, centerPoint, hourPoint); } }
评论