本文介绍了c#.net中的模拟时钟(win形式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我如何在c#.net(窗口应用程序)中制作一个模拟时钟
预先感谢
can any one tell me how to make an analog clock in c#.net(window application)
thanks in advance
推荐答案
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace Clock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(drawclock);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
}
private void drawclock(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rec = new Rectangle(20,20,250,250);
LinearGradientBrush linearbrush = new LinearGradientBrush(rec, Color.Yellow,
Color.Green,225);
g.FillEllipse(linearbrush,20, 20, 200, 200);
linearbrush.LinearColors = new Color[] { Color.Yellow, Color.Green, };
g.FillEllipse(linearbrush,30, 30, 180, 180);
linearbrush.LinearColors = new Color[] { Color.Green, Color.Yellow };
g.FillEllipse(linearbrush,33,33,174,174);
SolidBrush solidbrush = new SolidBrush(Color.White);
Font textFont = new Font("Arial Bold", 12F);
g.DrawString("12", textFont, solidbrush, 109, 40);
g.DrawString("11", textFont, solidbrush, 75, 50);
g.DrawString("10", textFont, solidbrush, 47, 75);
g.DrawString("9", textFont, solidbrush, 43, 110);
g.DrawString("8", textFont, solidbrush, 52, 145);
g.DrawString("7", textFont, solidbrush, 75, 170);
g.DrawString("6", textFont, solidbrush, 113, 180);
g.DrawString("5", textFont, solidbrush, 150, 170);
g.DrawString("4", textFont, solidbrush, 173, 145);
g.DrawString("3", textFont, solidbrush, 182, 110);
g.DrawString("2", textFont, solidbrush, 173, 75);
g.DrawString("1", textFont, solidbrush, 150, 50);
g.TranslateTransform(120,120,MatrixOrder.Append);
int hour = DateTime.Now.Hour;
int min = DateTime.Now.Minute;
int sec = DateTime.Now.Second;
// Create Pens
Pen hourPen = new Pen(Color.White, 2);
Pen minutePen = new Pen(Color.LightGray, 2);
Pen secondPen = new Pen(Color.Red, 1);
// Create angles
double secondAngle = 2.0 * Math.PI * sec / 60.0;
double minuteAngle = 2.0 * Math.PI * (min + sec / 60.0) / 60.0;
double hourAngle = 2.0 * Math.PI * (hour + min / 60.0) / 12.0;
// Set centre point
Point centre = new Point(0, 0);
// Draw Hour Hand
Point hourHand = new Point((int)(40 * Math.Sin(hourAngle)),
(int)(-40 * Math.Cos(hourAngle)));
g.DrawLine(hourPen, centre, hourHand);
// Draw Minute Hand
Point minHand = new Point((int)(70 * Math.Sin(minuteAngle)),
(int)(-70 * Math.Cos(minuteAngle)));
g.DrawLine(minutePen, centre, minHand);
// Draw Second Hand
Point secHand = new Point((int)(70 * Math.Sin(secondAngle)),
(int)(-70 * Math.Cos(secondAngle)));
g.DrawLine(secondPen, centre, secHand);
Invalidate();
}
}
}
[修改:添加了< pre>标签]
WW:顺便说一句,我们尽量不要为他们做家庭作业.我们给他们一些想法并将它们指向正确的方向,但是给他们他们所需要的确切代码并不能帮助他们.是的,如果他们有问题,请给他们更正,但不要为他们的整个项目付钱.
[Modified: added <pre> tags]
From WW: By the way, we try not to do people''s homework for them. We give them ideas and point them in the right direction, but giving them the exact code they need isn''t helping them. If they are having problems, yeah, give them the corrections, but don''t hand fee them their entire project.
这篇关于c#.net中的模拟时钟(win形式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!