C# Analog Clock On Win Form

Submitted by: 
Language: 
Visitors have accessed this post 2113 times.

Hi,
Here is an C# Analogue Clock on windows form.
.NET Framework 4.0


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

A small patch: Less flicker and sharper graphics

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Analog_Clock_On_Form
{
public partial class ClockTime : UserControl
{
private const float PI = (float)Math.PI;
private DateTime dTime;
private float fRadius, fCenterX, fCenterY, fCenterCircleRadius, fHourLength;
private float fMinLength, fSecLength, fHourThickness, fMinThickness, fSecThickness;
private float fTicksThickness = 3;
private Color secColor = Color.RoyalBlue;
private Color circleColor = Color.Lime;

public bool Draw1MinuteTicks { get; set; } = true;
public bool Draw5MinuteTicks { get; set; } = true;
public Color TicksColor { get; set; } = Color.Navy;
public Color HourHandColor { get; set; } = Color.Orange;
public Color MinuteHandColor { get; set; } = Color.Red;
public Color SecondHandColor
{
get { return this.secColor; }
set
{
this.secColor = value;
this.circleColor = value;
}
}

public ClockTime()
{
InitializeComponent();
this.DoubleBuffered = true;
}

private void ClockTime_Load(object sender, EventArgs e)
{
dTime = DateTime.Now;
this.ClockTime_Resize(sender, e);
}

private void Timer1_Tick(object sender, EventArgs e)
{
this.dTime = DateTime.Now;
this.Refresh();
}

private void DrawLine(float fThickness, float fLength, Color color, float fRadians, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(color, fThickness),
fCenterX - (float)(fLength / 9 * Math.Sin(fRadians)),
fCenterY + (float)(fLength / 9 * Math.Cos(fRadians)),
fCenterX + (float)(fLength * Math.Sin(fRadians)),
fCenterY - (float)(fLength * Math.Cos(fRadians)));

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
}

private void DrawPolygon(float fThickness, float fLength, Color color, float fRadians, PaintEventArgs e)
{
PointF A = new PointF((float)(fCenterX + fThickness * 2 * Math.Sin(fRadians + PI / 2)),
(float)(fCenterY - fThickness * 2 * Math.Cos(fRadians + PI / 2)));
PointF B = new PointF((float)(fCenterX + fThickness * 2 * Math.Sin(fRadians - PI / 2)),
(float)(fCenterY - fThickness * 2 * Math.Cos(fRadians - PI / 2)));
PointF C = new PointF((float)(fCenterX + fLength * Math.Sin(fRadians)),
(float)(fCenterY - fLength * Math.Cos(fRadians)));
PointF D = new PointF((float)(fCenterX - fThickness * 4 * Math.Sin(fRadians)),
(float)(fCenterY + fThickness * 4 * Math.Cos(fRadians)));
PointF[] points = { A, D, B, C };
e.Graphics.FillPolygon(new SolidBrush(color), points);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
}

private void ClockTime_Paint(object sender, PaintEventArgs e)
{
float fRadHr = (dTime.Hour % 12 + dTime.Minute / 60F) * 30 * PI / 180;
float fRadMin = (dTime.Minute) * 6 * PI / 180;
float fRadSec = (dTime.Second) * 6 * PI / 180;

DrawPolygon(this.fHourThickness, this.fHourLength, HourHandColor, fRadHr, e);
DrawPolygon(this.fMinThickness, this.fMinLength, MinuteHandColor, fRadMin, e);
DrawLine(this.fSecThickness, this.fSecLength, secColor, fRadSec, e);

for (int i = 0; i < 60; i++)
{
if (this.Draw5MinuteTicks == true && i % 5 == 0) // Draw 5 minute ticks
{
e.Graphics.DrawLine(new Pen(TicksColor, fTicksThickness),
fCenterX + (float)(this.fRadius / 1.50F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.50F * Math.Cos(i * 6 * PI / 180)),
fCenterX + (float)(this.fRadius / 1.65F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.65F * Math.Cos(i * 6 * PI / 180)));
}
else if (this.Draw1MinuteTicks == true) // draw 1 minute ticks
{
e.Graphics.DrawLine(new Pen(TicksColor, fTicksThickness),
fCenterX + (float)(this.fRadius / 1.50F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.50F * Math.Cos(i * 6 * PI / 180)),
fCenterX + (float)(this.fRadius / 1.55F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.55F * Math.Cos(i * 6 * PI / 180)));
}
}

//draw circle at center
e.Graphics.FillEllipse(new SolidBrush(circleColor), fCenterX - fCenterCircleRadius / 2, fCenterY - fCenterCircleRadius / 2, fCenterCircleRadius, fCenterCircleRadius);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
}

private void ClockTime_Resize(object sender, EventArgs e)
{
this.Width = this.Height;
this.fRadius = this.Height / 2;
this.fCenterX = this.ClientSize.Width / 2;
this.fCenterY = this.ClientSize.Height / 2;
this.fHourLength = (float)this.Height / 3 / 1.85F;
this.fMinLength = (float)this.Height / 3 / 1.20F;
this.fSecLength = (float)this.Height / 3 / 1.15F;
this.fHourThickness = (float)this.Height / 100;
this.fMinThickness = (float)this.Height / 150;
this.fSecThickness = (float)this.Height / 200;
this.fCenterCircleRadius = this.Height / 50;
timer1.Start();
}
}
}

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.