实例介绍
【实例简介】本实例没有借助任何DLL文件,直接下载运行即可。
【实例截图】


【核心代码】
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.Threading;
namespace WindowsFormsApplication1
{
public partial class Draw : Form
{
public Draw()
{
InitializeComponent();
bmp = (Bitmap)pictureBox1.Image;
Init();
Th = new Thread(TimerStart);
Th.Start();
}
Bitmap bmp;
Thread Th;
bool bEnd = false;
public PointF GetRotate(PointF center, PointF Target, double ThetaRadian)
{
PointF R_po = new PointF();
PointF Temp = Target;
Temp.X -= center.X;
Temp.Y -= center.Y;
R_po.X = (float)((Math.Cos(ThetaRadian) * Temp.X) - (Math.Sin(ThetaRadian) * Temp.Y));
R_po.Y = (float)((Math.Sin(ThetaRadian) * Temp.X) (Math.Cos(ThetaRadian) * Temp.Y));
R_po.X = center.X;
R_po.Y = center.Y;
return R_po;
}
private void m_Draw()
{
string[] dt = DateTime.Now.ToString("hh:mm:ss").Split(new char[1] { ':' });
Bitmap bmp1 = new Bitmap(bmp);
Rectangle Rect = new Rectangle(50, 50, 200, 200);
PointF Center = new PointF(Rect.X Rect.Width / 2, Rect.Y Rect.Height / 2);
PointF po1_ss = new PointF(Rect.Right, Center.Y);
PointF po1_mm = new PointF(Rect.Right - 10, Center.Y);
PointF po1_H = new PointF(Rect.Right - 20, Center.Y);
PointF po2_ss = GetRotate(Center, po1_ss, ((Convert.ToInt16(dt[2]) * 6 - 90) * Math.PI / 180d) - 0);
PointF po2_mm = GetRotate(Center, po1_mm, ((Convert.ToInt16(dt[1]) * 6 - 90) * Math.PI / 180d) - 0);
PointF po2_H = GetRotate(Center, po1_H, (((Convert.ToInt16(dt[0]) * 5 Convert.ToInt16(dt[1]) / 10) * 6 - 90) * Math.PI / 180d) - 0);
using (Graphics g = Graphics.FromImage(bmp1))
{
g.DrawLine(new Pen(Color.Red, 2), Center, po2_ss);
g.DrawLine(new Pen(Color.Green, 2), Center, po2_mm);
g.DrawLine(new Pen(Color.Blue, 2), Center, po2_H);
g.Dispose();
}
pictureBox1.Image = bmp1;
}
private void Init()
{
PointF po_ss;
PointF po_mm;
PointF po_H;
Rectangle Rect_ss = new Rectangle(50, 50, 200, 200);
PointF center = new PointF(Rect_ss.X Rect_ss.Width / 2, Rect_ss.Y Rect_ss.Height / 2);
PointF po1_ss = new PointF(Rect_ss.Right, center.Y);
PointF po1_mm = new PointF(Rect_ss.Right - 5, center.Y);
PointF po1_H = new PointF(Rect_ss.Right - 15, center.Y);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawEllipse(new Pen(Color.Red, 2), Rect_ss);
for (int k = 0; k < 60; k )
{
po_ss = GetRotate(center, po1_ss, ((k * 6) - 90) * Math.PI / 180d);
po_mm = GetRotate(center, po1_mm, ((k * 6) - 90) * Math.PI / 180d);
po_H = GetRotate(center, po1_H, ((k * 6) - 90) * Math.PI / 180d);
g.DrawLine(new Pen(Color.Red, 2), po_mm, po_ss);
if (k % 5 == 0)
{
g.DrawLine(new Pen(Color.Red, 2), po_H, po_ss);
}
}
g.DrawString("时针", new Font("宋体", 15), new SolidBrush(Color.Blue), new PointF(10, 10));
g.DrawString("分针", new Font("宋体", 15), new SolidBrush(Color.Green), new PointF(10, 30));
g.DrawString("秒针", new Font("宋体", 15), new SolidBrush(Color.Red), new PointF(10, 50));
g.Dispose();
pictureBox1.Image = bmp;
}
}
private void TimerStart()
{
for (; ; Thread.Sleep(400))
{
if (bEnd)
return;
m_Draw();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
bEnd = true;
this.Close();
}
}
private void Draw_FormClosing(object sender, FormClosingEventArgs e)
{
bEnd = true;
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论