《c#小程序時(shí)鐘源碼Word版》由會(huì)員分享,可在線閱讀,更多相關(guān)《c#小程序時(shí)鐘源碼Word版(31頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 時(shí)o?à鐘¨?
{
推薦精選
public partial class Form1 : Form
{
//指?針?坐á?標(biāo)à¨o
private sta
2、tic float hour_x = 0, hour_y = 0;
private static float minute_x = 0, minute_y = 0;
private static float second_x = 0, second_y = 0;
推薦精選
private float radii = 200;
//將?角?度¨¨轉(zhuǎn)áa成¨|弧?度¨¨ 2 * 3.1415926 / 360 = 0.01745329
const float radian = 0.01745329F
3、;
public Form1()
{
推薦精選
InitializeComponent();
//指?定?§當(dāng)ì?à窗???口¨2大?¨?小?改?變à?時(shí)o?à重?繪?整?個(gè)?窗???口¨2
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
推薦精選
private void Form1_Load(object sender, Ev
4、entArgs e)
{
//創(chuàng)???建?§定?§時(shí)o?à器??對(duì)?象¨?
Timer timer = new Timer();
//啟?動(dòng)?¥定?§時(shí)o?à器??
timer.Start();
//設(shè)|¨¨定?§定?§時(shí)o?à器??事o?件t的ì?觸?£¤發(fā)¤?é函?¥數(shù)oy
推薦精選
timer.Tick += new EventHandler(TickEvent);
//設(shè)|¨¨定?§
5、定?§時(shí)o?à器??的ì?間?隔?時(shí)o?à間?為a 1000 毫¨¢秒?
timer.Interval = 1000;
推薦精選
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//創(chuàng)???建?§畫(huà)-刷?é,ê?并?é設(shè)|¨¨定?§畫(huà)-刷?é的ì?顏?色|?
推薦精選
SolidBrush brus
6、h = new SolidBrush(Color.FromArgb(255, 0, 255));
Pen pen = new Pen(Color.Black);
//得ì?到ì?窗???口¨2長(zhǎng)?è寬¨a兩¢?邊à?最á?短¨?的ì?一°?邊à?,作á??為a圓2的ì?外aa切D矩?形?的ì?邊à?長(zhǎng)?è
radii =
推薦精選
this.ClientRectangle.Width < this.ClientRectangle.Height ? this.ClientRectangle.Width
7、 :
this.ClientRectangle.Height;
radii -= 10;
//把??窗???口¨2的ì?原-點(diǎn)ì?移°?到ì?新?坐á?標(biāo)à¨o
推薦精選
e.Graphics.TranslateTransform(this.ClientRectangle.Width / 2 - radii / 2,
this.ClientRectangle.Height / 2 - radii / 2);
//
8、畫(huà)-圓2
e.Graphics.DrawEllipse(pen, 0, 0, radii, radii);
//時(shí)o?à鐘¨?刻¨?度¨¨坐á?標(biāo)à¨o
推薦精選
float x = 0.00F, y = 0.00F;
//畫(huà)-出?時(shí)o?à鐘¨?的ì?刻¨?度¨¨
for (float i = 0.00F; i < 360; i += 6.00F)
{
x = (flo
9、at)(radii / 2-6) * (float)Math.Sin(i * radian);
推薦精選
y = (float)(radii / 2-6) * (float)Math.Cos(i * radian);
if ((int)i % 30 == 0)
e.Graphics.FillEllipse(brush, x + radii / 2 - 4, y + radii / 2 - 4, 8, 8);
else
10、 e.Graphics.FillEllipse(brush, x + radii / 2 - 2, y + radii / 2 - 2, 4, 4);
推薦精選
}
// 畫(huà)-實(shí)o|ì心?圓2形?,ê?new SolidBrush(Color.Black) 是o?創(chuàng)???建?§畫(huà)-刷?é
e.Graphics.FillEllipse(new SolidBrush(Color.Black), radii / 2 - 5, radii / 2 - 5, 10, 10);
推薦精選
11、 e.Graphics.Dispose();
}
public void TickEvent(object sender,EventArgs e)
{
//創(chuàng)???建?§畫(huà)-板??對(duì)?象¨?
Graphics g = this.CreateGraphics();
//指?定?§新?原-點(diǎn)ì?
推薦精選
g.TranslateTransform(this.ClientRectangle.Width / 2,
12、 this.ClientRectangle.Height / 2);
//創(chuàng)???建?§畫(huà)-筆à¨o,并?é設(shè)|¨¨定?§顏?色|?
Pen pen = new
推薦精選
Pen(Color.FromArgb(0,255,0));
float radii2 = radii / 2 - 6;
//利¤?用??背à3景??色|?擦¨¢除y原-來(lái)¤??的ì?指?針?
pen.Color = Color.FromArgb(2
13、55, 255, 255);
g.DrawLine(pen, 0, 0, hour_x, -hour_y);
推薦精選
g.DrawLine(pen, 0, 0, minute_x, -minute_y);
g.DrawLine(pen, 0, 0, second_x, -second_y);
//畫(huà)-出?時(shí)o?à針?
int hour = DateTime.Now.Hour > 12 ? DateTime.Now.Hour -
14、 12 :
推薦精選
DateTime.Now.Hour;
hour_x = (float)(radii2 - 70) * (float)Math.Sin((hour * 30 + DateTime.Now.Minute * (30f / 60f)) * radian);
hour_y = (float)(radii2 - 70) * (float)Math.Cos((hour * 30 + DateTime.Now.Minute * (30f / 60f)) * radian);
pen.Color =
15、Color.FromArgb(0, 255, 0);
推薦精選
g.DrawLine(pen, 0, 0, hour_x, -hour_y);
//畫(huà)-出?分¤?針?
minute_x = (float)(radii2-30) * (float)Math.Sin(DateTime.Now.Minute * 6 * radian);
minute_y = (float)(radii2-30) * (
推薦精選
float)Math.Cos(DateT
16、ime.Now.Minute * 6 * radian);
pen.Color = Color.FromArgb(0, 0, 255);
g.DrawLine(pen, 0, 0, minute_x, -minute_y);
//畫(huà)-出?秒?針?
second_x = (
推薦精選
float)(radii2-8) * (float)Math.Sin(DateTime.Now.Second * 6 * radian);
sec
17、ond_y = (float)(radii2-8) * (float)Math.Cos(DateTime.Now.Second * 6 * radian);
pen.Color = Color.FromArgb(0, 255, 255);
g.DrawLine(pen, 0, 0, second_x, -second_y);
推薦精選
g.Dispose();
}
}
}
到下面的網(wǎng)站去看也可以看到c#小程序代碼
(注:可編輯下載,若有不當(dāng)之處,請(qǐng)指正,謝謝!)
推薦精選