A simple method for making a clock when using C #

Source: Internet
Author: User

1. Introduction to the GDI + graphics library
1.1 Overview
GDI + is a new Microsoft. NET Framework class library used for Graphic programming. It is part of the. NET Framework, so it also faces objects.
1.2 device environment and objects
In GDI +, the output device is identified by using the DC object in the device environment. This object stores the information of a specific device and converts the call of the GDI + API function to the command to be sent to the device, you can also query the device environment object to determine the functions of the corresponding device so that the output result can be adjusted accordingly.
In GDI +, the device environment is encapsulated in the. NET base class System. Drawing. Graphics. Most of the Drawing work is done by calling the Graphics object.
2. How to Use GDI + to draw time clocks
Set the properties of each widget of the 2.1 hour clock
Use the controls in C # To create a pointer clock on the desktop display interface. It includes one PictureBox control, one Timer control, one policyicon control, and one StatusStrip control.
2.2 attribute settings for each control
The Interval attribute value of the Timer control is set to 1000, and the Enable attribute value is set to True. The StartPosition attribute of the form is set to CenterScreen, which enables the clock to be displayed in the center of the screen.
2.3 function implementation code
To implement the clock function, you need to design and enter the program code of the corresponding event or process of the corresponding object. In the design status, double-click the corresponding control, or double-click an event of the control, and enter the corresponding C # program code.
2.4 General statement timely Clock Design Method
A batch of variables or constants must be defined in the program, which can be completed in the general declaration in advance. The Code is as follows:
Const int s_pinlen = 100; // seconds Length
Const int m_pinlen = 75; // The length of the sub-needle
Const int h_pinlen = 75; // hour-Hand Length
PointF center = new PointF (s_pinlen + 3, s_pinlen + 3); // center position
SolidBrush sb = new SolidBrush (Color. Black); // The brush at the center of the clock
In addition to the preceding variable declaration, the time clock function has a sub-method named AngleToPos and myClock,
AngleToPos calculates the Coordinate Function of a point based on the angle and percentage. The Code is as follows:
PointF AngleToPos (int angle, float percent)
{
PointF pos = new PointF ();
Double radian = angle * Math. PI/180;
Pos. Y = center. Y-s_pinlen * percent * (float) Math. Sin (radian );
Pos. X = center. X + s_pinlen * percent * (float) Math. Cos (radian );
Return pos;
}
Methods myClock is mainly used to draw the center position, second point, and minute point of the clock. The Code is as follows:
Pen pDisk = new Pen (Color. Orange, 3); // Pen with clock background
Pen pScale = new Pen (Color. Coral); // scale Pen
Graphics myGraphics = pictureBox1.CreateGraphics ();
MyGraphics. Clear (Color. White );
Pen myPen = new Pen (Color. Black, 2 );
Point CPoint = new Point (s_pinlen, s_pinlen );
Point SPoint = new Point (int) (CPoint. X + (Math. sin (6 * s * Math. PI/180) * s_pinlen), (int) (CPoint. y-(Math. cos (6 * s * Math. PI/180) * s_pinlen ));
Point MPoint = new Point (int) (CPoint. X + (Math. sin (6 * m * Math. PI/180) * m_pinlen), (int) (CPoint. y-(Math. cos (6 * m * Math. PI/180) * m_pinlen ));
Point HPoint = new Point (int) (CPoint. X + (Math. sin (30 * h) + (m/2) * Math. PI/180) * h_pinlen), (int) (CPoint. y-(Math. cos (30 * h) + (m/2) * Math. PI/180) * h_pinlen ));
MyGraphics. FillEllipse (sb, center. X-8, center. Y-7, 14, 14 );
MyGraphics. DrawLine (myPen, CPoint, SPoint );
MyPen = new Pen (Color. Blue, 4 );
MyGraphics. DrawLine (myPen, CPoint, MPoint );
MyPen = new Pen (Color. Green, 6 );
MyGraphics. DrawLine (myPen, CPoint, HPoint );
MyGraphics. DrawEllipse (pDisk, 1, 1, s_pinlen * 2, s_pinlen * 2); // draw the scale
For (int I = 0; I <360; I + = 6)
{
Pen tempPen = (I % 30 = 0 )? PDisk: pScale;
PointF qidian = AngleToPos (I, 0.87f );
PointF zhongdian = AngleToPos (I, 1.0f );
MyGraphics. DrawLine (tempPen, qingdian, zhongdian );
}
2.5 code in the Tick event of Timer
The timer control code is the core of the entire clock. The Code is as follows:
Int h = DateTime. Now. Hour;
Int m = DateTime. Now. Minute;
Int s = DateTime. Now. Second;
MyClock (h, m, s); // call the method of clock painting
ToolStripStatusLabel1.Text = "current time:" + DateTime. Now. ToLongTimeString ();
3. Conclusion
Writing graphic Code is one of the complex tasks in the computer programming field. It is conducive to the dynamic generation of time clocks and clocks in C #. The design and implementation of programming is based on the design of mathematical science, guided by a reasonable design philosophy, the necessary programming language is used as a tool to basically meet the needs of users.
  
References:
[1] Liang Bing, Lu Shuang. C # sample code for Program Development (version 2nd). Tomorrow technology. People's post and telecommunications press, 2009.10.
[2] Chen guang. C # basic tutorial and practical training of program design. Peking University Press, 2008.2.
[3] Liu Xiaoying. C # Programming Tutorial. E-Industry Press, 2008.6.

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.