C # Draw a circular clock,

Source: Internet
Author: User
Tags linecap

C # Draw a circular clock,

This article is modified after the author references some cases:

The Reference Links are as follows:

Http://blog.csdn.net/xuemoyao/article/details/8001113

Http://wenku.baidu.com/view/1221d6522f60ddccda38a0e7

(1) Final Results

 

(2) Program Implementation Analysis:

Use the Paint event to implement the painting, and then add a timer to set it to 1000 milliseconds.

(3) specific methods

Add Time1 to Form (set the size to 300*300), Enabled to true, and Interval to 1000

Add

This. Paint + = new System. Windows. Forms. PaintEventHandler (this. Form1_Paint );

Add a function under the Form1 class in Form1.cs

Private void Form_Paint (Object Sender, PaintEventArgs e)

{

Graphics g = e. Graphics; // defines g as the canvas of the Form Control.

Int r = 100;

// Draw a digital clock

Rectangle r1 = new Rectangle );

// The parameters are the coordinates, width, and length of the rectangle in the upper left corner.

G. FillRectangle (Brushes. Chocolate, r1); // fill color

Int ss = DateTime. Now. Second;

Int mm = DateTime. Now. Minute;

Int hh = DateTime. Now. Hour;

String s = Convert. ToString (ss );

String m = Convert. ToString (mm );

String h = Convert. ToString (hh );

If (ss> = 0 & ss <= 9)

S = "0" + s;

If (mm> = 0 & mm <= 9)

M = "0" + m;

If (hh> = 0 & hh <= 9)

H = "0" + h;

Font f1 = new Font ("", 12, FontStyle. Bold );

StringFormat sf1 = new StringFormat ();

SolidBrush s1 = new SolidBrush (Color. White );

G. Drawstring ("Current Time" + h + ":" + m + ":" + s, f1, s1, r1, sf1 );

G. DrawString ("Current Time" + h + ":" + m + ":" + s, f1, s1, r1, sf1 );

// Draw a circular contour

G. FillEllipse (Brushes. White, 50, 50, 200,200)

G. DrawEllipse (New Pen (Color. Red, 2), 208,208 );

G. DrawEllipse (new Pen (Color. DarkGray, 1), 50, 50, 200,200 );

// Draw a digital scale

G. ResetTransform ();

G. TranslateTransform (150,150); // reposition coordinates

Font drawFont = new Font ("Arial", 12 );

SolidBrush drawBrush = new SolidBrush (Color. Black );

E. Graphics. DrawString ("6", drawFont, drawBrush,-7, 70 );

E. Graphics. DrawString ("12", drawFont, drawBrush,-9,-80 );

E. Graphics. DrawString ("3", drawFont, drawBrush, 70,-7 );

E. Graphics. DrawString ("9", drawFont, drawBrush,-80,-7 );

// Draw the scale

For (int z = 0; z <60; z ++)

{

G. SmoothingMode = System. Drawing. Drawing2D

. SmoothingMode. HighQuality;

// Make the drawn pointer smoother and higher quality

G. ResetTransform;

G. TranslateTransform (150,150); // modify the coordinate origin

G. RotateYransform (z * 5); // rotate, 6 degrees per second

If (z % 5 = 0)

G = DrawLine (new Pen (Color. Black, 3.0f). R-12, 0, R-5, 0 );

// Hourly Scale

Else

G. DrawLine (new Pen (Color. Black, 1.5f), r-8, 0, R-5, 0 );

// Standard minutely Scale

}

// Draw seconds

G. ResetTransform (); // restore the default status

G. TranslateTransform (150,150 );

G. RotateTransform (ss x 6 + 270 );

// Take the horizontal line as the X axis and start to rotate from the vertical top. Each rotation is 6 degrees.

Pen secPen = new Pen (Color. Red, 1 );

SecPen. StartCap = System. Drawing. Drawing2D. LineCap. RoundAnchor;

// Draw a line, starting with a dot

SecPen. EndCap = System. Drawing. Drawing2D. LineCap. ArrowAnchor;

// Draw a line, ending with an arrow

G. DrawLine (secPen,); // 65 indicates the length of the Line

// Draw a sub-needle

G. ResetTransform ();

G. TranslateTransform (150,150 );

G. RotateTransform (mm * 6 + 270 );

Pen minPen = new Pen (Color. Blue, 2 );

MinPen. StartCap = System. Drawing. Drawing2D. LineCap. RoundAnchor;

MinPen. EndCap = System. Drawing. Drawing2D. LineCap. ArrowAnchor;

G. DrawLine (minPen, 0, 0, 0, 0 );

// Draw the hour hand

G. ResetTransform ();

G. TranslateTransform (150,150 );

G. RotateTransform (hh * 30 + mm * 1/2 + 270 );

Pen hourPen = new Pen (Color. Black, 3 );

HourPen. EndCap = System. Drawing. Drawing2D. LineCap. ArrowAnchor;

G. DrawLine (hourPen, 0, 0, 35, 0 );

}

Time1 RESPONSE event

Private void timereffectick (object sender, EventArgs e)

{

Invalidate ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.