用c#如何编绘时钟

在VS2005里面编绘一个运转的时钟(显示系统时间),关键不知道如何使用draw方法,最好是告诉我完整的C#程序代码。。。急求。。谢谢聊~~~~~~... 在VS2005里面编绘一个运转的时钟(显示系统时间),关键不知道如何使用draw方法,最好是告诉我完整的C#程序代码。。。急求。。
谢谢聊~~~~~~
展开
 我来答
西三227
2010-03-31 · TA获得超过1168个赞
知道小有建树答主
回答量:473
采纳率:0%
帮助的人:342万
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyClockApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
/// 得到当前系统时间,并将其拼接成一个字符串
/// </summary>
/// <returns>数字时钟要显示的字符串</returns>
public string GetTime()
{
String TimeInString = "";
int hour = DateTime.Now.Hour;
int min = DateTime.Now.Minute;
int sec = DateTime.Now.Second;
//将时,分,秒连成一个字符串
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;
}
/// <summary>
/// 在窗体上画表盘时钟的图形
/// </summary>
/// <param name="h"></param>
/// <param name="m"></param>
/// <param name="s"></param>
private void MyDrawClock(int h, int m, int s)
{
Graphics g = this.CreateGraphics();
Rectangle rect = this.ClientRectangle;

g.Clear(Color.White);

Pen myPen = new Pen(Color.Black, 1);
g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 75, this.ClientRectangle.Height / 2-75, 150, 150);//画表盘

Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点
//计算出秒针,时针,分针的另外一个商点
Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50)));
Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 40)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 40)));
Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360));

//以不同颜色和宽度绘制表针
myPen = new Pen(Color.Red, 1);
g.DrawLine(myPen, centerPoint, secPoint);
myPen = new Pen(Color.Green, 2);
g.DrawLine(myPen, centerPoint, minPoint);
myPen = new Pen(Color.Orange, 4);
g.DrawLine(myPen, centerPoint, hourPoint);
}
/// <summary>
/// 定时刷新显示时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
int h = DateTime.Now.Hour;
int s = DateTime.Now.Second;
int m = DateTime.Now.Minute;

MyDrawClock(h, m, s);
toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s);
lblTime.Text = GetTime();
}
/// <summary>
/// 若无此方法,时钟也能显示,但要等窗体显示几秒以后表盘才会显示。有了此方法窗体和表盘同时显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Paint(object sender, PaintEventArgs e)
{
int h = DateTime.Now.Hour;
int s = DateTime.Now.Second;
int m = DateTime.Now.Minute;

MyDrawClock(h, m, s);
toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s);
lblTime.Text = GetTime();
}

}
}
yaobing0687
2010-03-31 · TA获得超过988个赞
知道小有建树答主
回答量:1309
采纳率:0%
帮助的人:902万
展开全部
桌面上不要任何东西,直接复制如下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Timer t = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
t.Enabled = true;
t.Interval = 1000;
t.Tick+=new EventHandler(t_Tick);
t.Start();
}
string time = "";
private void t_Tick(object sender, EventArgs e)
{
time = DateTime.Now.Date.ToShortDateString() + "\n" + DateTime.Now.TimeOfDay.ToString();
Graphics gp= this.CreateGraphics();//如果想在其他控件上绘制,吧this改成空间的name
gp.Clear(Color.Gold);
gp.DrawString(time, new Font("幼圆",20), Brushes.SeaGreen, new PointF(20, 20));
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Stop();
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式