C # Clock Control

Source: Internet
Author: User

The clock controls written many years ago were released for your reference. First, create a clock control inherited from UserControl and draw it with double buffering. Because it was a few years ago, some code was written into the attribute, and I was too lazy to change it. If you are interested, you can change it to a more standard. // ClockControl. cs using System; using System. drawing; using System. drawing. drawing2D; using System. windows. forms; namespace BrawDraw. com. photoFrame. net. publicFunctions {public class ClockControl: UserControl {DateTime dt; Pen pen = new Pen (Color. black, 1); Pen penSecond = new Pen (Color. black, 6); Brush brush = new SolidBrush (Color. black); Bitmap m_Bitmap = null; Graphics g = null; private Font Clock Font = new Font ("Arial", 48, FontStyle. bold); public ClockControl () {SetStyle (ControlStyles. userPaint | ControlStyles. doubleBuffer | ControlStyles. allPaintingInWmPaint | ControlStyles. resizeRedraw, true); this. updateStyles (); Enabled = false; m_Bitmap = new Bitmap (100,100);} public DateTime Time {get {return dt;} set {m_Bitmap = new Bitmap (this. clientRectangle. width, this. clientRectangle. he Ight); g = Graphics. fromImage (m_Bitmap); g. clear (Color. white); InitializeCoordinates (g); DrawDots (g, brush); DrawHourHand (g, pen); DrawMinuteHand (g, pen); DrawNumbers (g); // DrawSecondHand (g, pen); // if (g! = Null) g. Dispose (); if (dt. Hour! = Value. Hour) {DrawHourHand (g, pen);} if (dt. Minute! = Value. Minute) {DrawHourHand (g, pen); DrawMinuteHand (g, pen);} if (dt. Second! = Value. Second) {DrawMinuteHand (g, pen); DrawSecondHand (g, pen);} if (dt. Millisecond! = Value. millisecond) {DrawSecondHand (g, pen);} dt = value; Graphics grfx = CreateGraphics (); grfx. drawImage (m_Bitmap, 0, 0); this. validate (); if (grfx! = Null) grfx. dispose () ;}} protected override void OnPaint (PaintEventArgs pea) {g = pea. graphics; g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; g. textRenderingHint = System. drawing. text. textRenderingHint. antiAliasGridFit; DrawClock (g); base. onPaint (pea);} private void DrawClock (Graphics gg) {m_Bitmap = new Bitmap (this. width, this. height); Graphics grfx = Graphics. fromIm Age (m_Bitmap); grfx. clear (Color. white); InitializeCoordinates (grfx); DrawDots (grfx, brush); DrawHourHand (grfx, pen); DrawMinuteHand (grfx, pen); DrawSecondHand (grfx, pen ); drawNumbers (grfx); gg. drawImage (m_Bitmap, 0, 0); grfx. dispose ();} void InitializeCoordinates (Graphics grfx) {if (Width = 0 | Height = 0) return; grfx. translateTransform (Width/2, Height/2); float fInches = Math. min (Width /Grfx. dpiX, Height/grfx. dpiY); grfx. scaleTransform (fInches * grfx. dpiX/2000, fInches * grfx. dpiY/2000);} void DrawDots (Graphics grfx, Brush brush) {for (int I = 0; I <60; I ++) {int iSize = I % 5 = 0? 100: 30; grfx. fillEllipse (brush, 0-iSize/2,-860-iSize/2, iSize, iSize); grfx. rotateTransform (6) ;}} protected virtual void DrawHourHand (Graphics grfx, Pen pen) {GraphicsState gs = grfx. save (); grfx. rotateTransform (360f * Time. hour/12 + 30f * Time. minute/60 + 2); grfx. drawPolygon (pen, new Point [] {new Point (0,150), new Point (100, 0), new Point (0,-600), new Point (-100, 0)}); gr Fx. restore (gs);} protected virtual void DrawMinuteHand (Graphics grfx, Pen pen) {GraphicsState gs = grfx. save (); grfx. rotateTransform (360f * Time. minute/60 + 6f * Time. second/60 + 2); grfx. drawPolygon (pen, new Point [] {new Point (0,200), new Point (50, 0), new Point (0,-800), new Point (-50, 0)}); grfx. restore (gs);} protected virtual void DrawSecondHand (Graphics grfx, Pen pen) {GraphicsSta Te gs = grfx. save (); grfx. rotateTransform (360f * Time. second/60 + 6f * Time. millisecond/1000 + 2.8f); grfx. drawLine (penSecond, 0, 0, 0,-800); grfx. restore (gs);} private void InitializeComponent () {// ClockControl // this. name = "ClockControl"; this. size = new System. drawing. size (416,424);} protected void DrawNumbers (Graphics grfx) {int count = 1; int r = 1900; int dx = 25; int dy = 20; Int offset = 8; int dr = 300; GraphicsState gs = grfx. save (); grfx. translateTransform (-r/2-dx,-r/2-dy); grfx. drawEllipse (new Pen (Color. black, 10), dx/2 + offset, dy/2 + offset, r + dx-offset, r + dy-offset); // grfx. drawEllipse (new Pen (Color. black, 2), dx + offset, dy + offset, r-offset, r-offset); grfx. translateTransform (dr/3 + 15, dr/3 + 15); for (double a = 0; a <2 * Math. P I; a + = (2.0 * Math. PI/12) {double x = (r-dr * 1.2)/2 * Math. cos (a-Math. PI/3) + (r-dr)/2 + 25; double y = (r-dr * 1.2)/2 * Math. sin (a-Math. PI/3) + (r-dr)/2 + 20; grfx. drawString (Convert. toString (count), ClockFont, Brushes. black, (float) x, (float) y, new StringFormat (); count ++;} grfx. restore (gs); // grfx. drawLine (new Pen (Color. black, 1), 0,-800, 0,800) ;}} then from BezierClockControl and then from C The lockControl control is inherited. Because of the different painting methods, the final display effect is also different: // BezierClockControl. cs using System; using System. drawing; using System. drawing. drawing2D; using System. windows. forms; namespace BrawDraw. com. photoFrame. net. publicFunctions {public class BezierClockControl: ClockControl {protected override void DrawHourHand (Graphics grfx, Pen pen) {GraphicsState gs = grfx. save (); grfx. rotateTransform (360f * Time. hour/12 + 3 0f * Time. minute/60); GraphicsPath gp = new GraphicsPath (); Point [] points = new Point [] {new Point (0,-600), new Point (0, -300), new Point (200,-300), new Point (50,-200), new Point (50,-200), new Point (50, 0 ), new Point (50, 0), new Point (50, 75), new Point (-50, 75), new Point (-50, 0), new Point (-50, 0), new Point (-50,-200), new Point (-50,-200), new Point (-200,-300), new Point (0, -300), new Point (0,-600)}; gp. addBeziers (points); // grfx. drawBeziers (pen, points); grfx. fillPath (new SolidBrush (Color. red), gp); grfx. restore (gs); gp. dispose ();} private void InitializeComponent () {// BezierClockControl/this. name = "BezierClockControl"; this. size = new System. drawing. size (448,376);} protected override void DrawMinuteHand (Graphics grfx, Pen pen) {GraphicsState gs = gr Fx. save (); grfx. rotateTransform (360f * Time. minute/60 + 6f * Time. second/60); GraphicsPath gp = new GraphicsPath (); Point [] points = new Point [] {new Point (0,-800), new Point (0, -750), new Point (0,-700), new Point (25,-600), new Point (25,-600), new Point (25, 0 ), new Point (25, 0), new Point (25, 50), new Point (-25, 50), new Point (-25, 0), new Point (-25, 0), new Point (-25,-600), ne W Point (-25,-600), new Point (0,-700), new Point (0,-750), new Point (0,-800)}; gp. addBeziers (points); grfx. fillPath (new SolidBrush (Color. green), gp); // grfx. drawBeziers (pen, points); grfx. restore (gs); gp. dispose () ;}} Finally, let's test: using System; using System. drawing; using System. collections; using System. componentModel; using System. windows. forms; using System. data; namespace BrawDraw. com. photoF Rame. Net. PublicFunctions {/// <summary> /// abstract description of BezierClock. /// </Summary> public class BezierClock: System. Windows. Forms. Form {/// <summary> /// a required designer variable. /// </Summary> private System. componentModel. container components = null; // ClockControl clkctl; BezierClockControl clkctl; public BezierClock () {InitializeComponent (); this. setStyle (ControlStyles. doubleBuffer, true); this. setStyle (ControlStyles. allPaintingInWmPaint, true); // Redraw when resized this. setStyle (ControlStyles. resizeRedraw, true); Text = "bezerclock"; // clkctl = new ClockControl (); Clkctl = new BezierClockControl (); clkctl. parent = this; clkctl. time = DateTime. now; clkctl. dock = DockStyle. fill; clkctl. backColor = Color. black; clkctl. foreColor = Color. white; Timer timer = new Timer (); timer. interval = 1000; timer. tick + = new EventHandler (OnTimerTick); timer. start ();} void OnTimerTick (object obj, EventArgs ea) {clkctl. time = DateTime. now ;}//< summary> /// clear all resources in use. /// </Summary> protected override void Dispose (bool disposing) {if (components! = Null) {components. dispose () ;}} base. dispose (disposing );} # region Windows Form Designer generated code /// <summary> /// the designer supports the required methods-do not use the code editor to modify the content of this method. /// </Summary> private void InitializeComponent () {// BezierClock // this. autoScaleBaseSize = new System. drawing. size (6, 14); this. clientSize = new System. drawing. size (432,421); this. name = "BezierClock"; this. text = "BezierClock" ;}# endregion /// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main () {Application. Run (new BezierClock ());}}}

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.