Today, I made a nice digital clock with C #. The interface is as follows.
Implementation technology: Mainly through the Graphics class DrawImage method to draw all the numbers in the digital clock, these numbers are from the Internet to find some image files. The clock uses the now property in DateTime to get different, time, minute, second, and finally the clock running state through the timer.
Main Code:
[C-sharp]View Plaincopy
- Save the 0~9 digital picture in the image array
- Private image[] Image = new bitmap[10];
- Public Form1 ()
- {
- InitializeComponent ();
- For (int i = 0; i < 10;i++)
- {
- Image[i] = new Bitmap (@"d:/Programming/c#/digital clock/digital clock/resources/" +i.tostring () +". jpg");
- }
- }
[C-sharp]View Plaincopy
- Private void Form1_paint (object sender, PaintEventArgs e)
- {
- Graphics g = e.graphics;
- int hh = DateTime.Now.Hour; //Get hour numbers
- int hh1 = HH/10;
- int hh2 = hh% 10;
- G.drawimage (IMAGE[HH1], 20, 20, 80, 180);
- G.drawimage (IMAGE[HH2], 100, 20, 80, 180);
- int mm = DateTime.Now.Minute; //Get minute numbers
- int mm1 = MM/10;
- int mm2 = mm% 10;
- G.drawimage (IMAGE[MM1], 260, 20, 80, 180);
- G.drawimage (image[mm2], 340, 20, 80, 180);
- int ss = DateTime.Now.Second; //Get seconds digits
- int ss1 = SS/10;
- int SS2 = ss% 10;
- G.drawimage (Image[ss1], 500, 20, 80, 180);
- G.drawimage (Image[ss2], 580, 20, 80, 180);
- }
- Private void Timer1_Tick (object sender, EventArgs e) //Redraw the form
- {
- This . Invalidate ();
- }
Also, you need to set the timer's Interval property to 1000mm,enable to true!
C # Development of beautiful digital clocks