C # draw a circular clock

Source: Internet
Author: User
Tags linecap

This article is modified by the author's reference to some cases:

The reference links are as follows:

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

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

(1) Final effect

(2) Program Realization Analysis:

Use the Paint event to draw, and then add a timer that is set to update 1000 milliseconds.

(3) Specific methods

In the form form (set size to 300*300), add Time1, the property enabled to True,interval set to 1000

And then add it under the Form1 property definition in Designer.cs.

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 for the form control

  int r=100;

  Draw a digital clock

Rectangle r1=new Rectangle (50,10,200,20);

parameter is the upper-left corner of the rectangle coordinates, width and length

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 ("Song Body", 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 outline

G.fillellipse (brushes.white,50,50,200,200)

G.drawellipse (New Pen (color.red,2), 46,46,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 ("n", Drawfont, Drawbrush,-9,-80);

E.graphics.drawstring ("3", Drawfont, Drawbrush, 70,-7);

E.graphics.drawstring ("9", Drawfont, Drawbrush,-80,-7);

Draw a scale

for (int z = 0; z <; z++)

{

G.smoothingmode=system.drawing.drawing2d

. smoothingmode.highquality;

   Makes the drawn pointer smoother, higher quality

G.resettransform;

G.translatetransform (150,150); Change the origin of coordinates

G.rotateyransform (Z*5); Rotate, rotate 6 degrees per second

if (z%5==0)

G=drawline (New Pen (color.black,3.0f). r-12,0,r-5,0);

Hour scale

Else

G.drawline (New Pen (color.black,1.5f), r-8,0,r-5,0);

Minute standard scale

}

Draw the second hand

G.resettransform (); Restore Default state

G.translatetransform (150,150);

G.rotatetransform (ss*6+270);

The x-axis of the horizontal line starts from the vertical and rotates 6 degrees at a time.

Pen secpen=new Pen (color.red,1);

Secpen.startcap=system.drawing.drawing2d.linecap.roundanchor;

Draw a line, start with a dot

Secpen.endcap=system.drawing.drawing2d.linecap.arrowanchor;

Draw line, end at Arrow

G.drawline (secpen,0,0,65,0);//65 indicates the length of the line

Draw the minute hand

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,50, 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);

}

Response Events for TIME1

private void Timer1_Tick (object sender, EventArgs e)

{

Invalidate ();

}

C # draw a circular clock

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.